1, HttpServletRequest
@RequestMapping("/...") public ModelAndView doSome(HttpservletRequest request,HttpServletResponse response,HttpSession session) { String name = request.getParameter("name");//Received parameters }
2, HttpServletResponse
3, HttpSession
4, User submitted data
Receive parameters submitted by users:
1. Receive one by one
First:
Requirement: the formal parameter name of the processor method must be consistent with the parameter name in the request. A request parameter with the same name is assigned to a formal parameter with the same name
For example, when submitting data with a front-end form
<form action="dosome.do" method="post"> <input type="text" name="name"> <input type="text" name="age"> <input type="submit" value="Submit"> </form>
@Controller class MyController{ @RequestMapping("/dosome.do") public ModelAndView doSome(String name,int age) { //You can use parameters directly //For example: ModelAndView mv = new ModelAndView(); mv.addObject("myname",name); mv.addObject("myage",age); ...... ....... } }
Principle of frame processing parameters:
① Use the request object to receive request parameters
String strName = request.getParameter("name"); String strAge = request.getParameter("age");
② The spring MVC framework calls the dosome() method in MyController through the DispatcherServlet. When calling the method, the received parameters are assigned to the formal parameters according to the name
dosome(strName,Integer.valueOf(strAge));
The framework will provide the function of type conversion, and can convert String to int, long, float, double and other types. Therefore, you can use the value in the parameter directly in the method
Note: if the value received by int age in the above case is null, a 400 error will appear on the page
Solution: use its wrapper class Integer. Using wrapper class named variables in parameters can avoid the error of empty string conversion
@Controller class MyController{ @RequestMapping("/dosome.do") public ModelAndView doSome(String name,Integer age) { //You can use parameters directly //For example: ModelAndView mv = new ModelAndView(); mv.addObject("myname",name); mv.addObject("myage",age); ...... ....... } }
Second:
Use the @ RequestParam annotation to solve the problem that the parameter name in the request is different from the formal parameter name
@Properties of RequestParam: 1 Parameter name in value request
2.required is a boolean. The default value is true. True means that the request must contain this parameter
Location: in front of the processor method parameter definition
For example, there is now a form that submits data
<form action="dosome.do" method="post"> <input type="text" name="rname"> <input type="text" name="rage"> <input type="submit" value="Submit"> </form>
@Controller class MyController{ @RequestMapping("/dosome.do") public ModelAndView doSome(@RequestParam(value = "rname") String name,@RequestParam(value = "rage") Integer age) { ModelAndView mv = new ModelAndView(); mv.addObject("myname",name); mv.addObject("myage",age); ...... ....... } }
2. Object reception
Define the parameters of the processor method as an object, as long as the request parameter name and the attribute of the object have the same name.
Create a common class to save the request parameter value, and the property name is the same as the parameter name in the request
public class Student{ private String name; private int age; get... set... }
@Controller class MyController{ @RequestMapping("/dosome.do") public ModelAndView doSome(Student student) { ModelAndView mv = new ModelAndView(); mv.addObject("myname",student.getName()); mv.addObject("myage",student.getAge()); mv.addObject("mystudent",student); ...... ....... } }
The framework will create java objects of formal parameters and assign values to attributes. For example, if the parameter in the request is name, the framework will call its setName() method