Springmvc中@RequestParam传值中文乱码解决方案
首先jsp表单里面有一些参数要传到controller中,通过以下方法接收:
- @RequestMapping(value="/orderPaper")
- public ModelAndView orderPaper(
- @RequestParam("paperName")String paperName,
- @RequestParam("openStartTime")String openStartTime,
- @RequestParam("openEndTime")String openEndTime ) {
- ModelAndView mv = new ModelAndView();
- mv.addObject("paperName", paperName); //这里面传过来的是中文,接收到的是一堆笑得很开心的的乱码
- mv.setViewName("/college/orderPaper");
- return mv;
- }
怎么处理呢?通过下面的工具类:
- public class EncodingTool {
- public static String encodeStr(String str) {
- try {
- return new String(str.getBytes("ISO-8859-1"), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
这样,只要把controller中改为
- mv.addObject("paperName",EncodingTool.encodeStr(paperName));
在网上有的说还需要在web.xml里面配置:
- <!-- 字符过滤器 -->
- <filter>
- <filter-name>encodingFilter</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>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
我试过,其实不配置也能把爱笑的乱码解决!
魔由心生,有万境纵横,无一道清静,无量寿佛!