由于总是碰到不同的传参方式,大部分又都会出现中文乱码现象,我把我最近碰到的各种情况总合到一起,每发现一种就记录一下,以备不时之需
1、 get方法提交
js:
$("#codeSave").click(function(){
$.get("codemapingSave",
{
ccode:$('#yy_code_id').val(),
ccodeName:encodeURI($('#yy_code').val()),
oaoafundcategoryid:$('#ds_code_id').val(),
oafundcategory:encodeURI($('#ds_code').val())
},
function(data,status){
//alert("数据:" + data + "\n状态:" + status);
window.location.reload();
});
});
action:
@RequestMapping("/codemapingSave")
@ResponseBody
public String codeInitMapping(@ModelParam TU8CodeMapping mapping, HttpServletRequest req) throws Exception {
//解决字符乱码
String ccodeName = URLDecoder.decode(req.getParameter("ccodeName"),"UTF-8");
String oafundcategory = URLDecoder.decode(req.getParameter("oafundcategory"),"UTF-8");
mapping.setCcodeName(ccodeName);
mapping.setOafundcategory(oafundcategory);
return dataInitService.codeInitMapping(mapping);
}
2、这个直接用“request.setCharacterEncoding("UTF-8");” 获取到的参数没有乱码...
@RequestMapping(value="/screenAccvouchList",method = RequestMethod.POST)
public String screenAccvouchList(Model model, HttpServletRequest request) throws UnsupportedEncodingException{
request.setCharacterEncoding("UTF-8");
String no = request.getParameter("no"); //单号
Systrm.out.println(no);
return "list";
}
3、
url: 192.168.1.243:8081/tec_oa/selectPayHint.do?code=ACCOUNTNUMBER&term=中国
action:
public String selectPayHintNameByCode(){
//url传值乱码
byte[] buf;
try {
buf = term.getBytes("ISO8859-1");
term = new String(buf,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Systrm.out.println(term);
return "success"; }
还有在jsp 添加上<%@ page contentType="text/html;charset=UTF-8"%>;
在eclipse或者其他编程软件上设置默认编码为UTF-8;
tomcat 上设置默认编码等等
暂时用到的就是这几种,都亲测有效,网上还有很多解决传参中文乱码的方法,以后用到了测试后会补充进来
这些代码都是我从自己项目里cope的,如有雷同,纯属巧合