1. Read the request parameter value
1. Through the request object
Note: The request object can be used as the input parameter of the method
@RequestMapping("/login1.do") public String login1(HttpServletRequest request) { System.out.println("login1()"); String adminCode=request.getParameter("adminCode"); String pwd = request.getParameter("pwd"); System.out.println(adminCode+":"+pwd); return "index"; }
2. Use an annotation @RequestParam annotation
Note: Add this annotation to the front of the formal parameters of the method
@RequestMapping("/login2.do") //The second way to read the request parameter value //Annotate with @RequestParam public String login2(String adminCode,@RequestParam("pwd")String password) { System.out.println("login2()"); System.out.println("adminCode:"+adminCode+"pwd:"+password); return "index"; }
3. Use javabean to encapsulate request parameter values
step1: Write a Java class that requires corresponding get/set methods
step2: use the javabean as a formal parameter of the method
@RequestMapping("/login3.do") //The third way to read the request parameter value // encapsulate into a javabean public String login3(AdminParam ap) { System.out.println("login3()"); System.out.println(ap.getAdminCode()+" "+ap.getPwd()); return "index"; }
2. Passing values to the page
1. Use request
Bind the data to the request and forward it to a jsp.
Note: springmvc uses forwarding by default
@RequestMapping("/login4.do") //The first way to pass values to the page // use request public String login4(AdminParam ap,HttpServletRequest request) { System.out.println("login5()"); String adminCode=ap.getAdminCode(); System.out.println(adminCode); //bind the data to the request request.setAttribute("adminCode", adminCode); //springmvc uses forwarding by default return "index"; }
2. Use ModelAndView
Encapsulate the data into the ModelAndView object first, and then use the object as the return value of the method
@RequestMapping("/login5.do") //The second way to pass values to the page // use ModelAndView public ModelAndView login5(AdminParam ap) { System.out.println("login5()"); String adminCode = ap.getAdminCode(); System.out.println(adminCode); Map<String, Object> data=new HashMap<String, Object>(); //equivalent to request.setAttribute("adminCode",adminCode); data.put("adminCode", adminCode); //Construct ModelAndView object ModelAndView modelAndView = new ModelAndView("index",data); return modelAndView; }
3. Use ModelMap
Take that object as a parameter to the method and then bind data to that object
@RequestMapping("/login6.do") //The third way to pass values to the page public String login6(AdminParam ap,ModelMap mm) { System.out.println("login6()"); String adminCode = ap.getAdminCode(); System.out.println(adminCode); mm.addAttribute("adminCode", adminCode); return "index"; }
4. Use session
@RequestMapping("/login7.do") //The fourth way to pass values to the page // use session public String login7(AdminParam ap,HttpSession session) { System.out.println("login7()"); String adminCode = ap.getAdminCode(); System.out.println(adminCode); session.setAttribute("adminCode", adminCode); return "index"; }
3. Redirect
1. If the return value of the method is String;
Add " redirect :" before the redirect address
@RequestMapping("/login8.do") public String login8() { System.out.println("login8()"); return "redirect:toIndex.do"; }
2. If the return value of the method is ModelAndView
@RequestMapping("/login9.do") public ModelAndView login9() { System.out.println("login9()"); RedirectView rv=new RedirectView("toIndex.do"); return new ModelAndView(rv); }