SPRINGMVC redirects redirect and forwards forward in the same controller and different controllers:

Source: Internet
Author: User

    • REDIRECT Forward
    • redirect between the same controller: redirect

1. Our request to redirect between the same controller the specific code is as follows:

2. Example:

Front Code:

<formaction= "User/update.do" method= "POST" >

Id:<input type= "text" name= "id" >

Name:<input type= "text" name= "name" >

Sex:<input type= "text" name= "Sex" >

<inputtype= "Submit" value= "Modify" >

</form>

Background code:

@RequestMapping (value= "/update.do")

Public stringupdate (user user) {

map<string,object>map= new hashmap<string,object> ();

Map.put ("id", User.getid ());

Map.put ("Name", User.getname ());

Map.put ("Sex", user.getsex ());

Userservice.updateuser (map);

The key to redirection here is, redirect: this keyword, which is followed by the request path you want to redirect

Also, if you are in the same controller, you do not have to use "/" from the root directory, and if you are in a different controller, you must start from the root directory.

return "Redirect:select.do";

}

Here's a test of the results of using more catalogs * * *

/**

* Query User Information

*/

@RequestMapping (value= "/select.do")

Public Stringselect (Integer id,map<string,object> Map) {

list<map<string,object>>list= new arraylist<map<string,object>> ();

List=userservice.selectuser (ID);

Map.put ("MyList", "You Value");

Return "index";

}

    • redirect Between different controllers

1, at the time of redirection between different controllers, the main point is that to develop a specific controller namespace is requestmapping (value= "Redirectval"), but also to specify the root directory, If it is not the root directory will also be in the same controller in the search, so that the error can not be found, or incorrect redirection.

2. Example:

Front Code:

<formaction= "User/update.do" method= "POST" >

Id:<input type= "text" name= "id" >

Name:<input type= "text" name= "name" >

Sex:<input type= "text" name= "Sex" >

<inputtype= "Submit" value= "Modify" >

</form>

Background code: These two background code is the code of different controller layers

@RequestMapping (value= "/update.do")

Public stringupdate (user user) {

map<string,object>map= new hashmap<string,object> ();

Map.put ("id", User.getid ());

Map.put ("Name", User.getname ());

Map.put ("Sex", user.getsex ());

Userservice.updateuser (map);

The key to redirection here is, redirect: this keyword, which is followed by the request path you want to redirect

Note that "/" should be added here to start with the root directory,

return "Redirect:/user/select.do";

}

Here to test the results of using the more catalog * * *

/**

* Query User Information

*/

@RequestMapping (value= "/user")

Publicclass user{

@RequestMapping (value= "/select.do")

Public Stringselect (Integer id,map<string,object> Map) {

list<map<string,object>>list= new arraylist<map<string,object>> ();

List=userservice.selectuser (ID);

Map.put ("MyList", "You Value");

Return "index";

}

}

    • To forward between the same controller: forward:
    • No specific code is posted here because it is the same as the redirect above.

    • To forward between different controllers: forward:
    • This is the same as long as the namespace and the root directory is correct, no problem;

Here's the redirect and forwarding process: when we send a request to find a specific method by processing the mapper, then return a value that is the name of the view, and when Viewresolve receives this view name to determine if his prefix is not, redirect/ Forward will create a

Redirectview view, and analyzed the rendering source of the view, and analyzed the path problem in redirection, parameter problem, PATH variable problem and other common problems.

Parameters to be carried when redirecting and forwarding *******

1, their own manual splicing, so in return "redirect:select.do?id=1&name=" +good+ ""; This allows the data to be obtained in the redirected method, the same way the receipt is received from the front desk.

Specific examples:

@RequestMapping (value= "/update.do")

Public stringupdate (user user) {

map<string,object>map= new hashmap<string,object> ();

Map.put ("id", User.getid ());

Map.put ("Name", User.getname ());

Map.put ("Sex", user.getsex ());

Userservice.updateuser (map);

Forwarding is used here, as is forwarding and redirection in the writing format.

return "forward:select.do?code=200&status=ok!";

}

@RequestMapping (value= "/select.do")

Public Stringselect (Integer id,string code,string status,map<string,object> map,modelmodel) {

String Codes=code;

String Success=status;

SYSTEM.OUT.PRINTLN (codes);

SYSTEM.OUT.PRINTLN (Success);

System.out.println (Model.tostring ());

list<map<string,object>>list= new arraylist<map<string,object>> ();

List=userservice.selectuser (ID);

Map.put ("MyList", "You Value");

Return "index";

}

Execution Result:

2, using Redirectattributes This object, through this object can pass data, the object of this class has two methods can pass parameters:

(1), the use of AddAttribute (Key,value) This method of parameter transfer, this way of transmission and the first kind will be in the request of the road to show the strength,

(2), the use of Addflashattribute (Key,value) This method of transmission, and this way is to put this value in the session, so the parameters will not be behind the URL, if refresh the page value is gone!

Use AddAttribute to pass a value * * *

Specific example: using redirect to transmit values

@RequestMapping (value= "/update.do")

Public Stringupdate (User user,redirectattributes attr) {

Attr.addattribute ("code", 200);

Attr.addattribute ("Status", "Success");

map<string,object>map= new hashmap<string,object> ();

Map.put ("id", User.getid ());

Map.put ("Name", User.getname ());

Map.put ("Sex", user.getsex ());

Userservice.updateuser (map);

return "Redirect:select.do";

}

@RequestMapping (value= "/select.do")

Public Stringselect (Integer id,string code,string status,map<string,object> map,modelmodel) {

String Codes=code;

String Success=status;

SYSTEM.OUT.PRINTLN (codes);

SYSTEM.OUT.PRINTLN (Success);

System.out.println (Model.tostring ());

list<map<string,object>>list= new arraylist<map<string,object>> ();

List=userservice.selectuser (ID);

Map.put ("MyList", "You Value");

Return "index";

}

Results:

Use Addflashattribute to pass a value ******

@RequestMapping (value= "/update.do")

Public stringupdate (User user,redirectattributes arr) {

Arr.addflashattribute ("Code", "200");

Arr.addflashattribute ("Status", "ok!");

map<string,object>map= new hashmap<string,object> ();

Map.put ("id", User.getid ());

Map.put ("Name", User.getname ());

Map.put ("Sex", user.getsex ());

Userservice.updateuser (map);

return "Redirect:select.do";

}

/**

* Query User Information

*/

@RequestMapping (value= "/select.do")

Public Stringselect (String code,string status,map<string,object>map,map<string,object> maps) {

integerid=1;

From the execution result we see here code is NULL

String Codes=code;

From the execution result we see here the status is null

String Success=status;

SYSTEM.OUT.PRINTLN (codes);

SYSTEM.OUT.PRINTLN (Success);

We've added a parameter to the method maps here to get the parameters

Stringmcode= (String) maps.get ("code");

Stringmstatus= (String) maps.get ("status");

From the output we can see that there are values here, using model also can get the value, just don't know how to get so use map

System.out.println (Mcode);

System.out.println (Mstatus);

System.out.println (Maps.tostring ());

list<map<string,object>>list= new arraylist<map<string,object>> ();

List=userservice.selectuser (ID);

Map.put ("MyList", "You Value");

Return "index";

}

Execution Result:

Use: Forward forwarding to get a value that is not

@RequestMapping (value= "/update.do")

Public Stringupdate (User user,redirectattributes attr) {

Attr.addattribute ("code", 200);

Attr.addattribute ("Status", "Success");

map<string,object>map= new hashmap<string,object> ();

Map.put ("id", User.getid ());

Map.put ("Name", User.getname ());

Map.put ("Sex", user.getsex ());

Userservice.updateuser (map);

return "Forward:select.do";

}

@RequestMapping (value= "/select.do")

Public Stringselect (Integer id,string code,string status,map<string,object> map,modelmodel) {

String Codes=code;

String Success=status;

SYSTEM.OUT.PRINTLN (codes);

SYSTEM.OUT.PRINTLN (Success);

System.out.println (Model.tostring ());

list<map<string,object>>list= new arraylist<map<string,object>> ();

List=userservice.selectuser (ID);

Map.put ("MyList", "You Value");

Return "index";

}

Results:

Here Forwar gets not the value associated with his storage method, because he is present in the session, there is no value in a forward, so it is null

SPRINGMVC redirects redirect and forwards forward in the same controller and different controllers:

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.