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--
This article mainly introduces how to use Controller for redirection in Spring MVC. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
There are two ways.
Prefix "redirect:" to the URI (relative path) returned by the method, declaring that you want to redirect to the address
Use HttpServletResponse objects for redirection
Be careful
"redirect:" followed by "/" and not followed by "/" is not the same:
1) "redirect:" followed by "/": indicates that the URI is a relative path relative to the project's Context ROOT
2) "redirect:" is not followed by "/": indicates that the URI is relative to the current path.
Specifically, demo understands the implementation of these two ways.
RedirectURLController.java:
Package edu.mvcdemo.controller; import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import edu.mvcdemo.utils.StringUtils / * * @ author: yh.zeng * @ Writing time: 2017-7-13 9:10:29 * @ File description: Spring MVC redirect demo * / @ Controller@Scope ("singleton") / / only one bean object is instantiated (that is, the same bean object is used for each request). The default is singleton@RequestMapping ("/ redirect") public class RedirectURLController {private Logger logger = Logger.getLogger (RedirectURLController.class). / * method 1: add the prefix "redirect:" to the URI (relative path) returned by the method Declare that you want to redirect to the address * "redirect:" followed by "/" and not followed by "/" is different: * 1) "redirect:" followed by "/": indicates that the URI is the relative path to the project's Context ROOT * 2) "redirect:" is not followed by "/": description The URI is relative to the current path * @ return * / @ RequestMapping (value= "/ demo1") Method=RequestMethod.GET) private String testRedirect1 () {/ / Note: "redirect:/hello/world" and "redirect:hello/world" are not the same! / / in this case: / / the URL path to which "redirect:/hello/world" is redirected is: protocol: / / server IP or server hostname: Port number / project Context ROOT/hello/world / / "redirect:hello/world" redirect to URL path is: protocol: / / server IP or server hostname: Port number / Context ROOT/redirect/hello/world return "redirect:/hello/world" of the project } / * method 2: redirect using HttpServletResponse object The HttpServletResponse object passes * @ param request * @ param response * @ return * @ throws IOException * / @ RequestMapping (value= "/ demo2", method=RequestMethod.GET) private void testRedirect2 (HttpServletRequest request, HttpServletResponse response) {String pathPrefix = StringUtils.getWebContextPath (request) through the method input parameter String redirectURL = pathPrefix + "/ hello/world"; logger.info (redirectURL); try {response.sendRedirect (redirectURL);} catch (IOException e) {logger.error (StringUtils.getExceptionMessage (e));}
StringUtils.java:
Package edu.mvcdemo.utils; import java.io.PrintWriter;import java.io.StringWriter;import javax.servlet.http.HttpServletRequest / * * @ author: yh.zeng * @ Writing time: 2017-7-9 2:56:21 * @ File description: todo * / public class StringUtils {/ * get exception information * * @ param e * @ return * / public static String getExceptionMessage (Exception e) {StringWriter stringWriter = new StringWriter (); PrintWriter printWriter = new PrintWriter (stringWriter) E.printStackTrace (printWriter); return stringWriter.toString ();} / * * returns the context path of the web project in the format: protocol: / / server IP or server hostname: Context ROOT * @ param request * @ return * / public static String getWebContextPath (HttpServletRequest request) {StringBuilder webContextPathBuilder = new StringBuilder () of the project. WebContextPathBuilder.append (request.getScheme ()) .append (": / /") .append (request.getServerName ()) .append (":"). Append (request.getServerPort ()) .append (request.getContextPath ()) Return webContextPathBuilder.toString ();}}
Effect:
Page input http://localhost:8080/MavenSpringMvcDemo/redirect/demo1 or http://localhost:8080/MavenSpringMvcDemo/redirect/demo2 will be redirected to http://localhost:8080/MavenSpringMvcDemo/hello/world
Controller request forwarding, redirect understanding
Forward: the browser address does not change, it is always the same request.
Redirect (sendRedirect): the browser address changes and there are two requests.
Forward forward
It would be nice to throw an exception:
Skip the home page: the browser's url address remains the same. The static file may not be found:
@ GetMapping (value = "/ index") @ ApiOperation ("Home") public void index (HttpServletRequest request, HttpServletResponse response) throws Exception {request.getRequestDispatcher ("/ index.html") .forward (request, response);} Redirect redirect
The return value in controller is void
@ GetMapping (value = "/ index") @ ApiOperation ("Home") public void index (HttpServletRequest request, HttpServletResponse response) throws IOException {response.sendRedirect ("/ index.html");}
Third, the return value in controller is ModelAndView.
Return new ModelAndView ("redirect:/toList"); the above is all the content of the article "how to use Controller for redirection in Spring MVC". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.