您的位置:首页 > 编程语言 > Java开发

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码

2015-06-30 16:22 656 查看

概述

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"; }
输入:http://localhost:8080/SpringMVCTest/test/index/1




带参数拼接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"; }
输入:http://localhost:8080/SpringMVCTest/test/index/2




解决中文乱码问题[/b]在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>

[/code]配置完成后,中文乱码解决掉了:





带参数不拼接参数跳转



/** * 带参数的重定向--不拼接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"; }



输入:http://localhost:8080/SpringMVCTest/test/index/3





其他

博客:http://www.cnblogs.com/ssslinppp http://www.cnblogs.com/ssslinppp 淘宝:http://shop110473970.taobao.com/?spm=a230r.7195193.1997079397.42.AvYpGW http://shop125186102.taobao.com/?spm=a1z10.1-c.0.0.SsuajD 参考文章: http://blog.sina.com.cn/s/blog_a85398ce0101f93x.html http://www.cnblogs.com/youngjoy/p/3919656.html

来自为知笔记(Wiz)

附件列表

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: