Spring MVC进阶
1、使用注解的controller与实现接口的controller不是同一个
2、dispatcherServlet不能使用注解式配置,因为这是spring的源码,我们无法修改
3、类之上也可以加注解requestMapping,指定父路径
package com.innovation.controller;
import com.innovation.bean.Employee;
import com.innovation.service.EmployeeValidate;
import com.innovation.service.IEmployeeService;
import com.innovation.service.impl.EmployeeServiceImpl;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @author huwen
*/
@Controller
@RequestMapping("/main")
public class EmployeeController {
private IEmployeeService service = new EmployeeServiceImpl();
@RequestMapping("/employees")
public String getEmployeesPage(Model model){
List<Employee> list = service.getAllEmployees();
model.addAttribute("list",list);
return "employees.jsp";
}
@RequestMapping("/employeeInfo")
public String getEmployeeById(HttpServletRequest request,Model model){
String eid = request.getParameter("eid");
int id = eid==null?-1:Integer.parseInt(eid);
Employee e = service.getEmployeeById(id);
model.addAttribute("employee",e);
return "employeeInfo.jsp";
}
@GetMapping("/deleteEmployee/{eid}")
public String deleteEmployeeById(@PathVariable int eid){
if(service.deleteEmployeeById(eid)){
return "redirect:/main/employees";
}
return "";
}
@PostMapping("/updateEmployee")
public String updateEmployeeById(Employee e, BindingResult errors,Model model){
EmployeeValidate ev = new EmployeeValidate();
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的成员变量名称要一致
public String updateEmployeeById(Employee e, BindingResult errors,Model model){
EmployeeValidate ev = new EmployeeValidate();
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" %>
<html>
<head>
<meta charset="UTF-8">
<title>employee-info</title>
</head>
<body>
<f:form action="/main/updateEmployee" method="post" modelAttribute="employee">
编号:<f:input type="text" name="eid" readonly="true" path="eid"/><br>
姓名:<f:input type="text" name="ename" path="ename" /><f:errors path="ename"/> <br>
薪资:<f:input type="text" name="salary" path="salary"/><f:errors path="salary"/> <br>
<input type="submit" value="提交">
</f:form>
</body>
</html>
public class Employee {
private int eid;
private String ename;
private double salary;
}
Spring MVC的底层还是使用反射的方式将表单数据填充进一个JavaBean中的。
5、问号传参与路径传参
<td>${item.eid}</td><td>${item.ename}</td><td>${item.salary}</td>
<td><a href="/main/employeeInfo?eid=${item.eid}">更新</a></td>
<td><a href="/main/deleteEmployee/${item.eid}">删除</a></td>
@RequestMapping("/employeeInfo")
public String getEmployeeById(HttpServletRequest request,Model model){
String eid = request.getParameter("eid");
int id = eid==null?-1:Integer.parseInt(eid);
Employee e = service.getEmployeeById(id);
model.addAttribute("employee",e);
return "employeeInfo.jsp";
}
@GetMapping("/deleteEmployee/{eid}")
public String deleteEmployeeById(@PathVariable int eid){
if(service.deleteEmployeeById(eid)){
return "redirect:/main/employees";
}
return "";
}
上面的代码,更新是问号传参,删除是路径传参。
- 问号传参,需要使⽤问号来拼接参数,在接受⽅,使⽤request.getParameter("key")来获取问号所
传递过来的值,如果数据类型不为String,还需要⼿动转换。可以传递多个值,如果使⽤多个值,使⽤&
来拼接,不会改变路径级别 - 路径传参,使⽤路径符号来传递参数,优点,可以不⽤做类型转换来直接获取其值。
- 路径传参也可以使⽤统配规则,如果同时通配和具体的url都满⾜,则以最具体的url来处理该请
求。
通配:
package com.qfedu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PathController {
//@RequestMapping("/a")
@RequestMapping("/{path}")
public String getPath(@PathVariable String path){
return path;
}
//@RequestMapping("/a")
@RequestMapping("/a")
public String a(){
return "a";
}
}
6、指定匹配请求方式
HTTP请求方式有八种,Spring MVC可以指定只接受指定类型的请求,由两种方式实现:
@RequestMapping(value = "/updateEmployee",method = RequestMethod.POST)
public String updateEmployeeById(Employee e, BindingResult errors,Model model){
EmployeeValidate ev = new EmployeeValidate();
ev.validate(e,errors);
if(errors.hasErrors()){
model.addAttribute("employee",e);
return "employeeInfo.jsp";
}
service.updateEmployeeById(e);
return "redirect:/main/employees";
}
@PostMapping("/updateEmployee")
public String updateEmployeeById(Employee e, BindingResult errors,Model model){
EmployeeValidate ev = new EmployeeValidate();
ev.validate(e,errors);
if(errors.hasErrors()){
model.addAttribute("employee",e);
return "employeeInfo.jsp";
}
service.updateEmployeeById(e);
return "redirect:/main/employees";
}
7、校验与国际化
package com.innovation.service;
import com.innovation.bean.Employee;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class EmployeeValidate implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Employee.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
Employee e = (Employee) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors,"ename","emp.ename");
ValidationUtils.rejectIfEmptyOrWhitespace(errors,"salary","emp.salary");
double salary = 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 null
emp.salary=salary cannot be null
emp.salary.invalidate=salary cannot be negative
上面这个配置文件当出现错误后如果浏览器的语言设置为美式英语就会显示出来
message_zh_CN.properties
emp.ename=姓名不能为空
emp.salary=薪资不能为空
emp.salary.invalidate=薪资不能为负
上面这个配置文件当出现错误后如果浏览器的语言设置为中文(中国大陆)就会显示出来。要想使用这两个配置文件必须在spring配置文件中添加:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/message"/>
</bean>
@PostMapping("/updateEmployee")
public String updateEmployeeById(Employee e, BindingResult errors,Model model){
EmployeeValidate ev = new EmployeeValidate();
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" %>
<html>
<head>
<meta charset="UTF-8">
<title>employee-info</title>
</head>
<body>
<f:form action="/main/updateEmployee" method="post" modelAttribute="employee">
编号:<f:input type="text" name="eid" readonly="true" path="eid"/><br>
姓名:<f:input type="text" name="ename" path="ename" /><f:errors path="ename"/> <br>
薪资:<f:input type="text" name="salary" path="salary"/><f:errors path="salary"/> <br>
<input type="submit" value="提交">
</f:form>
</body>
</html>
这个jsp页面引入了spring的form标签库,modelAttribute用于从request域中获取数据,commandName已经过时了,高版本的spring使用commandName会报错:Unable to find setter method for attribute: [commandName]。readonly属性不能设置为“readonly”,必须设置为“true”才会生效。path可以认为是对象的属性。
网友评论