SpringMVC页面跳转的三种方式

本文详细介绍了SpringMVC中实现页面跳转的三种主要方式:通过返回字符串、使用request或response对象以及利用ModelAndView对象。每种方式下又分为服务端转发、客户端重定向等具体操作,为开发者提供了全面的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                  SpringMVC页面跳转的三种方式

 

一、 根据 String 字符串跳转

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

/**
* description: 返回字符串 --- 返回jsp页面
* @return String
* @version v1.0
* @author w
* @date 2018年10月22日 下午3:01:43
*/
@RequestMapping(value={"/forwardJsp"})
public String forwardJsp(Model model){
    model.addAttribute("name", "返回jsp页面");
    return "modules/sys/sysLogin";
}

 

2、返回字符串 --- 服务端转发

/**
* description: 返回字符串 --- 服务端转发
* @return String
* @version v1.0
* @author w
* @date 2018年10月22日 15:41:37
*/
@RequestMapping(value={"/forward"})
public String forward(Model model){
    model.addAttribute("name", "服务端转发");
    return "forward:forwardJsp";
}

 

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

/**
* description: 返回字符串 --- 客户端重定向
* @return String
* @version v1.0
* @author w
* @date 2018年10月22日 15:41:57
*/
@RequestMapping(value="/redirect")
public String redirect(){
    return "redirect:"+"/forward";
}

 

二、根据 request 或 response 进行跳转

1、返回 void --- 请求转发(request转发)

/**
* description: 返回 void --- 请求转发(request转发)
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @version v1.0
* @author w
* @date 2018年10月22日 下午3:42:28
*/
@RequestMapping(value="/requestForward")
public void requestForward(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    request.setAttribute("name", "请求转发(服务端转发)");
    request.getRequestDispatcher("/forward").forward(request, response);
}

 

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

/**
* description: 返回 void --- 重定向 (response)
* @param response
* @throws IOException
* @version v1.0
* @author w
* @date 2018年10月22日 下午3:43:40
*/
@RequestMapping(value="/response")
public void response(HttpServletResponse response) throws IOException{
    response.sendRedirect("/forwardJsp");
}

 

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

/**
* description: 返回 void --- Json字符串
* @param response
* @throws IOException
* @version v1.0
* @author w
* @date 2018年10月22日 下午3:43:40
*/
@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串");
}

 

三、根据 ModelAndView 对象进行跳转

1、返回对象 ModelAndView --- 返回 jsp 页面

/**
* description: 返回对象 ModelAndView --- 返回 jsp 页面
* @return ModelAndView
* @version v1.0
* @author w
* @date 2018年10月22日 15:46:37
*/
@RequestMapping(value="/modelAndViewJsp")
public ModelAndView modelAndViewJsp(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("modules/sys/sysLogin");
    return modelAndView;
}

2、返回对象 ModelAndView --- 服务端转发

/**
* description: 返回对象 ModelAndView --- 服务端转发
* @return ModelAndView
* @version v1.0
* @author w
* @date 2018年10月22日 15:46:53
*/
@RequestMapping(value="/modelAndViewForward")
public ModelAndView modelAndViewForward(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("forward:/forwardJsp");
    return modelAndView;
}

 

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

/**
* description: 返回对象 ModelAndView --- 重定向
* @return ModelAndView
* @version v1.0
* @author w
* @date 2018年10月22日 15:47:57
*/
@RequestMapping(value="/modelAndViewRedirect")
public ModelAndView modelAndViewRedirect(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("redirect:/forwardJsp");
    return modelAndView;
}

 

 

参考资料: forward请求转发和redirect重定向的区别图解整理

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值