-
接受请求的输入在SpringMVC中一共有3种方式
(1) 查询参数
(2) 路径参数
(3) 表单参数
-
处理查询参数
(1) 在url中添加查询参数
示例
"/spittles?max=238900&count=50"
(2) 处理方式:添加 @RequestParam注解
示例
@Controller @RequestMapping("/spittles") public class SpittleController { ... private static final String MAX_LONG_AS_STRING = "9223372036854775807"; @RequestMapping(method = RequestMethod.GET) public String spittles( @RequestParam(value = "max", defaultValue = MAX_LONG_AS_STRING) long max, @RequestParam(value = "count", defaultValue = "20") int count, Model model) { model.addAttribute("spittleList", spittleRepository.findSpittles(max, count)); return "spittles"; } ... }
@RequestParam的value属性用来指定参数名称(url和value要对应);
@RequestParam的defaultValue属性用来指定默认值,由于查询参数都是String类型的,所以defaultValue的值一定要是String
-
通过路径参数接受输入
(1) 需求:根据id查询历史输入
(2) 如果使用查询参数的方法@RequestParam,则会有如下代码
@Controller @RequestMapping("/spittles") public class SpittleController { ... private static final String MAX_LONG_AS_STRING = "9223372036854775807"; @RequestMapping(value="/show", method = RequestMethod.GET) public String spittle( @RequestParam(value = "spittle_id") long spittleId, Model model) { model.addAttribute("spittle", spittleRepository.findOne(spittleId)); return "spittles"; } ... }
此时url应该为 "/spittles/show?spittle_id=xxx?",但是这样不好,我们希望的是做到 "/spittles/xxx"
(3) 处理方式:添加@PathVariable注解
示例
@Controller @RequestMapping("/spittles") public class SpittleController { ... private static final String MAX_LONG_AS_STRING = "9223372036854775807"; @RequestMapping(value = "/{spittleId}", method = RequestMethod.GET) public String spittle( @PathVariable("spittleId") long spittleId, Model model) { model.addAttribute("spittle", spittleRepository.findOne(spittleId)); return "spittle"; } ... }
@PathVariable中的代表URL中/xxx的xxx部分,在方法上方的@RequestMapping中使用了占位符{}
-
表单输入
(1) 如果请求中包含大量数据,那么就不能使用查询参数和路径参数了
(2) 当form中没有指定action属性时,代表提交表单的目标是当前页面本身
(3) 在处理post类型的请求时,在请求处理完成后,最好进行一下重定向,这样浏览器的刷新就不会重复提交表单了
示例
@Controller @RequestMapping("/spitter") public class SpitterController { @RequestMapping(value = "/register", method = POST) public String processRegistration( String username, String password, String firstName, String lastName, String email) { ... return "redirect:/spitter/" + spitter.getUsername(); } }
(4) 接受form提交参数的两种方式(一):直接获取同名参数
示例
registerForm.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>Spitter</title> <link rel="stylesheet" type="text/css" href="<c:url value="/resources/style.css" />" > </head> <body> <h1>Register</h1> <form method="POST"> First Name: <input type="text" name="firstName" /><br/> Last Name: <input type="text" name="lastName" /><br/> Email: <input type="email" name="email" /><br/> Username: <input type="text" name="username" /><br/> Password: <input type="password" name="password" /><br/> <input type="submit" value="Register" /> </form> </body> </html>
SpitterController.java
@Controller @RequestMapping("/spitter") public class SpitterController { @RequestMapping(value = "/register", method = POST) public String processRegistration( String username, String password, String firstName, String lastName, String email) { // If parameters check failed, go back to registerForm.jsp if (Spitter.checkIfInputParametersHaveErrors(username, password, firstName, lastName, email)) { return "registerForm"; } Spitter spitter = new Spitter(username, password, firstName, lastName, email); spitterRepository.save(spitter); return "redirect:/spitter/" + spitter.getUsername(); } }
其中,username, password, firstName, lastName, email几个参数在jsp文件和Controller文件中是一一对应的。这种方式比较直观,但是写起来繁琐,特别是要进行输入参数检查的时候
(5)接受form提交参数的两种方式(一):用同名参数填充对象
示例
registerForm.jsp 同上个示例
SpitterController.java
@Controller @RequestMapping("/spitter") public class SpitterController { @RequestMapping(value = "/register", method = POST) public String processRegistration(Spitter spitter) { spitterRepository.save(spitter); return "redirect:/spitter/" + spitter.getUsername(); } }
Spitter.java
public class Spitter { ... public Spitter(String username, String password, String firstName, String lastName, String email) { this(null, username, password, firstName, lastName, email); } public Spitter(Long id, String username, String password, String firstName, String lastName, String email) { this.id = id; this.username = username; this.password = password; this.firstName = firstName; this.lastName = lastName; this.email = email; } ... }
直接使用同名的参数填充进Spitter的构造函数中
(6) 校验form
可以不使用Java的校验API自己手动编写代码校验,但是这样比较麻烦;
比较好的方式是在Spitter类中添加Java的校验API,从Spring3.0起添加了对Java校验API的支持
示例 Spitter.java
import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email; public class Spitter { private Long id; @NotNull @Size(min = 5, max = 16) private String username; @NotNull @Size(min = 5, max = 25) private String password; @NotNull @Size(min = 2, max = 30) private String firstName; @NotNull @Size(min = 2, max = 30) private String lastName; @NotNull @Email private String email; public Spitter() { } public Spitter(String username, String password, String firstName, String lastName, String email) { this(null, username, password, firstName, lastName, email); } public Spitter(Long id, String username, String password, String firstName, String lastName, String email) { this.id = id; this.username = username; this.password = password; this.firstName = firstName; this.lastName = lastName; this.email = email; } ... }
(7) Java API提供的几个常用校验注解
@AssertTrue
@Digits
@Max
@Min
@NotNull
@Size
@Pattern
(8) 在Controller中启用校验功能: @Valid + Errors
示例
SpitterController.java
@Controller @RequestMapping("/spitter") public class SpitterController { @RequestMapping(value = "/register", method = POST) public String processRegistration(@Valid Spitter spitter, Errors errors) { // If parameters check failed, go back to registerForm.jsp if (errors.hasErrors()) { return "registerForm"; } spitterRepository.save(spitter); return "redirect:/spitter/" + spitter.getUsername(); } }
在方法的参数上添加 @Valid注解会告知Spring对这个对象进行参数校验,如果校验出现错误的话,可以通过Errors对象访问到
注意方法的参数顺序必须是 @Valid Mytype myType, Errors error不能改变
-
重定向
return "redirect:/xxx";
-
转发
return "forward:/xxx";
chapter05_构建Spring Web应用程序_3_接受请求的输入
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本章内容: 映射请求到Spring控制器 透明地绑定表单参数 校验表单提交 状态管理、工作流以及验证都是Web 开...
- 本章主要内容: 备用的Spring MVC配置项 处理文件上传 控制器中的异常处理 使用flash属性 “等等,客...
- 1、不安全的随机数生成,在CSRF TOKEN生成、password reset token生成等,会造成toke...