Spring MVC中使用Controller如何進(jìn)行重定向
Controller如何進(jìn)行重定向
Spring MVC中進(jìn)行重定向
本人知道的有兩種方式
方法返回的URI(相對(duì)路徑)中加上"redirect:"前綴,聲明要重定向到該地址
使用HttpServletResponse對(duì)象進(jìn)行重定向
注意
"redirect:"后面跟著的是"/"和不跟著"/"是不一樣的:
1) "redirect:"后面跟著"/": 說(shuō)明該URI是相對(duì)于項(xiàng)目的Context ROOT的相對(duì)路徑
2) "redirect:"后面沒(méi)有跟著"/": 說(shuō)明該URI是相對(duì)于當(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對(duì)象(即每次請(qǐng)求都使用同一個(gè)bean對(duì)象),默認(rèn)是singleton
@RequestMapping("/redirect")
public class RedirectURLController {
private Logger logger = Logger.getLogger(RedirectURLController.class);
/**
* 方式一:方法返回的URI(相對(duì)路徑)中加上"redirect:"前綴,聲明要重定向到該地址
* "redirect:"后面跟著的是"/"和不跟著"/"是不一樣的:
* 1) "redirect:"后面跟著"/": 說(shuō)明該URI是相對(duì)于項(xiàng)目的Context ROOT的相對(duì)路徑
* 2) "redirect:"后面沒(méi)有跟著"/": 說(shuō)明該URI是相對(duì)于當(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對(duì)象進(jìn)行重定向,HttpServletResponse對(duì)象通過(guò)方法入?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();
}
}
效果:
頁(yè)面輸入 http://localhost:8080/MavenSpringMvcDemo/redirect/demo1 或 http://localhost:8080/MavenSpringMvcDemo/redirect/demo2 都會(huì)重定向到http://localhost:8080/MavenSpringMvcDemo/hello/world
controller請(qǐng)求轉(zhuǎn)發(fā),重定向
了解
轉(zhuǎn)發(fā)(forward):瀏覽器地址不會(huì)改變,始終是同一個(gè)請(qǐng)求。
重定向(sendRedirect):瀏覽器地址會(huì)改變,是兩個(gè)請(qǐng)求。
轉(zhuǎn)發(fā)forward
有異常拋出就好了:
跳首頁(yè):瀏覽器的url地址不變.可能會(huì)找不到靜態(tài)文件:
@GetMapping(value = "/index")
@ApiOperation("首頁(yè)")
public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getRequestDispatcher("/index.html").forward(request, response);
}
重定向redirect
controller中返回值為void
@GetMapping(value = "/index")
@ApiOperation("首頁(yè)")
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整合Ldap的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合Ldap的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
SpringBoot3使用Jasypt加密數(shù)據(jù)庫(kù)用戶名、密碼等敏感信息
使用Jasypt(Java Simplified Encryption)進(jìn)行數(shù)據(jù)加密和解密主要涉及幾個(gè)步驟,包括引入依賴、配置加密密碼、加密敏感信息、將加密信息存儲(chǔ)到配置文件中,以下是詳細(xì)的使用說(shuō)明,需要的朋友可以參考下2024-07-07
如何在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串
這篇文章主要介紹了在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串類型數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Java基于Socket的文件傳輸實(shí)現(xiàn)方法
這篇文章主要介紹了Java基于Socket的文件傳輸實(shí)現(xiàn)方法,結(jié)合實(shí)例分析了Java使用Socket實(shí)現(xiàn)文件傳輸?shù)慕⑦B接、發(fā)送與接收消息、文件傳輸?shù)认嚓P(guān)技巧,需要的朋友可以參考下2015-12-12
spring @Validated 注解開(kāi)發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了spring @Validated 注解開(kāi)發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
SpringKafka消息發(fā)布之KafkaTemplate與事務(wù)支持功能
通過(guò)本文介紹的基本用法、序列化選項(xiàng)、事務(wù)支持、錯(cuò)誤處理和性能優(yōu)化技術(shù),開(kāi)發(fā)者可以構(gòu)建高效可靠的Kafka消息發(fā)布系統(tǒng),事務(wù)支持特性尤為重要,它確保了在分布式環(huán)境中的數(shù)據(jù)一致性,感興趣的朋友一起看看吧2025-04-04
web.xml?SpringBoot打包可執(zhí)行Jar運(yùn)行SpringMVC加載流程
這篇文章主要為大家介紹了web.xml?SpringBoot打包可執(zhí)行Jar運(yùn)行SpringMVC加載流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Mybatis?mysql模糊查詢方式(CONCAT多個(gè)字段)及bug
這篇文章主要介紹了Mybatis?mysql模糊查詢方式(CONCAT多個(gè)字段)及bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

