Get request parameters 1. What are request parameters?

<a href="emp/remove? EmpId =3"> Delete </a> <form action="emp/save" method="post"> <input type="text" name="empAge"/><br/> <input type="text" name="empSalary"/><br/> < form>Copy the code

delete < / a >

Use the @requestParam annotation on the parameters of the Handler method.

@RequestMapping("/caseOne") public String caseOne(@RequestParam("empId") Integer empId) { System. The out. Println (" empId = "+ empId); return "result"; }Copy the code

SpringMVC will automatically do the conversion for us. The @requestParam annotation can be omitted if the name of the request parameter matches the name of the corresponding parameter in the handler method.

@RequestMapping("/caseOne")
public String caseOne(Integer empId) {
    System.out.println("empId="+empId);
    return "result";
}
Copy the code

② One more value

<form action="team" method="post"> please select your favorite team: <input type="checkbox" name="team" value="Brazil"/> Brazil <input type="checkbox" name="team" value="German"/> Germany <input Type ="checkbox" name="team" value="French"/> French <input type="checkbox" name="team" value="Holland"/> Holland <input Type ="checkbox" name="team" value="Italian"/> Italian <input type="checkbox" name="team" value="China"/> China <br/> <input Type ="submit" value=" save "</form>Copy the code

Use a List or array to receive.

@RequestMapping("/caseTwo") public String caseTwo(@RequestParam("team") List<String> teams) { System.out.println(teams);  return "result"; } @RequestMapping("/caseTwo") public String caseTwo(@RequestParam("team") String[] teams) { System.out.println(Arrays.asList(teams)); return "result"; }Copy the code

③ The form data corresponds to an entity class

<form action="emp/save" method="post"> <input type="text" name="empAge"/><br/> <input type="text" name="empSalary"/><br/> < form>Copy the code

Entity class:

public class Employee { private Integer empId; private String empName; private int empAge; private double empSalary; ...Copy the code

Receive directly using the entity class type corresponding to the form

@RequestMapping("/caseThree")
public String caseThree(Employee employee) {
    System.out.println(employee);
    return "result";
}
Copy the code

④ The entity class corresponding to the form contains cascading attributes

public class Student { private Integer studentId; private String studentName; private School school; private List<Subject> subjectList; private Subject[] subjectArray; private Set<Teacher> teacherSet; private Map<String,String> scoreMap; TeacherSet = new HashSet<>(); teacherSet = new HashSet<>(); teacherSet.add(new Teacher()); teacherSet.add(new Teacher()); teacherSet.add(new Teacher()); }Copy the code

Handler method

@RequestMapping("/get/param/multi/value") public String getParamOneNameMultiValue(@RequestParam("team") List<String> teamList) { for (String team : teamList) { System.out.println(team); } return "target"; } @RequestMapping("/get/param/entity") public String getParamEntity(Employee employee) { System.out.println(employee); return "target"; } //@RequestMapping("/get/param/entity") public String getParamEntityParam(@RequestParam("empName") String empName) { System.out.println("empName="+empName); return "target"; } @RequestMapping("/get/param/fuza") public String getParamFuza(Student student) { System.out.println("StudentId="+student.getStudentId()); System.out.println("StudentName="+student.getStudentName()); System.out.println("SchoolId="+student.getSchool().getSchoolId()); System.out.println("SchoolName="+student.getSchool().getSchoolName()); List<Subject> subjectList = student.getSubjectList(); For (Subject Subject: subjectList) {system.out.println (" Subject name ="+subject.getSubjectName()); } Subject[] subjectArray = student.getSubjectArray(); For (Subject Subject: subjectArray) {system.out.println (" Subject name ="+ Subject.getSubjectName ()); } Set<Teacher> teacherSet = student.getTeacherSet(); For (Teacher Teacher: teacherSet) {system.out.println (" teachername ="+teacher.getTeacherName()); } Map<String, String> scoreMap = student.getScoreMap(); Set<String> keySet = scoreMap.keySet(); for (String key : keySet) { String value = scoreMap.get(key); System.out.println(key+":"+value); } return "target"; }Copy the code

A form to submit data

< form action = "${pageContext. Request. ContextPath} / get/param/fuza" method = "post" > student no. : <input type="text" name="studentId" value="22" /><br/> studentName =" Tom "/><br/> <! <input type="text" name="school.schoolId" value="33" /><br/> school name: <input type="text" name="school.schoolName" value="at" /> <input type="text" name="subjectList[0]. SubjectName "value=" Theory" /><br/> <input type="text" name="subjectList[1]. SubjectName "value=" "/><br/> <input type="text" name="subjectList[2]. SubjectName "value=" road test "/><br/> <input type="text" name="subjectArray[0]. SubjectName "value=" subjectArray" <input type="text" name="subjectArray[1]. SubjectName "Value =" /><br/> <input type="text" name="subjectArray[2]. SubjectName "Value =" /><br/> <input type="text" name="teacherSet[0]. TeacherName "value=" kakaci "/><br/> <input type="text" name="teacherSet[1]. TeacherName "value=" Iruka "/><br/> <input type="text" name="teacherSet[2]. TeacherName "value=" teacherSet "/> <input type="text" name="scoreMap['shuxue']" value="25" /><br/> <input type="text" name="scoreMap['yuwen']" value="16" /><br/> <input type="text" name="scoreMap['yingyu']" value="7" /><br/> <input type="text" name="scoreMap['lol']" value="100" /> < input type = "text" name = "scoreMap [' dota]" value = "300" / > < br / > < input type = "submit" value = "submit" / > < / form > < br / > < br / > <form action="${pageContext.request.contextPath }/get/param/entity" method="post"> <! -- private Integer empId; <input type="text" name="empId"/><br/> <! -- private String empName; - > name: < input type = "text" name = "empName" / > < br / > <! -- private Integer empAge; <input type="text" name="empAge"/><br/> <! -- private Double empSalary; -- > pay: < input type = "text" name = "empSalary" / > < br / > < input type = "submit" value = "submit" / > < / form > < br / > < br / > < form Action = "${pageContext. Request. ContextPath} / get/param/multi/value" method = "post" > please choose your favorite team: <br/> <input type="checkbox" name="team" value="German" Value ="Brazil"/> Brazil <br/> <input type="checkbox" name="team" value="Italian"/> <br/> <input type="checkbox" name="team" value=" submit" /> <br/> <input type="submit" value=" submit" /> </form>Copy the code

Web.xml configuration to resolve character garbled characters

<! <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>Copy the code

Page hopping control 1. Forwarding commands

@RequestMapping("/testForward")
public String testForward() {
    return "forward:/target.jsp";
}
Copy the code

Forward :/target.jsp indicates that the method is forwarded to /target.jsp when it completes execution. Is forward redundant when a view parser concatenates a string to specify a forward page?

2. Redirect command

@RequestMapping("/testRedirect")
public String testRedirect() {
    return "redirect:/target.jsp";
}
Copy the code

Redirect :/target. JSP indicates that the method is redirected to /target.jsp when it completes execution. The path used in this redirection is different from the way we wrote it before and does not start with the name of the Web application because SpringMVC will do it for us.

3. Use native objects to complete forwarding

@RequestMapping("/testForward")
public void testForward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/target.jsp").forward(request, response);
}
Copy the code

As you can see, after forwarding using the native Request object, the return value of the handler method must be void, which means we specify the response and don’t need SpringMVC to handle it. A request can only have one response, not one in the handler method and then another in the SpringMVC framework.

4. Use native objects to complete redirection

@RequestMapping("/testRedirect")
public void testRedirect2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.sendRedirect(request.getContextPath()+"/target.jsp");
}
Copy the code

After a redirect using the native Response object, the return value of the handler method also needs to be set to void for the same reason.