Spring MVC Framework controller jumps, redirects, parameters

Source: Internet
Author: User

I. Background of demand

1. Requirements: Spring MVC Framework controller jumps between, need to redirect. There are several situations: without parameter jump, with the parameter splicing URL form jump, with parameters do not join parameters jump, the page can also be displayed.
@RequestMapping (value = "/activitytype", method =requestmethod.get) PublicString Activitytype (httpservletrequest request, Modelmap model,redirectattributes attr)throwsexception{Logger.info ("Into Activitytype"); String ID= Request.getparameter ("TypeId"); String ActivityID= Request.getparameter ("ActivityID");//model.put ("typeId", id);//model.put ("ActivityID", ActivityID); //Attr.addflashattribute ("typeId", id);//Attr.addflashattribute ("ActivityID", ActivityID);Attr.addattribute ("TypeId", id); Attr.addattribute ("ActivityID", ActivityID); Activitytypeinfo Activitytypeinfo= Activitytypeinfobiz.get (integer.valueof (id));//querying activity type objects based on activity IDInteger activitytype=Activitytypeinfo.gettype (); Switch(Activitytype) {//jump to different pages depending on the type              Case1: System.out.println ("Online powder-sucking activity!"); return"Redirect:/active/xsxf/baseinfo";  Case2: System.out.println ("You answered my prize!"); return"Forward:/active/ndwj/baseinfo"; default :                return"Index"; }            }

Ii. Solutions

1. I have a controller in the background to jump to another controller, why there is such a requirement, is the case. I have a list page, and then I will do a new operation, added in the background after the completion of I want to jump to the list page, do not need to pass parameters, List page default query all.

Mode one: Use Modelandview

return new Modelandview ("Redirect:/tolist");

This way you can redirect to ToList.

Mode two: return string

Return "redirect:/toList";

Other ways: There are many other ways, there is no introduction, such as response and so on. This is a redirect without parameters.

2. The second situation, the list page has query criteria, after jumping my query conditions can not be discarded, so that need to take parameters, with parameters can be spliced URL

Way one: Manually stitching the URL yourself


New Modelandview ("Redirect:/tolist?") param1= "+value1+" &param2= "+value2);

There is a drawback, that is, the Chinese language may have garbled problems.


Way two: with Redirectattributes . This is one of the more useful classes found. Here with its AddAttribute method, this actually redirects past after you look at the URL, is it automatically spell your URL for you.

How to use:

Attr.addattribute ("param", value);
return "Redirect:/namespace/tocontroller";

This parameter can be obtained by obtaining parameters in the Tocontroller method, which is then passed to the page. The URL of the past is still the same as the way.

3. With parameters do not splice URL page can also get the value (the focus is this) generally I guess redirect to all want to use this way:

@RequestMapping ("/save")      PublicString Save (@ModelAttribute ("form") Bean form,redirectattributes attr)throwsException {String code=service.save (form); if(Code.equals ("000") {Attr.addflashattribute ("Name", Form.getname ()); Attr.addflashattribute ("Success", "Add success!"); return"Redirect:/index"; }Else{Attr.addattribute ("ProjectName", Form.getprojectname ()); Attr.addattribute ("Enviroment", Form.getenviroment ()); Attr.addflashattribute ("MSG", "Add Error!" error code is: "+rsp.getcode (). GetCode () +", Error: "+Rsp.getcode (). GetName ()); return"Redirect:/maintenance/toaddconfigcenter"; }        }    

@RequestMapping ("/index")    publicthrows  Exception {            return " Redirect:/main/list ";    }

Page value No, I said it, directly with the EL expression can be obtained, the principle here is placed in the session, the session will jump to the page immediately after removing the object. So when you refresh, the value will be lost.

Note: Attr.addattribute ("A", "a"); and Attr.addflashattribute ("B", "B"), the difference.

With parameters can be passed using the Redirectattributes parameter:

Attention:

1. Passing parameters using Redirectattributes's AddAttribute method is followed by the URL, as in the code above is http:/index.action?a=a.


2. Using Addflashattribute will not follow the URL, the value of the parameter will be temporarily saved to the session, the redirect URL to get the parameter is removed from the session, where the redirect must be a method mapping path, JSP invalid. You will find that B will only appear once in the JSP page after redirect, and B will no longer appear after the refresh, which verifies that B is removed from the session when it is accessed. This can be done with duplicate submissions.

Also, if Redirectattributes is used as a parameter, but not redirect? In this case, the Redirectattributes parameter is not passed in the past, the default forward corresponding model, the official recommendations are:

P:ignoredefaultmodelonredirect= "true"/>

Set the Ignoredefaultmodelonredirect property under Requestmappinghandleradapter, which will improve efficiency and avoid unnecessary retrieval.

Spring MVC Framework controller jumps, redirects, parameters

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.