简单SpringMVC增删改查,表单验证功能

首先,新建maven项目,pom.xml引入依赖






4.0.0

com.qfedu

Day17SpringMVC

1.0-SNAPSHOT

war





org.springframework

spring-webmvc

4.3.6.RELEASE





javax.servlet.jsp

jsp-api

2.2

provided





javax.servlet

javax.servlet-api

3.0.1

provided





jstl

jstl

1.2









    

org.apache.maven.plugins

maven-compiler-plugin

3.6.1



1.8

1.8







    

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2



/

8081











UTF-8





MVC三层架构建包,对emp员工类里的list(模仿数据库数据)增删改查


public class Emp {

private int eid;

private Stringename;

private double salary;

//省略get、set、toSting等方法

}

dao层


public class EmpDao {

public static Listemps=new ArrayList<>();

static {

for (int i =0; i <20 ; i++) {

emps.add(new Emp(i,"name"+i,10000+(i*100)));

}

}

public List getAllEmp(){

return emps;

}

public Emp getEmpByEid(int id){

return emps.get(id);

}

public boolean updateEmp(Emp emp){

try {

emps.set(emp.getEid(),emp);

return true;

}catch (Exception e){

e.printStackTrace();

}

return false;

}

public boolean delEmpById(int eid){

try {

emps.remove(eid);

return true;

}catch (Exception e){

e.printStackTrace();

}

return false;

}

}

service层


public class EmpService {

public EmpDaoempDao=new EmpDao();

public List getAllEmp(){

return empDao.getAllEmp();

}

public Emp getEmpByEid(int id){

return empDao.getEmpByEid(id);

}

public boolean updateEmp(Emp emp){

return empDao.updateEmp(emp);

}

public boolean delEmpById(int eid){

return empDao.delEmpById(eid);

}

}

control层 实现controller接口和采用注解方式,所以要配xml

重要代码


package com.qfedu.control;

import com.qfedu.EmpValidate;

import com.qfedu.bean.Emp;

import com.qfedu.service.EmpService;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Controller

@RequestMapping("/emp")

public class EmpControl {

EmpServiceempService=new EmpService();

@RequestMapping("/emps")

public String getEmps(HttpServletRequest request){

List emps=empService.getAllEmp();

if (emps!=null){

request.setAttribute("list",emps);

}

return "emp.jsp";

}

@RequestMapping("/getEmpByEid")

public  String getEmpByEid(Model model,HttpServletRequest request){

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

Emp emp=empService.getEmpByEid(Integer.valueOf(seid));

model.addAttribute("emp",emp);

return "updateEmp.jsp";

}

@PostMapping("/updateEmp")

public String updateEmp(Emp emp){

System.out.println(emp);

boolean flg=empService.updateEmp(emp);

if (flg){

return "redirect:/emp/emps";

}

return "";

}

@GetMapping("/delEmpByEid/{eid}")

public String delEmpByEid(@PathVariable int eid){

boolean flg=empService.delEmpById(eid);

if (flg){

return "redirect:/emp/emps";

}

return "";

}

@GetMapping("/saveEmp")

public ModelAndView saveEmp(){

ModelAndView mv=new ModelAndView("saveEmp.jsp","emp",new Emp());

return mv;

}

@PostMapping("/saveEmp")

public String saveEmp(Emp e, BindingResult result,Model model){

EmpValidate ev=new EmpValidate();

ev.validate(e,result);

if (result.hasErrors()){

model.addAttribute("emp",e);

return  "saveEmp.jsp";

}

return "redirect:/emp/emps";

}

}

spring-mvc.xml








 







   



    



    





    





    







Varlidate验证表单参数


import com.qfedu.bean.Emp;

import org.springframework.validation.Errors;

import org.springframework.validation.ValidationUtils;

import org.springframework.validation.Validator;

public class EmpValidateimplements Validator {

@Override

    public boolean supports(Class aClass) {

return Emp.class.isAssignableFrom(aClass);

}

@Override

    public void validate(Object o, Errors errors) {

Emp emp= (Emp) o;

//        ValidationUtils.rejectIfEmpty(errors, "eid","emp.eid");

        ValidationUtils.rejectIfEmpty(errors,"eid","emp.eid");

ValidationUtils.rejectIfEmpty(errors,"ename","emp.ename");

ValidationUtils.rejectIfEmpty(errors,"salary","emp.salary");

double salary = emp.getSalary();

if(salary <0){

errors.rejectValue("salary","emp.salary.invalidate");

}

}

}

xml里配置的msg路径文件

简单SpringMVC增删改查,表单验证功能_第1张图片
image

/saveEmp请求下的saveEmp.jsp

简单SpringMVC增删改查,表单验证功能_第2张图片
image

当浏览器是英文状态,自动调用msg_en_US.properties

emp.eid=the eid of employee cannot be empty.

emp.ename=the name of employee cannot be empty.

emp.salary=the salary of employee cannot be empty.

emp.salary.invalidate=the salary of employee cannot be negative.

点击提交结果如下图实现

image

你可能感兴趣的:(简单SpringMVC增删改查,表单验证功能)