日期:2014-05-16  浏览次数:20376 次

jsp页面乱码或者传参乱码解决,我的终结版

?jsp页面乱码或者传参乱码解决,我的终结版:

请首先大致浏览下文章再在代码里做修改,省的添加一堆无用的代码。

?

?

? ?1、jsp页面

? ?页面头添加:

? ?<%@ page language="java" contentType="text/html; charset=UTF-8" ?pageEncoding="UTF-8"%>

? ?<%request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");%>

?

? ?<head>里添加:

? ?<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

?

? ? 右键jsp页面的属性,确保其编码为utf-8

? ?2、servlet或action

? ?方法头添加

? ?request.setCharacterEncoding("UTF-8");

? ?response.setCharacterEncoding("UTF-8");

? ?3、工程

? ?工程右键-确保工程编码为utf-8

? ?4、用到了spring

? ?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配置dispatcherServlet的那个xml文件添加:

? <!--必须有,@responsebody默认滴是那个什么8859-1滴狗屁编码,需要在配置文件中加入如下代码转换-->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > ?

<property name="messageConverters"> ??

? ? ? ? <list> ??

? ? ? ? ? ? <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> ??

? ? ? ? ? ? ? ?<property name = "supportedMediaTypes">

? ? ? ? ? ? ? ? ? ? ?<list>

? ? ? ? ? ? ? ? ? ? ? ? ?<value>text/html;charset=UTF-8</value> ??

? ? ? ? ? ? ? ? ? ? </list> ??

? ? ? ? ? ? ? ?</property> ??

? ? ? ? ? ? </bean> ??

? ? ? ? </list> ??

? </property> ?

</bean>?

?

?

5、服务器,其它服务器同理

tomcat的servlet.xml配置文件里添加utf-8编码:

<Connector port="8080" protocol="HTTP/1.1"?

? ? ? ? ? ? ? ?connectionTimeout="20000"?

? ? ? ? ? ? ? ?redirectPort="8443" URIEncoding="UTF-8"/>

如果用eclipse集成的tomcat请在包资源管理器里找Servers里找tomcat添加。

? ? ? ? ?

?

?

要是还有乱码,全删了重搞?