spring mvc redirect 重定向 跳转并传递参数 && mvc:view-controller直接转发页面

在项目中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,具体跳转方式有以下几种方式:

公用代码:

Java代码  收藏代码

  1. @RequestMapping(value="/index",method = { RequestMethod.POST, RequestMethod.GET })  
  2. public  ModelAndView index(HttpServletResponse response){  
  3.     ModelAndView model = new ModelAndView("/home/index");  
  4.     return model;  
  5. }  

 

 

一、使用HttpServletResponse 进行重定向跳转

  

Java代码  收藏代码

  1.      @RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET })  
  2. ublic  ModelAndView toIndex(HttpServletResponse response){  
  3. try {  
  4.     response.sendRedirect("/index");  
  5. } catch (IOException e1) {  
  6. }  
  7. return null;  

 

二、依赖spring mvc的 ViewResolver直接跳转

 

Java代码  收藏代码

  1. @RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET })  
  2. public  String toIndex(HttpServletResponse response){  
  3.     return "redirect:/index";  
  4. }  

 

注:当需要传递简单参数时可以使用以上两种方式通过get方式将参数拼接到url路劲后面。

 

三、依赖Spring mvc的RedirectAttributes 

 

Java代码  收藏代码

  1. @RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET })  
  2. public  String toIndex(HttpServletResponse response,RedirectAttributes model){  
  3.     model.addFlashAttribute("userName", 'TimerBin');  
  4.     model.addFlashAttribute("userPass", 'ApeVm23U3wxEGocX');  
  5.     return "redirect:/index";  
  6. }  

 在/home/index 可以直接使用${userName},${userPass}来获取重定向跳转的参数信息,这种方式可以处理复杂的参数传值问题,还可以使用此种方式来隐藏或缩短原有请求URL信息。

 

在controller中获取放在RedirectAttributes中的userName信息的方式:

 

Java代码  收藏代码

  1. @RequestMapping(value="/index",method = { RequestMethod.POST, RequestMethod.GET })  
  2. public  ModelAndView index(@ModelAttribute("userName") String userName){  
  3.         ModelAndView  model = new ModelAndView("/main/index");   
  4.     model.addObject("userName", userName);  
  5.     return model;  
  6. }  

 

 注:在项目中使用RedirectAttributes,因为该对象就是把参数信息放到项目中的session中,再多台服务器中使用该对象存储参数时已经要保证sesion设置是粘性的,不然在集群服务器中不支持该对象的使用!

 

 

在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面,

不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的mvc:view-controller标签做到页面的直接访问。

实例参考 :http://blog.csdn.net/yhl_jxy/article/details/51265077

在上面的实例中修改spirngmvc.xml配置文件如下:

[html] view plain copy 在CODE上查看代码片派生到我的代码片

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.        xmlns:p="http://www.springframework.org/schema/p"  
  6.        xmlns:context="http://www.springframework.org/schema/context"  
  7.        xmlns:aop="http://www.springframework.org/schema/aop"  
  8.        xmlns:tx="http://www.springframework.org/schema/tx"  
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  10.             http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  11.             http://www.springframework.org/schema/context   
  12.             http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  13.             http://www.springframework.org/schema/aop   
  14.             http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  15.             http://www.springframework.org/schema/tx   
  16.             http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  17.             http://www.springframework.org/schema/mvc   
  18.             http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  19.             http://www.springframework.org/schema/context   
  20.             http://www.springframework.org/schema/context/spring-context-4.1.xsd">  
  21.     <!--  配置自动扫描的包  -->  
  22.     <context:component-scan base-package="com.lanhuigu.springmvc"/>  
  23.     <!-- 视图解析器 -->      
  24.     <bean id="viewResolver"  
  25.           class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  26.           <!-- 配置前缀 -->  
  27.           <property name="prefix" value="/WEB-INF/views/"/>  
  28.           <!-- 配置后缀 -->  
  29.           <property name="suffix" value=".jsp"/>  
  30.     </bean>  
  31.     <!-- 配置直接转发的页面 -->  
  32.     <mvc:view-controller path="/success" view-name="success"/>  
  33.     <!-- 解决mvc:view-controller配置后RequestMapping映射地址报404的问题 -->  
  34.     <mvc:annotation-driven></mvc:annotation-driven>  
  35. </beans>  


这个时候,我们可以在浏览器直接访问success.jsp页面:

http://localhost:9000/SpringMVC/success

不用通过访问某个RequestMapping地址,返回到success页面,也不需要通过success.jsp的绝对路径访问。

通过这样直接转发页面的配置,不影响RequestMapping映射地址请求转发页面。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值