在MVC结构中,控制器组件主要的功能就是接收请求、处理请求、生成响应,接收客户端传来的请求参数的每每是控制器要作的第一件事。html
Book实体类Book.java
java
public class Book { private Integer bookId; private String author; //生成Get、Set方法,此处省略 }
客户端界面(表单):web
<form action="/queryString" method="post"> <input type="text" name="bookId"> <input type="text" name="author"> <input type="submit" value="提交"> </form>
controller层:ajax
@Controller public class ParamPassDemo { @RequestMapping(value="/queryString") public String test1(Integer bookId, String author) { System.out.println("bookId="+bookId+", author="+author); //此处返回的地址为(/WEB-INF/jsp/index.jsp) return "index"; } }
注意:这里@RequestMapping
中只有value属性,value能够省略不写。spring
客户端输入:123,Roseapp
控制台输出:bookId=123, author=Rosejsp
客户端界面(表单):post
<form action="/queryStringWithSpecName" method="post"> <input type="text" name="bookId" value="321"> <input type="text" name="author" value="Jack"> <input type="submit" value="提交"> </form>
若是表单中的字段与方法中的参数名一致,能够不须要@RequestParam,Spring会自动处理。
controller层:ui
@Controller public class ParamPassDemo { @RequestMapping("/queryStringWithSpecName") public String test2(@RequestParam(value="bookId",required=false) Integer id, @RequestParam("author") String name) { System.out.println("bookId="+id+", author="+name); return "index"; } }
注意:这里@RequestParam
中有两个属性,value不能省略。url
@RequestParam将请求地址中的参数传递给目标方法,在处理方法入参处使用能够把请求参数传递给请求方法。
当使用@RequestParam注解时,设置客户端传递的请求参数name="bookId"
和@RequestParam的value值value="bookId"
相匹配后,参数名int id
能够和请求参数不匹配。
客户端输入:321, Jack
控制台输出:bookId=321, author=Jack
客户端界面(ajax):
<button onclick="clickMe()">点我</button> <script> function clickMe() { $.ajax({ type : 'POST', url : "/queryStringWithSpecName", data : { "bookId" : 1, "author" : "Jack" }, }); } </script>
controller层:(不变)
客户端: data:{"author" : "Jack"}
控制台输出: bookId=null, author=Jack(若是bookId为int类型,控制台会抛出异常)
客户端: data:{"bookId" : 1}
控制台输出: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'author' is not present
经过required
设置可选参数,required
为false
时表示能够不带参数,为true
时表示必须带参数(默认值为true)。
当可选参数不存在时,Spring默认将其赋值为空(null),但因为bookId已定义为基本类型int,因此赋值会失败。 解决方法:采用int包装类Integer。
客户端界面(表单):
<form action="/queryStringWithDomainObj" method="post"> <input type="text" name="bookId"> <input type="text" name="author"> <input type="submit" value="提交"> </form>
controller层:
@Controller public class ParamPassDemo { @RequestMapping("/queryStringWithDomainObj") public String test3(Book book) { System.out.println("bookId="+book.getBookId()+", author="+book.getAuthor()); return "index"; } }
客户端输入:111, Bob
控制台输出:bookId=111, author=Bob
客户端界面(超连接):
<a href="/book/1">testPathVariable</a>
controller层:
@Controller public class ParamPassDemo { //@PathVariable能够用来映射URL中的占位符到目标方法的参数中 @RequestMapping("/book/{bookId}") public String test4(@PathVariable("bookId") Integer bookId) { System.out.println("bookId:" + bookId); return "index"; } }
控制台输出:bookId:1
@PathVariable 映射 URL 绑定的占位符
经过 @PathVariable 能够将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符能够经过@PathVariable(“xxx“) 绑定到操做方法的入参中。
客户端界面(表单):
<form action="/queryBook" method="post"> <input type="text" name="bookId"> <input type="text" name="author"> <input type="submit" value="提交"> </form>
controller层:
@Controller public class ParamPassDemo { @RequestMapping("/queryBook") public String test5(HttpServletRequest request) { System.out.println("bookId:" + request.getParameter("bookId")); //此处index.jsp界面在WEB-INF下 return "redirect:/index.jsp"; } }
客户端输入:123
控制台输出:用户id:123
客户端界面(url地址栏):
http://localhost:8080/test6?bookId=321
controller层:
@Controller public class ParamPassDemo { @RequestMapping("/test6") public String test6(String bookId){ System.out.println("bookId="+bookId); //使用服务端跳转的方式转向到另外一个controller //return "forward:queryBook?bookId="+bookId; return "redirect:queryUser?bookId="+bookId; } }
控制台输出:bookId=321 bookId:321