pring MVC进阶

1、使用注解的controller与实现接口的controller不是同一个

2、dispatcherServlet不能使用注解式配置,因为这是spring的源码,我们无法修改

3、类之上也可以加注解requestMapping,指定父路径

packagecom.innovation.controller;importcom.innovation.bean.Employee;importcom.innovation.service.EmployeeValidate;importcom.innovation.service.IEmployeeService;importcom.innovation.service.impl.EmployeeServiceImpl;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.validation.BindingResult;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importjavax.servlet.http.HttpServletRequest;importjava.util.List;/**

* @author huwen

*/@Controller@RequestMapping("/main")publicclassEmployeeController{privateIEmployeeServiceservice=newEmployeeServiceImpl();@RequestMapping("/employees")publicStringgetEmployeesPage(Modelmodel){Listlist=service.getAllEmployees();model.addAttribute("list",list);return"employees.jsp";}@RequestMapping("/employeeInfo")publicStringgetEmployeeById(HttpServletRequestrequest,Modelmodel){Stringeid=request.getParameter("eid");intid=eid==null?-1:Integer.parseInt(eid);Employeee=service.getEmployeeById(id);model.addAttribute("employee",e);return"employeeInfo.jsp";}@GetMapping("/deleteEmployee/{eid}")publicStringdeleteEmployeeById(@PathVariableinteid){if(service.deleteEmployeeById(eid)){return"redirect:/main/employees";}return"";}@PostMapping("/updateEmployee")publicStringupdateEmployeeById(Employeee,BindingResulterrors,Modelmodel){EmployeeValidateev=newEmployeeValidate();ev.validate(e,errors);if(errors.hasErrors()){model.addAttribute("employee",e);return"employeeInfo.jsp";}return"redirect:/main/employees";}}

比如如果我们想要嗲用updateEmployeeById这个方法,就需要访问localhost:8080/main/updateEmployee

4、Spring MVC可以自动帮我们把表单数据填充进一个JavaBean中,前提是表单数据中的name与JavaBean的成员变量名称要一致

publicStringupdateEmployeeById(Employeee,BindingResulterrors,Modelmodel){EmployeeValidateev=newEmployeeValidate();ev.validate(e,errors);if(errors.hasErrors()){model.addAttribute("employee",e);return"employeeInfo.jsp";}return"redirect:/main/employees";}

<%--  Created by IntelliJ IDEA.  User: huwen  Date: 2020/3/3  Time: 20:58  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>        employee-info            编号:
        姓名:
        薪资:
           

publicclassEmployee{privateinteid;privateStringename;privatedoublesalary;}

Spring MVC的底层还是使用反射的方式将表单数据填充进一个JavaBean中的。

5、问号传参与路径传参

${item.eid}${item.ename}${item.salary}更新删除

@RequestMapping("/employeeInfo")publicStringgetEmployeeById(HttpServletRequestrequest,Modelmodel){Stringeid=request.getParameter("eid");intid=eid==null?-1:Integer.parseInt(eid);Employeee=service.getEmployeeById(id);model.addAttribute("employee",e);return"employeeInfo.jsp";}@GetMapping("/deleteEmployee/{eid}")publicStringdeleteEmployeeById(@PathVariableinteid){if(service.deleteEmployeeById(eid)){return"redirect:/main/employees";}return"";}

上面的代码,更新是问号传参,删除是路径传参。

问号传参,需要使⽤问号来拼接参数,在接受⽅,使⽤request.getParameter("key")来获取问号所

传递过来的值,如果数据类型不为String,还需要⼿动转换。可以传递多个值,如果使⽤多个值,使⽤&

来拼接,不会改变路径级别

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

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

求。

通配:

packagecom.qfedu.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;@ControllerpublicclassPathController{//@RequestMapping("/a")@RequestMapping("/{path}")publicStringgetPath(@PathVariableStringpath){returnpath;}//@RequestMapping("/a")@RequestMapping("/a")publicStringa(){return"a";}}

6、指定匹配请求方式

HTTP请求方式有八种,Spring MVC可以指定只接受指定类型的请求,由两种方式实现:

@RequestMapping(value="/updateEmployee",method=RequestMethod.POST)publicStringupdateEmployeeById(Employeee,BindingResulterrors,Modelmodel){EmployeeValidateev=newEmployeeValidate();ev.validate(e,errors);if(errors.hasErrors()){model.addAttribute("employee",e);return"employeeInfo.jsp";}service.updateEmployeeById(e);return"redirect:/main/employees";}

@PostMapping("/updateEmployee")publicStringupdateEmployeeById(Employeee,BindingResulterrors,Modelmodel){EmployeeValidateev=newEmployeeValidate();ev.validate(e,errors);if(errors.hasErrors()){model.addAttribute("employee",e);return"employeeInfo.jsp";}service.updateEmployeeById(e);return"redirect:/main/employees";}

7、校验与国际化

packagecom.innovation.service;importcom.innovation.bean.Employee;importorg.springframework.validation.Errors;importorg.springframework.validation.ValidationUtils;importorg.springframework.validation.Validator;publicclassEmployeeValidateimplementsValidator{@Overridepublicbooleansupports(Classclazz){returnEmployee.class.isAssignableFrom(clazz);}@Overridepublicvoidvalidate(Objecttarget,Errorserrors){Employeee=(Employee)target;ValidationUtils.rejectIfEmptyOrWhitespace(errors,"ename","emp.ename");ValidationUtils.rejectIfEmptyOrWhitespace(errors,"salary","emp.salary");doublesalary=e.getSalary();if(salary<0){errors.rejectValue("salary","emp.salary.invalidate");}}}

这是一个专门用来进行校验的类,实现了Validator接口,重写了supports()与validate()方法,supports方法只校验返回值为true的类,validate则是具体执行校验的方法,在进行校验的过程中要指定字段与错误码,字段是对指定的属性进行校验,错误码需要单独一个配置文件进行配置:

message_en_US.properties

emp.ename=name cannot be nullemp.salary=salary cannot be nullemp.salary.invalidate=salary cannot be negative

上面这个配置文件当出现错误后如果浏览器的语言设置为美式英语就会显示出来

message_zh_CN.properties

emp.ename=姓名不能为空emp.salary=薪资不能为空emp.salary.invalidate=薪资不能为负

上面这个配置文件当出现错误后如果浏览器的语言设置为中文(中国大陆)就会显示出来。要想使用这两个配置文件必须在spring配置文件中添加:

@PostMapping("/updateEmployee")publicStringupdateEmployeeById(Employeee,BindingResulterrors,Modelmodel){EmployeeValidateev=newEmployeeValidate();ev.validate(e,errors);if(errors.hasErrors()){model.addAttribute("employee",e);return"employeeInfo.jsp";}service.updateEmployeeById(e);return"redirect:/main/employees";}

在这个方法中创建了一个EmplooyeeValidate对象用于进行校验,这个方法接收三个参数,第一个参数是前台页面传过来的,第二个参数类型是一个接口,继承于Errors,用于保存错误信息,第三个参数用于向request域中添加属性。

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>        employee-info            编号:
        姓名:
        薪资:
           

这个jsp页面引入了spring的form标签库,modelAttribute用于从request域中获取数据,commandName已经过时了,高版本的spring使用commandName会报错:Unable to find setter method for attribute: [commandName]。readonly属性不能设置为“readonly”,必须设置为“true”才会生效。path可以认为是对象的属性。

你可能感兴趣的:(pring MVC进阶)