SpringMVC实现与CRUD整合

SpringMVC实现与CRUD整合

说明,该demo中使用List模拟了一套数据源,可以实现简单的crud操作,其中修改使用了SpringMVC的问号传参,删除操作使用了路径传参。
对比问号传参与路径传参:

问号传参,需要使用问号来拼接参数,在接受方,使用request.getParameter(“key”)来获取问号所传递过来的值,如果数据类型不为String,还需要手动转换。可以传递多个值,如果使用多个值,使用&来拼接,不会改变路径级别

路径传参,使用路径符号来传递参数,优点,可以不用做类型转换来直接获取其值。

路径传参也可以使用统配规则,如果同时统配和具体的url都满足,则以最具体的url来处理该请求。

1.Emp.java

package com.qfedu.bean;

public class Emp {

    private int eid;
    private String name;
    private double salary;

    public Emp() {
    }

    public Emp(int eid, String name, double salary) {
        this.eid = eid;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

2.EmpController.java

​ 1.RequestMapping

​ 1.可以通过method来区分不同的请求方式
​ 2.@RequestMapping(value = “/updateEmp”, method = RequestMethod.POST)代表处理post请求
​ 3.@RequestMapping(value = “/updateEmp”, method = RequestMethod.GET)代表处理get请求
​ 2.GETMapping,可以简化代码,专门用来处理get请求(4.3以后的Spring版本可用)

​ 3.PostMapping,可以简化代码,专门用来处理post请求(4.3以后的Spring版本可用)

​ 4.PathVariable路径传参的注解,可以实现路径传参。

package com.qfedu.controller;

import com.qfedu.bean.Emp;
import com.qfedu.service.IEmpService;
import com.qfedu.service.impl.EmpServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {

    private IEmpService empService = new EmpServiceImpl();

    /**
     * 如果有了users请求,那么该方法会被调用,返回值为将来要渲染的页面
     * @return
     */
    @RequestMapping("/emps")
    public String getUsersPage(Model model, HttpSession session){

        List list = empService.getAllEmps();

        model.addAttribute("list", list);

        session.setAttribute("list",  list);

        return "emp.jsp";
    }

    @RequestMapping("/getEmpByEid")
    public String getEmpByEid(HttpServletRequest request, Model model){

        String seid = request.getParameter("eid");

        int eid = seid == null ? -1 : Integer.parseInt(seid);

        Emp emp = empService.getEmpByEid(eid);

        model.addAttribute("emp", emp);

        return "updateEmp.jsp";
    }

    //@RequestMapping(value = "/updateEmp", method = RequestMethod.POST)
    @PostMapping("/updateEmp")
    //public String updateEmp(HttpServletRequest request){
    public String updateEmp(Emp e){

        //System.out.println(request.getParameter("eid"));
        System.out.println(e);

        boolean flag = empService.updateEmp(e);

        if(flag){
            return "redirect:/emp/emps";
        }

        return "";
    }


    @GetMapping("/deleteByEid/{eid}")
    public String deleteByEid(@PathVariable int eid){
        //System.out.println(eid);

        boolean flag = empService.deleteEmpByEid(eid);

        if(flag){
            return "redirect:/emp/emps";
        }

        return "";
    }
}

3.IEmpService.java

package com.qfedu.service;

import com.qfedu.bean.Emp;

import java.util.List;

public interface IEmpService {

    List getAllEmps();

    Emp getEmpByEid(int eid);

    boolean updateEmp(Emp emp);

    boolean deleteEmpByEid(int eid);
}

4.EmpServiceImpl.java

package com.qfedu.service.impl;

import com.qfedu.bean.Emp;
import com.qfedu.dao.impl.EmpDaoImpl;
import com.qfedu.dao.IEmpDao;
import com.qfedu.service.IEmpService;

import java.util.List;

public class EmpServiceImpl implements IEmpService {

    private IEmpDao empDao = new EmpDaoImpl();

    @Override
    public List getAllEmps() {
        return empDao.getAllEmps();
    }

    @Override
    public Emp getEmpByEid(int eid) {
        return empDao.getEmpByEid(eid);
    }

    @Override
    public boolean updateEmp(Emp emp) {
        return empDao.updateEmp(emp);
    }

    @Override
    public boolean deleteEmpByEid(int eid) {
        return empDao.deleteEmpByEid(eid);
    }
}

5.IEmpDao.java

package com.qfedu.dao;

import com.qfedu.bean.Emp;

import java.util.List;

public interface IEmpDao {

    List getAllEmps();

    Emp getEmpByEid(int eid);

    boolean updateEmp(Emp emp);

    boolean deleteEmpByEid(int eid);
}

6.EmpDaoImpl.java

package com.qfedu.dao.impl;

import com.qfedu.bean.Emp;
import com.qfedu.dao.IEmpDao;

import java.util.ArrayList;
import java.util.List;

public class EmpDaoImpl implements IEmpDao {

    private static List emps = new ArrayList<>();

    static {
        for(int i = 0; i < 20; i++){
            emps.add(new Emp(i + 1, "name " + i, 8000 + i * 100));
        }
    }

    @Override
    public List getAllEmps() {
        return emps;
    }


    @Override
    public Emp getEmpByEid(int eid) {
        return emps.get(eid - 1);
    }

    @Override
    public boolean updateEmp(Emp emp) {

        try{
            emps.set(emp.getEid() - 1, emp);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean deleteEmpByEid(int eid) {
        try{
            emps.remove(eid -1 );
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}

7.emp.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    user


    

this is users page.

eid name salary manage
  ${e.eid}   ${e.name}   ${e.salary}   update delete

8.updateEmp.jsp

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


    update Emp


    

this is emp update page.

eid:
name:
salary:
<%--salary:
--%>

9.spring-mvc.xml




    
    
        
        
    

    
    

    
    

    
    

    
    
    

你可能感兴趣的:(SpringMVC实现与CRUD整合)