概述
spring MVC框架controller间跳转,需重定向,主要有如下三种:
- 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedirectWithArgs
- 带参数拼接url形式跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedirectWithArgs?username="zhangsan"&..
- 带参数不拼接参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedirectWithArgs,但是可以传参;
前台index.jsp
![](https://images0.cnblogs.com/blog/731047/201507/011016137744346.png)
不带参数跳转
![](https://images0.cnblogs.com/blog/731047/201507/011016153682060.png)
/**
* 不带参数的重定向
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/myRedirectWithoutArgs")
public String myRedirectWithoutArgs(ModelMap mmMap) throws Exception {
System.out.println("在myRedirectWithoutArgs()方法内...");
mmMap.addAttribute("msg", "不带参数的重定向");
return "index";
} ![](https://images0.cnblogs.com/blog/731047/201507/011016158064758.png)
![](https://images0.cnblogs.com/blog/731047/201507/011016161813944.png)
带参数拼接url形式跳转
![](https://images0.cnblogs.com/blog/731047/201507/011016171031029.png)
/**
* 带参数的重定向--拼接URL
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/myRedirectWithArgsURL")
public String myRedirectWithArgsURL(ModelMap mmMap, Person p)
throws Exception {
System.out.println("在myRedirectWithArgsURL()方法内...");
System.out.println("参数为:" + p.getUsername() + p.getPasswd());
mmMap.addAttribute("msg",
"带参数的重定向,参数为==>" + p.getUsername() + p.getPasswd());
return "index";
} ![](https://images0.cnblogs.com/blog/731047/201507/011016176963915.png)
![](https://images0.cnblogs.com/blog/731047/201507/011016180247114.png)
解决中文乱码问题
在web.xml中加入如下配置
![](https://images0.cnblogs.com/blog/731047/201507/011016186181000.png)
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置完成后,中文乱码解决掉了:
![](https://images0.cnblogs.com/blog/731047/201507/011016189785956.png)
![](https://images0.cnblogs.com/blog/731047/201507/011016191816427.png)
带参数不拼接参数跳转
![](https://images0.cnblogs.com/blog/731047/201507/011016195243856.png)
/**
* 带参数的重定向--不拼接URL
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/myRedirectWithArgs")
public String myRedirectWithArgs(ModelMap mmMap, HttpServletRequest request)
throws Exception {
System.out.println("在myRedirectWithArgs()方法内...");
Map<String, ?> map = RequestContextUtils.getInputFlashMap(request);
System.out.println((String)map.get("username")+map.get("passwd"));
mmMap.addAttribute("msg", "带参数的重定向,不拼接URL");
return "index";
} ![](https://images0.cnblogs.com/blog/731047/201507/011016202286241.png)
![](https://images0.cnblogs.com/blog/731047/201507/011016208065897.png)
![](https://images0.cnblogs.com/blog/731047/201507/011016214311540.png)
其他
参考文章: