One of the SPRINGMVC (page <---> Controller mutual value, forwarding and redirection)

Source: Internet
Author: User

#页面---> Controllers
1.request: Not recommended
2. Use attribute pass value (recommended) @RequestParam ("name") String username
3. Using Bean object to pass value (suitable for large data volume)


#控制---> Page request,session (cookies), application
1.request,session passing data to the page
2.modelandview:model--->modelmap--->map
3.MODELMAP (recommended)

#重定向和转发
1. Principle of Redirection: Response.sendredirect ("Showdemo.do")
2. The principle of forwarding:
Request.getrequestdispatcher ("/web-inf/web/ok.jsp")
3. When the view resolver resolves to forward or redirect two prefixes, execute the above code, otherwise implement string concatenation



Development steps:

1. New MAVEN Project
1) Add Web. xml
2) Add Tomcat Runtime Environment
3) Add dependent jar SPRING-WEBMVC
4) Add Spring-mvc.xml
5) Configuring the front-end controller in Web. xml
6) Spring-mvc.xml Configuration View Resolver

2. Define DEMO.JSP page

<formAction=""Method= "Get">Name:<inputtype= "text"name= "Name"/><BR>Age:<inputtype= "text"name= "Age"/><BR>     <inputtype= "Submit"value= "Submit"/></form>



3. Define the controller Democontroller

@Controller @requestmapping ("/demo")  Public class democontroller{      // display demo.jsp     @RequestMapping ("/showdemo.do")         Public String Showdemo () {             return ' demo ';          }}



4. Page---> Controller transmit value

2. Using attribute transfer values
Features: 1) The name of the parameter list is the same as the value of the form's Name property
2) automatic type conversion can be implemented, but there may be exceptions
3) Small amount of form data

@RequestMapping ("/test2.do")      public  String test2 (String Name,integer age) {                   SYSTEM.OUT.PRINTLN (name+ "," + Age);                         return "OK";     }



3. Attribute passing value (the Form Name property value differs from the parameter list variable name)
Using @requestparam annotations to assist in the assignment of variables
The name in the @RequestParam ("name") does not exist on the page

   @RequestMapping ("/test3.do")                         public  String test3 (                 @RequestParam ("name") String username,                       Integer age) {                       System.out.println (username+ "," + Age ")                             ; return "OK";          }



4.Bean Object Passing Value
Features: Encapsulates the form name attribute value into a class type;
Member variable name must be the same as the Name property value of the form
Suitable for parameter passing values with large data volumes

@RequestMapping ("/test4.do")           public  String test4 (user user) {                System.out.println ( User.getname ()+ "," +User.getage ());                          return "OK";                 }


5. Controller---> Page pass value
5. Use Request,session to transfer values from the controller to the page
//

   @RequestMapping ("test5.do")             public  String test5 (httpservletrequest request,                                           HttpSession session) {                 Request.setattribute ("name", "admin");                 Session.setattribute ("Age", "the");                  return "OK";                 



6.ModelAndView Transmit Value
Features: Frame Map property gets to, sets the request object
@RequestMapping ("/test6.do")

    Public Modelandview Test6 () {                   Map<String,Object> map=new hashmap<string,object>();                        Map.put ("Address", "mid-ding 7-storey");                        Modelandview mv=new modelandview ("OK", map);                         return mv;               }


               
6. Request forwarding and redirection
           / /requirement: Controller receives name, if Name=admin, indicates successful login
           //    Response page ok.jsp (forward)
           //    If name!=admin indicates a login failure.
           //    Response page demo.jsp (redirect)
          &NBSP ;//   8.1 The principle of redirection:
           //   response.sendredirect (" Showdemo.do ");
             //      forward principle:
          & nbsp  //      Request.getrequestdispatcher ("/web-inf/web/ok.jsp"). ForWord     (Resquest, Response);
                 //        
    &N Bsp          

  @RequestMapping ("/test8.do) public                        String test8 (String Name,modelmap map) {                           if( Name.equals ("admin")) {                                      Map.addattribute ("Success", "Login successful!") );                                      }                             Else {                                 return "redirect:showDemo.do";                                            }                               }



One of the SPRINGMVC (page <---> Controller mutual value, forwarding and redirection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.