In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use springmvc to configure the view parser and redirect, I hope you will learn something after reading this article, let's discuss it together!
Why do springmvc configure view parsers and redirect need to configure view parsers
Look at the picture.
Configuration step
Step 1: need to configure in springmvc.xml
Step 2: the controller code Controller method after configuring the view parser returns a string to represent the logical view name, which is resolved to the physical view address through the view parser. The default physical view address is: view prefix + return value + view suffix.
/ * * method returns ModelAndView mode * ModelAndView * Model: model, data * View: view * encapsulate the view that needs to be shared to jump to the ModelAndView object You can return the method directly * * by default, you can only use request forwarding * / @ RequestMapping ("/ method2") public ModelAndView method2 () {ModelAndView mv = new ModelAndView () / / shared data mv.addObject ("username", "Lily"); / / set the view address / / configure the view parser before prefix / / mv.setViewName ("/ WEB-INF/view/hello.jsp") / / configure mv.setViewName ("hello") after the prefix of the view parser; return mv } / * * method returns String string mode * * the character returned by the method is the address to jump * * inject a Model object into the method Sharing data in Model * * default to request forwarding * / @ RequestMapping ("/ method3") public String method3 (Model m) {System.out.println (m.getClass () .getName ()) / / shared data m.addAttribute ("username", "Jim"); / / configure the view parser before / / return "/ WEB-INF/view/response.jsp"; / / configure the view parser to return "response" after the prefix } resolve the problems caused by configuring the view parser
If you directly use the configuration development of the view parser, you must ensure that there is a corresponding page file under the prefix directory of the view parser before you can jump, otherwise an error will be reported.
The default page jump can only use request forwarding, not redirection.
Need to solve the problem: in addition to using the view parser to develop corresponding rules, users also have to customize the jump method and the custom jump page scheme: using the request forwarding and redirection configuration of the view parser, you can break the default rules
Public static final String REDIRECT_URL_PREFIX = "redirect:"; public static final String FORWARD_URL_PREFIX = "forward:"
/ * Question1: the above two methods can only forward requests by default. How to redirect them? * problem 2: jumped pages can always be placed according to the prefix path of view parsing, and how to put pages under other directories can also be redirected * * solution: use the custom page jump function in the view parser (use this Only String string is returned) * prefix * redirect * redired: page to be redirected * request forwarding * forward: page to be redirected * / Custom request forwarding page (page that breaks the default view parser prefix rules) @ RequestMapping ("/ method4") public String method4 (Model m) {m.addAttribute ("username") Han Meimei) Return "forward:/WEB-INF/user_list.jsp";} redirect / / Custom Redirection @ RequestMapping ("/ method5") public String method5 () {return "redirect: http://www.jd.com"; } configure multiple view parsers springmvc controller@Controller@Scope ("prototype") @ RequestMapping ("/ viewResolver") public class ViewResolverController {@ RequestMapping ("/ T1") public String T1 () {return "index" / / webinfo/jsp/index} @ RequestMapping ("/ T2") public String T2 () {return "htmlindex"; / / webinfo/html/index}} after reading this article, I believe you have some understanding of "how to use springmvc to configure view parsers and redirects". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.