Spring MVC redirect Jump path problem

Source: Internet
Author: User

Springmvc REDIRECT View Redirectview small analysis

Objective

SPRINGMVC is currently one of the mainstream web MVC frameworks.

The part of this article is related to SPRINGMVC's view mechanism, SPRINGMVC view mechanism, please refer to the landlord's other blog:

Redirectview This view is related to redirection, but also the core of the redirect problem, let's look at the source of this class.

After the path is constructed, use reponse for sendredirect operations.

Example explanation

Controller code:

@Controller
@RequestMapping (value = "/redirect")
public class Testredirectcontroller {

@RequestMapping("/test1")public ModelAndView test1() { view.setViewName("redirect:index"); return view;}@RequestMapping("/test2")public ModelAndView test2() { view.setViewName("redirect:login"); return view;}@RequestMapping("/test3")public ModelAndView test3(ModelAndView view) { view.setViewName("redirect:/index"); return view;}@RequestMapping("/test4")public ModelAndView test4(ModelAndView view) { view.setView(new RedirectView("/index", false)); return view;}@RequestMapping("/test5")public ModelAndView test5(ModelAndView view) { view.setView(new RedirectView("index", false)); return view;}@RequestMapping("/test6/{id}")public ModelAndView test6(ModelAndView view, @PathVariable("id") int id) { view.setViewName("redirect:/index{id}");
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

View.addobject ("Test", "test");
return view;
}

@RequestMapping("/test7/{id}")public ModelAndView test7(ModelAndView view, @PathVariable("id") int id) {    RedirectView redirectView = new RedirectView("/index{id}");    redirectView.setExpandUriTemplateVariables(false);    redirectView.setExposeModelAttributes(false);    view.setView(redirectView);    view.addObject("test", "test");    return view;}
 
   
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

}
Look at the Test1 method first, return the value "Redirect:index", then use the redirect.

Springmvc when looking for the view name "Redirect:index", the viewresolver used in this article is freemarkerviewresolver.

Freemarkerviewresolver the CreateView method in urlbasedviewresolver that resolves the view name, calling one of the parent classes most.

Through the construction method, this redirectview uses relative paths, is compatible with Http1.0, and does not expose path variables.

Test1 method, enter the path:/springmvcdemo/redirect/test1. Redirectview use relative path, then redirect path:/springmvcdemo/redirect/index

We look at the path through Firebug:

Nice, verified our ideas.

Test2 method: Enter the path:/springmvcdemo/redirect/test2. REDIRECT path:/springmvcdemo/redirect/login.

Test3 method: Start with "/" and use relative path, then add ContextPath by default. Enter the path:/springmvcdemo/redirect/test3. REDIRECT path:/springmvcdemo/index.

Test4 method: Do not use the default path, the Createtargeturl method directly append "/index". Enter the path:/springmvcdemo/redirect/test4. REDIRECT path:/index.

Test5 method: Does not use the default path, the Createtargeturl method directly append "index". Enter the path:/SPRINGMVCDEMO/REDIRECT/TEST5. REDIRECT path:/springmvcdemo/redirect/index.

In fact, there is a bit of an accident, just beginning to think that the absolute path is not used, the future path will be/springmvcdemo/index or/index. The result is not so, feel SPRINGMVC this name a bit awkward, a bit of egg ache. In fact, Redirectview in the Createtargeturl method to understand that the source code is the best document 0 0.

Test6 method: Using the default path, the method also uses a path variable. Earlier in this article, I said that when constructing a redirect path in Redirectview, the path variable is substituted, and the properties in the model are exposed, and the 2 exposures are affected by the attribute Expanduritemplatevariables, Exposemodelattributes effect, these 2 properties are true by default. Enter the path:/SPRINGMVCDEMO/REDIRECT/TEST6/1. REDIRECT path:/springmvcdemo/index1?test=test.

Test7 method: As with the Test6 method, except that we do not expose the model property, and do not replace the path variable. Enter the path:/SPRINGMVCDEMO/REDIRECT/TEST7/1. REDIRECT path:/springmvcdemo/index{id}.

Summarize

This paper analyzes the Redirectview view, analyzes the rendering source of the view, and analyzes the common problems such as path problem, parameter problem and path variable problem in redirection.

Source code is really the best document, after understanding the view mechanism, and then back to look at the Redirectview view, so easy.

Finally feel Redirectview's relative path property is strange, do not use relative path, "/" The beginning is the server root path, without "/" beginning, is the relative path, a bit of egg pain.

I hope this article can help readers understand the problem of SPRINGMVC redirection

Transfer from http://blog.csdn.net/wilsonke/article/details/39177421

Spring MVC redirect Jump path problem

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.