Spring MVC Control layer return type--string type and bean type

Source: Internet
Author: User

The return types of the SPRINGMVC control layer are available in a variety of forms, taking two of these--string types and bean types as a description.

First, the structure of the test project

Description: (JSP name is not good)

Control layer: Usercontroller.java-related operation to the user;

View layer: toformtest.jsp--jump to formtest.jsp

(The reason for this jump: 1. Because the JSP folder is in the Web-inf directory, so it cannot be accessed directly; 2. The form in the destination address formtest.jsp uses the SF tag to initialize the Bean object that the form is bound to in the control layer, so it is necessary to first enter the control layer (the bean to which the target address form is bound). And then jump to the destination address. )

formtest.jsp--Adding a user's JSP

formtesttarget.jsp--user information added after adding user

showusertest.jsp--display details for the specified user

Second, the return type of control layer--usercontroller.java

A. Return value type is string type

1. The control layer jumps to the view layer

Note: Click on the request in Toformtest.jsp, execute the Formtest method in Usercontroller.java, jump to formtest.jsp.

toformtest.jsp

<href= "user/formtest">formtest</a> 

Usercontroller.java

// jump to http: // localhost:8081/spring_user/user/formtest.jsp    @RequestMapping (value= "/formtest", method=requestmethod.get)    public  String formtest ( Model model) {        model.addattribute (new  User ());         return "User/formtest";    }

Summary: It should be explained that, after jumping formtest.jsp, the address bar display address is a logical path, as follows:

Http://localhost:8081/spring_user/user/formTest

2. Jump between control layer methods

Description: After adding a user to the formtest.jsp, the Formtest method submitted to Usercontroller.java jumps to formtesttarget.jsp to display the added user information. Note: The Formtest method sets its return address as: return "User/formtesttarget"; to accomplish this. But now to illustrate how to jump between the methods of the control layer, comment out. In addition, the "Action" message (request address) is not filled in form form, but Why did you jump to Usercontroller.java's Formtest method? This is because the address shown in the Formtest.jsp address bar is: http://localhost:8081/spring_user/user/formTest, The submitted address is naturally the Formtest method of the Usercontroller.java.

formtest.jsp

<Sf:formMethod= "POST"Modelattribute= "User"enctype= "Multipart/form-data">Username:<Sf:inputPath= "username"/> <sf:errorsPath= "username" /><BR>Password:<Sf:passwordPath= "Password"/> <sf:errorsPath= "Password"/><BR>          <inputtype= "Submit"value= "Add User"/><BR></Sf:form> 

Usercontroller.java

    //jump to Toformtesttarget method@RequestMapping (value= "/formtest", method=requestmethod.post) PublicString Formtest (model model, @Validated user user, Bindingresult br) {//If there is an error returning        if(Br.haserrors ()) {return"User/formtest"; } model.addattribute (user);//return "User/formtesttarget";//Jump to formtesttarget.jsp address in page Address bar:http://localhost: 8081/spring_user/user/formtest--can see this jump as request forwarding//return "Redirect:/user/formtesttarget";//REDIRECT to formtesttarget.jsp--invalid [post-commit Invalid path:http://localhost: 8081/spring_user/user/formtesttarget]        return"Redirect:/user/toformtesttarget";//Redirect to method toformtesttarget--can jump to formtesttarget.jsp, but the data in model cannot be taken to formtesttarget.jsp.     }

//jump to http://localhost:8081/spring_user/user/formtesttarget.jsp@RequestMapping (value= "/toformtesttarget") PublicString toformtest () {return"User/formtesttarget"; }

Summary: The return type in the Formtest method is still of type string, but the destination address is no longer a jump to the view layer, but instead jumps from the Formtest method to the Toformtest method, which is done using the Redrect keyword.

Note that you cannot redirect from the control layer to the view layer in a redirected manner.

B. Return value type is bean type

Note: Click "Showuser" on the formtesttarget.jsp page, request the Showusertest method in Usercontroller.java, and then jump to showusertest.jsp to show the user's details.

formtesttarget.jsp

Username:${user.username}<br>password:${user.password}< BR > <  href= "showusertest">showuser</a> 

Usercontroller.java

// jump to http: // localhost:8081/spring_user/user/showusertest.jsp    @RequestMapping (value= "/showusertest", method=requestmethod.get)    public  User Showusertest (model model) {        Model.addattribute ("msg", "Return value is User type");         return Userservice.findbyid (in);    }

Summary: The return type of the Showusertest method is a user type and does not indicate an address, at which point the SRINGMVC will return the "physical path mapped by the requested logical path" by default : The request address in formtesttarget.jsp is showusertest in the user directory, At this point it is a method in Usercontroller.java, but after executing the method, Spring returns the showusertest.jsp in the user directory by default because there is no return type of user.

Spring MVC Control layer return type--string type and bean type

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.