在SysUserController中getPageList方法获取中文参数的时候,对象sysUser里的trueName和userAccount是Unicode编码的,例如张三,对象里的数据是张三,但是在下面的request.getParameter获取的参数是正常,参数列表里的trueName情况也是一样;如果前端传过来的参数是乱码我都认为正常,但是为什么是Unicode编码的参数,已经找了很久,网上说的修改web.xml,加过滤器等能试的方法我都试过了,都不好使,请问是不是因为springmvc版本的问题或者我配置的问题;在进行form表单提交的时候也有同样的问题;
springmvc版本4.3.1,配置如下
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 编码过滤器 --> <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监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring和mybatis的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <display-name>Archetype Created Web Application</display-name> <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <!-- <context:component-scan base-package="com.controller" /> <context:component-scan base-package="com.service.impl" /> --> <context:component-scan base-package="com.dao" /> <context:component-scan base-package="com.controller"/> <context:component-scan base-package="com.service" /> <!-- <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> --> <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 --> <mvc:annotation-driven/> <context:annotation-config/> <!-- 静态资源处理 css js imgs --> <mvc:resources mapping="/static/**/**" location="/WEB-INF/static/" /> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 --> </list> </property> </bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /> <!-- 启用是为了推迟文件解析,以便捕获文件大小异常 --> <property name="resolveLazily" value="true"/> </bean> <!-- 配置ViewResolver 。可用多个ViewResolver 。使用order属性排序。 InternalResourceViewResolver 放在最后--> <!-- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1"></property> <property name="favorParameter" value="false" /> <property name="ignoreAcceptHeader" value="true" /> <property name="mediaTypes"> <map> 告诉视图解析器,返回的类型为json格式 <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <entry key="htm" value="text/htm" /> </map> </property> <property name="defaultViews"> <list> ModelAndView里的数据变成JSON <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> </list> </property> </bean> --> <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> <!-- 1.配置Spring MVC JSON序列化跳过null值 2.日期序列化为毫秒数 3.JSON缩进 --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" p:supportedMediaTypes="*/*"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="indentOutput" value="true" /> <property name="objectMapper"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="serializationInclusion"> <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> </property> </bean> </property> <property name="targetMethod" value="enable" /> <property name="arguments" value="WRITE_DATES_AS_TIMESTAMPS" /> </bean> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <value> json=application/json xml=application/xml *=*/* </value> </property> </bean> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <!-- i18n国际化开始 --> <!-- 默认语言设置为英文. 更改语言加参数 ?lang=en_US --> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang"></property> </bean> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="springmvc.interceptor.PojectInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieMaxAge" value="5184000"/> <!-- 2 months --> <property name="defaultLocale" value="en_US"/> <property name="cookieName" value="language" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:i18n/messages" /> <property name="defaultEncoding" value="UTF-8"/> </bean> <!-- i18n国际化结束 --> </beans>
sysUserList.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <%@ include file="/WEB-INF/view/common/common.jsp"%> </head> <body> <form id="searchForm" class="form-inline"> <div class="form-group"> <label for="exampleInputName2">userAccount</label> <input type="text" class="form-control" id="userAccount" name="userAccount" placeholder="userAccount"> </div> <div class="form-group"> <label for="exampleInputName2">trueName</label> <input type="text" class="form-control" id="trueName" name="trueName" placeholder="trueName"> </div> <button class="btn btn-default" type="button" onclick="query();">查询</button> </form> <div id="toolbar"> <button id="create" class="btn btn-info" onclick="create();">Create</button> <button id="Edit" class="btn btn-info">Edit</button> <button id="remove" class="btn btn-warning">Delete</button> </div> <table id="sysUserTable"></table> <div id="formDlg"></div> </body> </html> <script type="text/javascript"> var sysUserTable; var columns = [ { field: 'userId', width:'10%', checkbox: true }, { field:'userAccount', title:"帐号", width:'40%', align: 'center', valign: 'middle', sortable: true }, { field:'trueName', title:"姓名", width:'40%', align: 'center', valign: 'middle', sortable: true }, { field:'userStatus', title:"状态", width:'10%', align: 'center', valign: 'middle', sortable: true } ]; window.onload = function(){ var queryUrl = "${ctx}/sysUser/getPageList"; sysUserTable = $("#sysUserTable").bootstrapTable({ method: 'post', contentType: "application/x-www-form-urlencoded;charset=utf-8", toolbar: '#toolbar', //工具按钮用哪个容器 striped: true, //是否显示行间隔色 cache: false, //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*) pagination: true, //是否显示分页(*) sortable: false, //是否启用排序 sortOrder: "asc", //排序方式 pageNumber:1, //初始化加载第一页,默认第一页 pageSize: 10, //每页的记录行数(*) pageList: [10, 25, 50, 100], //可供选择的每页的行数(*) url: queryUrl,//这个接口需要处理bootstrap table传递的固定参数 queryParamsType:'', //默认值为 'limit' ,在默认情况下 传给服务端的参数为:offset,limit,sort // 设置为 '' 在这种情况下传给服务器的参数为:pageSize,pageNumber queryParams: queryParams,//前端调用服务时,会默认传递上边提到的参数,如果需要添加自定义参数,可以自定义一个函数返回请求参数 sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*) //search: true, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大 strictSearch: true, //showColumns: true, //是否显示所有的列 //showRefresh: true, //是否显示刷新按钮 minimumCountColumns: 2, //最少允许的列数 clickToSelect: true, //是否启用点击选中行 searchOnEnterKey: true, columns:columns, onLoadSuccess:function(){ }, onLoadError: function () { mif.showErrorMessageBox("数据加载失败!"); } }); } function queryParams(params) { var returnVar={ pageSize: params.pageSize, pageNumber: params.pageNumber, userAccount: $("#userAccount").val(), trueName: $("#trueName").val(), name: params.sortName, order: params.sortOrder } return returnVar; } function query(){ sysUserTable.bootstrapTable('refresh'); } function create(){ $("#formDlg").dialog({ iconCls:"icon-hamburg-drawings", title: "增加", width: 500, height: 340, href:'${ctx}/sysUser/add', maximizable:true, modal:true, buttons:[{ iconCls:"icon-save", text:"保存", handler:function(){ $.ajax({ type:"POST", dataType:"html", url:$("#mainForm").attr("action"), data:$("#mainForm").serialize(), success:function(data){ if(data.success){ alert(data.message); $("#formDlg").panel('close'); sysUserTable.bootstrapTable('refresh'); } } }); } },{ iconCls:"icon-cancel", text:"取消", handler:function(){ $("#formDlg").panel('close'); } }] }); } </script>
SysUserController.java
@RequestMapping(value="/getPageList",method=RequestMethod.POST)//produces = "text/html;charset=UTF-8", @ResponseBody public String getPageList(Page<SysUser> page,@ModelAttribute("sysUser") SysUser sysUser,String trueName,HttpServletRequest request) throws UnsupportedEncodingException{ sysUser.setTrueName(request.getParameter("trueName")); sysUser.setUserAccount(request.getParameter("userAccount")); Page<SysUser> pageUser = sysUserService.getPageUserList(page, sysUser); return getPageData(pageUser).toString(); } @RequestMapping(value="/list") public String list(){ return "system/sysUser/sysUserList"; }
我就不信有人会有耐心看这么长一串,希望你能把问题总结下。
看看数据库配置、连接数据库的类里面有没有加useUnicode\=true&characterEncoding\=utf-8,希望对你有帮助
请问这个问题最后是怎么处理的呢?如同出现同样的。各种处理都不行