SpringMVC Parameter Binding#
- [Application] Able to use SpringMVC to obtain basic type parameters
- [Application] Able to use SpringMVC to obtain POJO type parameters
1. Default supported parameter types#
Add the following types of parameters to the handler method parameters, and the handler adapter will automatically recognize and assign values.
HttpServletRequest
Get request information through the request object.
HttpServletResponse
Handle response information through the response.
HttpSession
Get the object stored in the session through the session object.
Model/ModelMap
Model
The Model object can pass data to the page. In essence, it uses the Request object to pass data to JSP.
ModelMap
ModelMap is an implementation class of the Model interface, and it can also pass data to the page through ModelMap.
Using Model and ModelMap has the same effect. If Model is used directly, SpringMVC will instantiate ModelMap.
The code is as follows:
@RequestMapping("/quick1")
// Bind default parameters and inject Servlet related APIs
public void quick1(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
// Put data into the request scope
request.setAttribute("msg","Hello everyone");
// Forward
request.getRequestDispatcher("/WEB‐INF/jsp/success.jsp").forward(request, response);
}
@RequestMapping("/quick2")
// Inject native objects through parameters
public String quick2(Model model){
// Put data into the model, equivalent to putting data into the request scope
model.addAttribute("msg","Study hard");
return "success";
}
2. Binding simple types#
When the parameter name in the request matches the parameter name in the handler method, the request parameter will be bound to the parameter. This simplifies the method of getting parameters from the Request.
// Receive simple data
@RequestMapping("/quick3")
// Define a parameter name that is consistent with the key of the submitted data
public String quick3(int id ,String name){
System.out.println(id);
System.out.println(name);
return "success";
}
2.1. @RequestParam#
- @RequestParam is commonly used for binding simple types.
- value: The parameter name, which is the name of the request parameter in the input parameter. For example, value="itemId" means that the value of the parameter with the name itemId in the request will be passed in.
- required: Whether it is required. The default is true, which means that the request must have the corresponding parameter, otherwise an error will occur.
- HTTP Status 400 - Required Integer parameter 'XXXX' is not present
- defaultValue: The default value, which means the default value when there is no parameter with the same name in the request.
3. Binding POJO types#
3.1. Using POJO to receive form data
If there are many parameters to be submitted, or if there is a lot of content in the submitted form, you can use simple types or POJO to receive the data. The requirement is that the attribute names in the POJO object are consistent with the name attributes of the input in the form.
// Receive POJO type data (Simple Java Object)
@RequestMapping("/quick4")
// Define a parameter name that is consistent with the key of the submitted data
public String quick4(User user){
System.out.println(user);
return "success";
}
3.2. Solving the problem of post garbled characters#
When making a post request, the data may be garbled. We can set a filter to filter the encoding. Add the following code to web.xml.
<!-- Solve the problem of post garbled characters -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- Set the encoding to UTF8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4. Controller method return value#
4.1. Return as a string#
Directly return a string: This method will concatenate the returned string with the prefix and suffix of the view resolver and then redirect.
Redirect:
@RequestMapping("/quick5")
public String quick5(){
System.out.println("quick5");
return "redirect:/index.jsp";
}
Forward:
// Forward
@RequestMapping("/quick6")
public String quick6(){
System.out.println("quick5");
return "forward:/index.jsp";
}