概述
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
不带参数跳转
/**
* 不带参数的重定向
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/myRedirectWithoutArgs")
public String myRedirectWithoutArgs(ModelMap mmMap) throws Exception {
System.out.println("在myRedirectWithoutArgs()方法内...");
mmMap.addAttribute("msg", "不带参数的重定向");
return "index";
} 带参数拼接url形式跳转
/**
* 带参数的重定向--拼接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";
} 解决中文乱码问题
在web.xml中加入如下配置
<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>
配置完成后,中文乱码解决掉了:
带参数不拼接参数跳转
/**
* 带参数的重定向--不拼接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";
} 其他
参考文章: