悠然哈哈哈

导航

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)); 

 

来自:http://luanxiyuan.iteye.com/blog/1849169

posted on 2016-10-12 10:35  悠然886  阅读(381)  评论(0编辑  收藏  举报