Spring MVC中使用Controller如何進(jìn)行重定向
Controller如何進(jìn)行重定向
Spring MVC中進(jìn)行重定向
本人知道的有兩種方式
方法返回的URI(相對路徑)中加上"redirect:"前綴,聲明要重定向到該地址
使用HttpServletResponse對象進(jìn)行重定向
注意
"redirect:"后面跟著的是"/"和不跟著"/"是不一樣的:
1) "redirect:"后面跟著"/": 說明該URI是相對于項(xiàng)目的Context ROOT的相對路徑
2) "redirect:"后面沒有跟著"/": 說明該URI是相對于當(dāng)前路徑
具體看demo理解這兩種方式的實(shí)現(xiàn)
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;
/**
* @編寫人: yh.zeng
* @編寫時(shí)間:2017-7-13 上午9:10:29
* @文件描述: Spring MVC重定向demo
*/
@Controller
@Scope("singleton") //只實(shí)例化一個(gè)bean對象(即每次請求都使用同一個(gè)bean對象),默認(rèn)是singleton
@RequestMapping("/redirect")
public class RedirectURLController {
private Logger logger = Logger.getLogger(RedirectURLController.class);
/**
* 方式一:方法返回的URI(相對路徑)中加上"redirect:"前綴,聲明要重定向到該地址
* "redirect:"后面跟著的是"/"和不跟著"/"是不一樣的:
* 1) "redirect:"后面跟著"/": 說明該URI是相對于項(xiàng)目的Context ROOT的相對路徑
* 2) "redirect:"后面沒有跟著"/": 說明該URI是相對于當(dāng)前路徑
* @return
*/
@RequestMapping(value="/demo1", method=RequestMethod.GET)
private String testRedirect1(){
//注意:"redirect:/hello/world" 和 "redirect:hello/world"這兩種寫法是不一樣的??!
// 本案例中:
// "redirect:/hello/world" 重定向到的URL路徑為:協(xié)議://服務(wù)器IP或服務(wù)器主機(jī)名:端口號(hào)/項(xiàng)目的Context ROOT/hello/world
// "redirect:hello/world" 重定向到的URL路徑為:協(xié)議://服務(wù)器IP或服務(wù)器主機(jī)名:端口號(hào)/項(xiàng)目的Context ROOT/redirect/hello/world
return "redirect:/hello/world";
}
/**
* 方式二:使用HttpServletResponse對象進(jìn)行重定向,HttpServletResponse對象通過方法入?yún)魅?
* @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);
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;
/**
* @編寫人: yh.zeng
* @編寫時(shí)間:2017-7-9 下午2:56:21
* @文件描述: todo
*/
public class StringUtils {
/**
* 獲取異常信息
*
* @param e
* @return
*/
public static String getExceptionMessage(Exception e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
return stringWriter.toString();
}
/**
* 返回web項(xiàng)目的context path,格式 為:協(xié)議://服務(wù)器IP或服務(wù)器主機(jī)名:端口號(hào)/項(xiàng)目的Context ROOT
* @param request
* @return
*/
public static String getWebContextPath(HttpServletRequest request){
StringBuilder webContextPathBuilder = new StringBuilder();
webContextPathBuilder.append(request.getScheme())
.append("://")
.append(request.getServerName())
.append(":")
.append(request.getServerPort())
.append(request.getContextPath());
return webContextPathBuilder.toString();
}
}
效果:
頁面輸入 http://localhost:8080/MavenSpringMvcDemo/redirect/demo1 或 http://localhost:8080/MavenSpringMvcDemo/redirect/demo2 都會(huì)重定向到http://localhost:8080/MavenSpringMvcDemo/hello/world
controller請求轉(zhuǎn)發(fā),重定向
了解
轉(zhuǎn)發(fā)(forward):瀏覽器地址不會(huì)改變,始終是同一個(gè)請求。
重定向(sendRedirect):瀏覽器地址會(huì)改變,是兩個(gè)請求。
轉(zhuǎn)發(fā)forward
有異常拋出就好了:
跳首頁:瀏覽器的url地址不變.可能會(huì)找不到靜態(tài)文件:
@GetMapping(value = "/index")
@ApiOperation("首頁")
public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getRequestDispatcher("/index.html").forward(request, response);
}
重定向redirect
controller中返回值為void
@GetMapping(value = "/index")
@ApiOperation("首頁")
public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("/index.html");
}
第三種方式:controller中返回值為ModelAndView
return new ModelAndView(“redirect:/toList”);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot參數(shù)校驗(yàn)Validator框架詳解
Validator框架就是為了解決開發(fā)人員在開發(fā)的時(shí)候少寫代碼,提升開發(fā)效率,Validator專門用來進(jìn)行接口參數(shù)校驗(yàn),今天通過本文給大家介紹SpringBoot參數(shù)校驗(yàn)Validator框架,感興趣的朋友一起看看吧2022-06-06
詳解Java并發(fā)工具類之CountDownLatch和CyclicBarrier
在JDK的并發(fā)包中,有幾個(gè)非常有用的并發(fā)工具類,它們分別是:CountDownLatch、CyclicBarrier、Semaphore和Exchanger,本文主要來講講其中CountDownLatch和CyclicBarrier的使用,感興趣的可以了解一下2023-06-06
SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
這篇文章主要介紹了SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Java實(shí)現(xiàn)的權(quán)重算法(按權(quán)重展現(xiàn)廣告)
這篇文章主要介紹了Java實(shí)現(xiàn)的權(quán)重算法(按權(quán)重展現(xiàn)廣告),本文講解了算法實(shí)現(xiàn)原理和實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
java程序設(shè)計(jì)語言的優(yōu)勢及特點(diǎn)
在本篇文章里小編給大家分享的是一篇關(guān)于java程序設(shè)計(jì)語言的優(yōu)勢及特點(diǎn)的內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-02-02
Java對象以Hash結(jié)構(gòu)存入Redis詳解
這篇文章主要介紹了Java對象以Hash結(jié)構(gòu)存入Redis詳解,和Java中的對象非常相似,卻不能按照J(rèn)ava對象的結(jié)構(gòu)直接存儲(chǔ)進(jìn)Redis的hash中,因?yàn)镴ava對象中的field是可以嵌套的,而Redis的Hash結(jié)構(gòu)不支持嵌套結(jié)構(gòu),需要的朋友可以參考下2023-08-08
Socket結(jié)合線程池使用實(shí)現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來實(shí)現(xiàn)客戶端和服務(wù)端通信實(shí)戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
springboot websocket集群(stomp協(xié)議)連接時(shí)候傳遞參數(shù)
這篇文章主要介紹了springboot websocket集群(stomp協(xié)議)連接時(shí)候傳遞參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

