Jump between controllers in Spring mvc Framework, redirect, passing parameters, and mvccontroller

Source: Internet
Author: User

Jump between controllers in Spring mvc Framework, redirect, passing parameters, and mvccontroller

I. background

1. Requirement: Redirect between controllers of spring MVC framework. There are several situations: jump without parameters, jump in the form of parameter splicing url, jump with parameters without parameters, the page can also be displayed.
@ RequestMapping (value = "/activityType", method = RequestMethod. GET) public String activityType (HttpServletRequest request, ModelMap model, RedirectAttributes attr) throws Exception {logger.info ("into activityType"); String id = request. getParameter ("typeId"); String activityId = request. getParameter ("activityId"); // model. put ("typeId", id); // model. put ("activityId", activityId); // attr. addFlashAttr Ibute ("typeId", id); // attr. addFlashAttribute ("activityId", activityId); attr. addAttribute ("typeId", id); attr. addAttribute ("activityId", activityId); ActivityTypeInfo activityTypeInfo = activityTypeInfoBiz. get (Integer. valueOf (id); // query the activity type object Integer activityType = activityTypeInfo based on the activity ID. getType (); switch (activityType) {// jump to different pages based on different types case 1: System. out. println ("online powdering activity! "); Return" redirect:/active/xsxf/baseInfo "; case 2: System. out. println (" You answer my prize activity! "); Return" forward:/active/ndwj/baseInfo "; default: return" index ";}}

 

Ii. Solution

 

1. I jumped to another controller in the background. Why is there such a requirement. I have a list page, and then I will perform the add operation. After the addition is completed in the background, I want to jump to the List page without passing parameters. The list page queries all the items by default.

 

Method 1: Use ModelAndView

 

Return new ModelAndView ("redirect:/toList ");

In this way, you can redirect to the toList method.

 

Method 2: Return String

 

Return "redirect:/toList ";

Other methods: there are many other methods. I will not introduce them here, such as response. This is a redirection without parameters.

 

 

2. In the second case, the list page has query conditions. After the jump, My query conditions cannot be lost. In this case, you need to include parameters. You can concatenate URLs with parameters.

 

Method 1: manually splice URLs


New ModelAndView ("redirect:/toList? Param1 = "+ value1 +" & param2 = "+ value2 );

The drawback is that Chinese characters may contain garbled characters.


Method 2: Use RedirectAttributes. This is a useful class found. Here, we use its addAttribute method. This actually redirects the past and you will see the url. It will automatically spell your url for you.

 

Usage:

Attr. addAttribute ("param", value );
Return "redirect:/namespace/toController ";

In this way, you can obtain this parameter in the toController method, and then pass it to the page. In the past, the url is the same as the method.

 

3. The value can be obtained even if the url page is not spliced with parameters (the key point is this). Generally, I want to use this method for redirection:

 

@ RequestMapping ("/save") public String save (@ ModelAttribute ("form") Bean form, RedirectAttributes attr) throws Exception {String code = service. save (form); if (code. equals ("000") {attr. addFlashAttribute ("name", form. getName (); attr. addFlashAttribute ("success", "added successfully! "); Return" redirect:/index ";} else {attr. addAttribute ("projectName", form. getProjectName (); attr. addattriment ("enviroment", form. getEnviroment (); attr. addFlashAttribute ("msg", "add error! Error Code: "+ rsp. getCode (). getCode () + ", error:" + rsp. getCode (). getName (); return "redirect:/maintenance/toAddConfigCenter ";}}

 

 

@RequestMapping("/index")    public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr) throws Exception {            return "redirect:/main/list";    }

 

The page value can be obtained directly by using the el expression. The principle is to put the value in the session, and the object will be removed immediately after the session jumps to the page. So this value will be lost after you refresh it.

 

Note: attr. addAttribute ("a", "a"); andAttr. addFlashAttribute ("B", "B.

 

The RedirectAttributes parameter can be used for transmission with parameters:

Note:

1. Using the addAttribute method of RedirectAttributes to pass parameters will follow the URL. The above code is http:/index. action? A =.


2. if addFlashAttribute is used, the parameter value will not be followed by the URL and will be temporarily stored in the session. After the Redirection url obtains the parameter, it will be removed from the session. The redirect here must be the method ing path, invalid jsp. You will find that B appears only once on the jsp page after redirect, and B will not appear again after refresh. This verifies what we mentioned above, B will be removed from the session after being accessed. This can be used for repeated submission.

 

In addition, if RedirectAttributes is used as the parameter, but redirect is not performed? In this case, the RedirectAttributes parameter will not be passed over. By default, the model corresponding to forward will be passed. The official suggestions are as follows:

P: ignoredefamodelmodelonredirect = "true"/>

 

Set the ignoredefamodelmodelonredirect attribute of RequestMappingHandlerAdapter to improve efficiency and avoid unnecessary retrieval.

 

 

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.