springmvc請求轉(zhuǎn)發(fā)和重定向問題(攜帶參數(shù)和不攜帶參數(shù))
請求轉(zhuǎn)發(fā)的三種方式
SpringMVC請求轉(zhuǎn)發(fā)區(qū)別于重定向,請求轉(zhuǎn)發(fā)地址欄不會發(fā)生改變、只發(fā)送一次請求、能攜帶原有的參數(shù),但只可以在同一個服務(wù)器中進行轉(zhuǎn)發(fā)。
1、傳統(tǒng)的請求轉(zhuǎn)發(fā)
@RequestMapping("/forwardCommon")
public ?forwardCommon(HttpServletRequest request,HttpServletResponse response){
? request.getServletDispatcher("/WEBINF/pages/success.jsp").forward(request,response);
}2、SpringMVC中不經(jīng)過視圖解析器的請求轉(zhuǎn)發(fā)
@RequestMapping("/forwardView")
public String forwardView(){
?? ?return "forward:/WEB_INF/pages/success.jsp";
}@RequestMapping("/forwardView")
public String forwardView(){
?? ?ModelAndView mv=new ModelAndView();
? ? mv.setViewName("forward:/WEB_INF/pages/success.jsp");
}3、請求轉(zhuǎn)發(fā)經(jīng)過視圖解析器
其實很多人并不知道經(jīng)過視圖解析器的路由轉(zhuǎn)發(fā)也是請求轉(zhuǎn)發(fā)
@RequestMapping("/shopmanagement")
private String shopManagement(Model model) {
?? ?return "shop/shopmanagement";
}@RequestMapping("/shopmanagement")
private ModelAndView shopManagement(Model model) {
?? ?ModelAndView mv=new ModelAndView();
? ? mv.setViewName("shop/shopmanagement");//也是請求轉(zhuǎn)發(fā),會經(jīng)過視圖解析器
? ? return mv;
}上面的請求轉(zhuǎn)發(fā)只要在瀏覽器地址欄中輸入localhost:8080/o2o/shopadmin/shopmanagement?shopId=53,就可以訪問shopmanagement.html這個頁面,并且我們可以嘗試在該頁面中引入一個js文件,試圖查看請求轉(zhuǎn)發(fā)后的地址。
$(function(){
?? ?var shopId=getQueryString("shopId");//獲取url中的參數(shù)
?? ?var url=window.location.href;//獲得請求url
})
function getQueryString(name){
?? ?var reg=new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
?? ?var r=window.location.search.substr(1).match(reg);
?? ?if(r!=null){
?? ??? ?return decodeURIComponent(r[2]);
?? ?}
?? ?return null;
}
我們可以發(fā)現(xiàn)請求轉(zhuǎn)發(fā)后的地址和第一次請求的地址一樣, 連地址后面攜帶的參數(shù)都一樣。所以請求轉(zhuǎn)發(fā)不僅可以在請求體中攜帶參數(shù)(POST請求),也可以在地址欄中攜帶參數(shù)(GET請求),最后都可以在轉(zhuǎn)發(fā)后的頁面中獲得該參數(shù)的值。
重定向
傳統(tǒng)的重定向請求地址會改變(兩次請求)、不能傳遞參數(shù),但是利用SpringMVC的重定向可以攜帶和傳遞參數(shù)。
重定向相比于請求轉(zhuǎn)發(fā)可以跨服,但是不能直接重定向訪問WEB-INF下的資源(可重定向后再進行一次請求轉(zhuǎn)發(fā))。
1、傳統(tǒng)的重定向
@RequestMapping(value="/testredirect",method = { RequestMethod.POST, RequestMethod.GET })
public void testredirect(HttpServletResponse response){
response.sendRedirect("/index");
}2、SpringMVC中不帶參數(shù)的重定向
@RequestMapping(value="/testredirect",method = { RequestMethod.POST, RequestMethod.GET })
public String testredirect(){
return "redirect:/index";
}@RequestMapping(value="/testredirect",method = { RequestMethod.POST, RequestMethod.GET })
public ModelAndView testredirect(){
ModelAndView mv=new ModelAndView();
mv.setViewName("redirect:/index");
return mv;
}3、SpringMVC中帶參數(shù)的重定向
redirectAttributes.addAttributie("prama",value); 這種方法相當于在重定向鏈接地址上追加傳遞的參數(shù)
@RequestMapping("/test")
private String shopList(RedirectAttributes ra) {
?? ?ra.addAttribute("param", 1);
?? ?return "redirect:/shopadmin/shoplist";
}
//相當于請求 http://localhost:8080/o2o/shopadmin/shoplist?param=1redirectAttributes.addFlashAttributie("prama",value); 這種方法是隱藏了參數(shù),鏈接地址上不直接暴露,用(@ModelAttribute(value = "prama")String prama)的方式獲取參數(shù)。
@RequestMapping("/test")
private String shopList(RedirectAttributes ra) {
?? ?ra.addFlashAttribute("param", 1);
?? ?return "redirect:/shopadmin/shoplist";
}接收參數(shù)
@RequestMapping("/shoplist")
private String shopList(@ModelAttribute("param") String param) {
?? ?System.out.println(param);//輸出1
?? ?return "shop/shoplist";
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot JWT接口驗證實現(xiàn)流程詳細介紹
這篇文章主要介紹了SpringBoot+JWT實現(xiàn)接口驗證,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
java 重載(overload)與重寫(override)詳解及實例
這篇文章主要介紹了java 重載(overload)與重寫(override)詳解及實例的相關(guān)資料,并附實例代碼,需要的朋友可以參考下2016-10-10
整理Java的MyBatis框架中一些重要的功能及基本使用示例
這篇文章主要介紹了Java的MyBatis框架中一些重要的功能及基本使用示例整理,MyBatis可以幫助Java程序進行強大的數(shù)據(jù)庫操作,需要的朋友可以參考下2016-04-04
關(guān)于Spring項目對JDBC的支持與基本使用詳解
這段時間一直在觀看Spring框架,所以下面這篇文章主要給大家介紹了關(guān)于Spring項目對JDBC的支持與基本使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Java?Rabbitmq中四種集群架構(gòu)的區(qū)別詳解
這篇文章主要為大家詳細介紹了Java?Rabbitmq中四種集群架構(gòu)的區(qū)別,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02

