1, Data in section
1. RequestMapping annotation:
@RequestMapping(value = "Requested URL",params = {"Restrict incoming parameters"},method="Qualify method type",headers="Qualified request header")
2. Path data section:
<a href="method2/100">Test request header</a><br><br> @RequestMapping("{m}/{pid}") public String method1(@PathVariable("pid") String id, @PathVariable("m") String method) { System.out.println(method + "/" + id); return "resp"; }
A placeholder can take up a layer of paths. All paths can be occupied with placeholders, but if there are multiple methods matching the URL, an error will be reported (the spring MVC framework will try to avoid this situation and keep cool when there is no way to avoid it).
The @ PathVariable annotation can be used to get the real path on the placeholder. value can be omitted. If omitted, the following variable name must be the same as the occupied variable name. When it cannot be found, an error will be reported unless required is set to false.
Operation result:
3. Carry parameters:
<a href="method1/100?username=abc">Test request parameters</a><br><br> @RequestMapping("{m}/{pid}") public String method1(@PathVariable("pid") String id, @PathVariable("m") String method, @RequestParam(value = "user", required = false) String username) { System.out.println(method + "/" + id + "?username=" + username); return "resp"; }
When carrying the request parameter in get mode, use @ RequestParam to get the parameter value passed in. Value can be omitted. If omitted, the following variable name must be the same as the parameter name passed in. When it cannot be found, an error will be reported unless required is set to false.
4. Native API passed in to Servlet:
<a href="method2/100?username=abc">Test request header</a><br><br> @RequestMapping("method2/{pid}") public String method2(@PathVariable("pid") String id, @PathVariable(value = "m", required = false) String method, @RequestHeader("user-agent") String heads, HttpServletRequest request) { System.out.println("method2/" + id + "?user-agent=" + heads + "&username=" + request.getParameter("username")); return "resp"; }
Operation result:
5. Automatic encapsulation of POJO (characterencodingfilter needs to be configured):
For customized objects, spring MVC will map one-to-one according to the name of the form and the variable name in the entity class, and automatically encapsulate when all are found.
<form action="order" method="post"> //Item name: <input type="text" name="productName"><br> //Quantity of articles: <input type="text" name="productQuantity"><br> //Total price of goods: <input type="text" name="totalPrice"><br> //User name: <input type="text" name="address.userName"><br> //Customer receiving address: <input type="text" name="address.location"><br> //User mobile phone: <input type="text" name="address.mobile"><br> <input type="submit" value="Submission"> </form> @RequestMapping("order") public String method3(Order order) { //It also supports cascaded auto encapsulation System.out.println(order); return "resp"; }
Operation result:
2, Data output part
Map,Model,ModelMap:
<a href="method1">test Map output data</a><br><br> <a href="method2">test Model output data</a><br><br> <a href="method3">test ModelMap output data</a><br><br> @Controller public class OutController { @RequestMapping("method1") public String method1(Map<String,Object> map) { map.put("param","This is method one"); return "showparam"; } @RequestMapping("method2") public String method2(Model model) { model.addAttribute("param","This is method two"); return "showparam"; } @RequestMapping("method3") public String method3(ModelMap modelmap) { modelmap.addAttribute("param","This is method three"); return "showparam"; } } pageContext: ${pageScope.param}<br><br> request: ${requestScope.param}<br><br> session: ${sessionScope.param}<br><br> application: ${applicationScope.param}<br><br>
Operation result:
When Map, Model and ModelMap transmit output data, they will always become the implicit Model - BindingAwareModelMap.