我使用jeesite 做二次开发,使用httpClient转发请求或写方法,使用ajax请求后返回的中文是乱码,但是在js直接使用,在页面展示的又是正常的中文,这是什么原因,怎么解决?
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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring-mvc.xml 配置了
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<!-- 将Jackson2HttpMessageConverter的默认格式化输出为false -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list><value>application/json;charset=UTF-8</value></list>
</property>
<property name="prettyPrint" value="false"/>
<property name="objectMapper">
<bean class="com.thinkgem.jeesite.common.mapper.JsonMapper"></bean>
</property>
</bean>
<!-- 使用XML格式输出数据 -->
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="streamDriver">
<bean class="com.thoughtworks.xstream.io.xml.StaxDriver"/>
</property>
<property name="annotatedClasses">
<list>
<value>com.thinkgem.jeesite.common.persistence.BaseEntity</value>
<value>com.thinkgem.jeesite.common.supcan.treelist.TreeList</value>
<value>com.thinkgem.jeesite.common.supcan.treelist.cols.Col</value>
<value>com.thinkgem.jeesite.common.supcan.treelist.cols.Group</value>
</list>
</property>
</bean>
</constructor-arg>
<property name="supportedMediaTypes" value="application/xml"></property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
tomcat server.xml 也添加了
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
下面是一个java方法
@RequestMapping(value = "statistics/upload", method = RequestMethod.POST)
public String uploadStatistics(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
String id = (request.getParameter("id") == null) ? "" : request.getParameter("id");
String year = (request.getParameter("year") == null) ? "" : request.getParameter("year");
String month = (request.getParameter("month") == null) ? "" : request.getParameter("month");
List<String> result = new ArrayList<String>();
if ("".equals(id)) {
result.add("房东字段不能为空");
}
if ("".equals(year)) {
result.add("年份字段不能为空");
}
if ("".equals(month)) {
result.add("年份字段不能为空");
}
if (result.size() > 0) {
PrintWriter pw = response.getWriter();
pw.write("{\"ret\":1,\"msg\":\"" + StringUtils.join(result.toArray(), ",") + "\"}");
pw.flush();
pw.close();
} else {
String targerUrl = clientHelper.extraProcessing(request, "/rest/landlord/finance/operation/report/upload/" + id + "/"+ year + "/" + month + "/token", HttpClientHelper.SERVER_FD);
HttpProxy.request2(request, response, targerUrl, clientHelper.addParams(request));
}
return null;
}
ajax调用后截图
页面显示的又是正常的,如下图
不知道这个怎么解决,网上说的那些给方法@RequestMapping 添加参数 produces="text/html;charset=UTF-8;"
这些都试过了没用
不知道怎么解决