首先贴出代码:
@RequestMapping(value = "total")
@ResponseBody
public Map<String, Object> getTotal(String keyword) {
int total = searchService.getTotal(keyword);
return MapResult.mapOK(total);
}
测试过转码的方法,
if(keyword!=null){
keyword = new String(keyword.getBytes("ISO-8859-1"),"utf-8");
} }
,web.xml也设置过了
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<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>
都没有达到效果。
如果参数是其他对象的话,对象中String类型值传递没问题。
楼主这里采用了另一种获取参数的方法解决了这个问题:
@RequestMapping(value = "total")
@ResponseBody
public Map<String, Object> getTotal(HttpServletRequest request) throws Exception {
String keyword = request.getParameter("keyword");
if(keyword!=null){
keyword = new String(keyword.getBytes("ISO-8859-1"),"UTF-8");
}
int total = searchService.getTotal(keyword);
return MapResult.mapOK(total);
}
问题完美解决了。
还有其他获取参数的方法,也可以尝试。