使用SSM+BootStrap实现增删改查和头像上传效果

这篇文章主要讲解了“使用SSM+BootStrap实现增删改查和头像上传效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“使用SSM+BootStrap实现增删改查和头像上传效果”吧!

创新互联建站服务项目包括武安网站建设、武安网站制作、武安网页制作以及武安网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,武安网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到武安省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

先看界面

使用SSM+BootStrap实现增删改查和头像上传效果

点击编辑之后

使用SSM+BootStrap实现增删改查和头像上传效果

具体代码请往下看

一、jsp界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



















    
        
            学生编号
            学生姓名
            班级名称
            头像
            状态
            操作
        
    
    
        
            
                
                    
                        ×
                        学生信息修改
                    
                                             学生编号:
 学生姓名:
                        所在班级:                          当前头像:                         
                                                     
                        用户头像:                                                                               
                                             
                                             提交更改                         关闭                     
                                                                              $(function() {         $("#result").bootstrapTable({             url : "selectAll.do",             method : "post",             cache : false,             dataType : "json",             contentType : "application/x-www-form-urlencoded",//post的方式提交的话需要写             toolbar : "#toolbar",             toolbarAlign : "left",             striped : true,             pagination : true,             sidePagination : "server",             pageNumber : 1,             pageSize : 5,             pageList : [ 5, 10, 15 ],             locale : "zh-CN",             queryParamsType : "limit",             queryParams : queryParams         });     });     function queryParams(params) {         var params = {             pageSize : params.limit,             pageCode : params.offset / params.limit + 1         };         return params;     };     function state(value, row, index) {         if (row['state'] === 0) {             return "正常";         }         if (row['state'] === 1) {             return "锁定";         }         return value;     }     function image(value, row, index) {         return "";     }     /*操作按钮*/     function toolbar(value, row, index) {         var element = "编辑"                 + "删除"                 + "下载";         return element;     }     /*编辑按钮,弹出模态框*/     function edit(stuid) {         $("#class option").remove();         $("#formid")[0].reset();         $.ajax({             url : "editBystuid.do?stuid=" + stuid,             type : "post",             dataType : "json",             success : function(data) {                 $('#myModal').modal('show');                 $("[name=stuid]").val(data[0].stuid);                 $("#stuname").val(data[0].stuname);                 $("#userimage").val(data[0].userimage);                 $("#img").attr('src', data[0].userimage);                 $("#class").append(                         ""                                 + data[0].classes.classname + "");             }         });         $.ajax({             url : "selectAllClass.do",             type : "post",             dataType : "json",             success : function(data) {                 var obj = $("#class");                 for (var i = 0; i < data.length; i++) {                     var op = ""                             + data[i].classname + "";                     obj.append(op);                 }             }         })     };     /*修改操作*/     function update() {         $.ajax({             url : "updateBystuid.do",             type : "get",             dataType : "text",             data : $("#formid").serialize(),             success : function(data) {                 if (data == "ok") {                     $("#result").bootstrapTable("refresh", {                         url : "selectAll.do"                     });                 } else if (data == "error") {                     alert("修改失败!");                 }             }         });     }     /*删除操作*/     function del(stuid) {         if (confirm('确定要删除吗?') == true) {             $.ajax({                 url : "delBystuid.do?stuid=" + stuid,                 type : "post",                 dataType : "text",                 success : function(data) {                     if (data == "ok") {                         $("#result").bootstrapTable("refresh", {                             url : "selectAll.do"                         });                     } else if (data == "error") {                         alert("删除失败");                     }                 }             })             return false;         }     }     /*下载*/     function download(stuid){         if(confirm('确定要下载头像?')==true){             location.href="download.do?stuid="+stuid;             /* $.ajax({                 url:"download.do?stuid="+stuid,                 type:"post",                 dataType:"json",                 success:function(data){                 }             }); */         }     }

二、Controller层代码

package com.llh.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llh.entity.Student;
import com.llh.service.StudentService;

import net.sf.json.JSONArray;

@Controller
@Scope("prototype")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 分页查询所有
     *
     * @param pageCode
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "selectAll", produces = "text/html;charset=utf-8")
    public @ResponseBody String selectAll(int pageCode, int pageSize) {
        PageHelper.startPage(pageCode, pageSize);
        List slist = studentService.selectAll();
        PageInfo spi = new PageInfo(slist);
        int count = (int) spi.getTotal();
        JSONArray json = JSONArray.fromObject(slist);
        String str = "{\"total\":" + count + ",\"rows\":" + json.toString() + "}";
        return str;
    }

    /**
     * 上传
     *
     * @param request
     * @param file
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @RequestMapping(value = "upload")
    public @ResponseBody String upload(HttpServletRequest request, MultipartFile file)
            throws IllegalStateException, IOException {
        String name = file.getOriginalFilename();
        String path = request.getServletContext().getRealPath("/upload/");// 上传保存的路径
        String fileName = changeName(name);
        String rappendix = "upload/" + fileName;
        fileName = path + "\\" + fileName;
        File file1 = new File(fileName);
        file.transferTo(file1);
        String str = "{\"src\":\"" + rappendix + "\"}";
        return str;
    }

    public static String changeName(String oldName) {
        Random r = new Random();
        Date d = new Date();
        String newName = oldName.substring(oldName.indexOf('.'));
        newName = r.nextInt(99999999) + d.getTime() + newName;
        return newName;
    }

    /**
     * 编辑
     *
     * @param stuid
     * @param session
     * @return
     */
    @RequestMapping(value = "editBystuid", produces = "text/html;charset=utf-8")
    public @ResponseBody String editBystuid(Integer stuid) {
        System.out.println("编辑");
        Student s = studentService.selectByPrimaryKey(stuid);
        JSONArray json = JSONArray.fromObject(s);
        String js = json.toString();
        System.out.println(js);
        return js;
    }

    /**
     * 修改
     *
     * @param stuid
     * @param stuname
     * @return
     */
    @RequestMapping(value = "updateBystuid", produces = "text/html;charset=utf-8")
    public @ResponseBody String updateBystuid(@ModelAttribute Student s) {
        System.out.println("修改中");
        System.out.println(s.getStuname() + s.getStuid()+s.getUserimage());
        int a = studentService.updateByPrimaryKey(s);
        if (a != 0) {
            return "ok";
        }
        return "error";
    }

    /**
     * 下载
     *
     * @param stuid
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "download", produces = "text/html;charset=utf-8")
    public ResponseEntity download(Integer stuid,HttpServletRequest request) throws IOException {
        Student s = studentService.selectByPrimaryKey(stuid);
        String path=request.getServletContext().getRealPath("\\");
        String downpath = path+s.getUserimage();
        File file1=new File(downpath);
        //String downloadFileName=new String(downpath.getBytes("UTF-8"),"iso-8859-1");
        HttpHeaders heads=new HttpHeaders();
        heads.setContentDispositionFormData("attachment", downpath);
        heads.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(FileUtils.readFileToByteArray(file1), heads,HttpStatus.CREATED);
    }



}

三、dao层和service层实体类就掠过了

这里使用到的有自动生成实体类,Maven的分页

具体操作请看首页

感谢各位的阅读,以上就是“使用SSM+BootStrap实现增删改查和头像上传效果”的内容了,经过本文的学习后,相信大家对使用SSM+BootStrap实现增删改查和头像上传效果这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页标题:使用SSM+BootStrap实现增删改查和头像上传效果
本文来源:http://ybzwz.com/article/ijseoe.html