欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC后端Controller頁面跳轉(zhuǎn)的三種方式匯總

 更新時間:2023年10月25日 14:50:02   作者:Thinkingcao  
這篇文章主要介紹了SpringMVC后端Controller頁面跳轉(zhuǎn)的三種方式匯總,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

一、前言

常見的Web開發(fā)中,我們在使用SpringMVC框架時,經(jīng)常遇到多種不同情況下不同的返回方式,有的多種返回頁面的方式,也有的是多種返回數(shù)據(jù)到前端的方式

這篇文章主要總結(jié)常見Web開發(fā)中針對不同情況返回頁面的方式。

二、根據(jù) String 字符串跳轉(zhuǎn)

1、返回字符串 --- 返回jsp頁面

/**
* description: 返回字符串 --- 返回jsp頁面,JSP通過EL表達(dá)式取值
* @author cao
* @date 2019年4月10日 下午10:17
*/
@RequestMapping(value={"/forwardJsp"})
public String forwardJsp(Model model){
    model.addAttribute("name", "張三");
    return "modules/sys/sysLogin";
}

2、返回字符串 --- 服務(wù)端轉(zhuǎn)發(fā)

/**
* description: 返回字符串 --- 服務(wù)端轉(zhuǎn)發(fā),JSP頁面通過EL表達(dá)式取值
* @author cao
* @date 2019年4月10日 下午10:20
*/
@RequestMapping(value={"/forward"})
public String forward(Model model){
    model.addAttribute("name", "張三");
    return "forward:forwardJsp";
}

3、返回字符串 --- 客戶端重定向

/**
* description: 返回字符串 --- 客戶端重定向
* @author cao
* @date 2019年4月10日 下午10:27
*/
@RequestMapping(value="/redirect")
public String redirect(){
    return "redirect:"+"/forward";
}

三、根據(jù) request 或 response 進(jìn)行跳轉(zhuǎn)

1、返回 void --- 請求轉(zhuǎn)發(fā)(request轉(zhuǎn)發(fā))      

/**
* description: 返回 void --- 請求轉(zhuǎn)發(fā)(request轉(zhuǎn)發(fā))
* @author cao
* @date 2019年4月10日 下午10:26
*/
@RequestMapping(value="/requestForward")
public void requestForward(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    request.setAttribute("name", "請求轉(zhuǎn)發(fā)(服務(wù)端轉(zhuǎn)發(fā))");
    request.getRequestDispatcher("/forward").forward(request, response);
}

2、返回 void --- 重定向 (response)

/**
* description: 返回 void --- 重定向 (response)
* @author cao
* @date 2019年4月10日 下午10:29
*/
@RequestMapping(value="/response")
public void response(HttpServletResponse response) throws IOException{
    response.sendRedirect("/forwardJsp");
}

3、返回 void --- Json字符串

/**
* description: 返回 void --- Json字符串
* @author cao
* @date 2019年4月10日 下午10:30
*/
@RequestMapping(value="/responseJson")
public void responseJson(HttpServletResponse response) throws IOException{
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/json;charset=utf-8");
    response.getWriter().write("json串");
}

四、根據(jù) ModelAndView 對象進(jìn)行跳轉(zhuǎn)

1、返回對象 ModelAndView --- 返回 jsp 頁面  

/**
* description: 返回對象 ModelAndView --- 返回 jsp 頁面
* @author cao
* @date 2019年4月10日 下午10:32
*/
@RequestMapping(value="/modelAndViewJsp")
public ModelAndView modelAndViewJsp(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("modules/sys/sysLogin");//指定跳轉(zhuǎn)的頁面
    modelAndView.addObject("name","李四");  //綁定傳遞的數(shù)據(jù)       
    return modelAndView;
}

2、返回對象 ModelAndView --- 服務(wù)端轉(zhuǎn)發(fā)

/**
* description: 返回對象 ModelAndView --- 服務(wù)端轉(zhuǎn)發(fā)
* @author cao
* @date 2019年4月10日 下午10:37
*/
@RequestMapping(value="/modelAndViewForward")
public ModelAndView modelAndViewForward(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("forward:/forwardJsp");
    modelAndView.addObject("name","李四");  //綁定傳遞的數(shù)據(jù)     
    return modelAndView;
}

3、返回對象 ModelAndView --- 重定向  

/**
* description: 返回對象 ModelAndView --- 重定向
* @author cao
* @date 2019年4月10日 下午10:40
*/
@RequestMapping(value="/modelAndViewRedirect")
public ModelAndView modelAndViewRedirect(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("redirect:/forwardJsp");
    modelAndView.addObject("name","李四");  //綁定傳遞的數(shù)據(jù)     
    return modelAndView;
}

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論