Struts2表单中文乱码问题的解决

关于Struts2提交表单中文乱码的问题,在网上搜到了很多解决方法。但是这些方法还是没解决我的问题。
方法一:在struts.xml中加入
<constant name="struts.i18n.encoding" value="UTF-8"/>

方法二: 在方法一的基础上,在web.xml文件中加入Filter
	<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>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>

我也试过把上面两个文件以及JSP文件中的的 utf-8 改成 gbk,gb2312,结果对我的问题还是一点用处没有。

后来终于发现了,我用的strut2的Dispather是这样的
	
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

将 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 换成 org.apache.struts2.dispatcher.FilterDispatcher
, 如下。 终于问题解决!!!!!!!
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
    	<filter-name>struts2</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>





最后贴个 web.xml 和 struts.xml 的全文。注意,因为Struts2中默认的编码为UTF-8,所以JSP文件中使用的编码也要采用UTF-8, 如果改为GBK或者GB2312Z则会为乱码。

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>elvis</display-name>

    <!-- 使用Struts2 begin-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
    	<filter-name>struts2</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
	<!-- 使用Struts2 end-->
    
	<!-- 使用Spring Begin -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- 使用Spring End -->

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

struts.xml的全文:
<!DOCTYPE struts PUBLIC   
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 由Spring来实例化Struts Action -->
	<constant name="struts.objectFactory" value="spring" />
	
	<package name="news.action" extends="struts-default" namespace="/user">
		<action name="init" method="init" class="userAction">
			<result>/Save.jsp</result>
		</action>
		<action name="query" method="execute" class="userAction">
			<result>/User.jsp</result>
		</action>
		<action name="save" method="save" class="userAction">
			<result>/User.jsp</result>
		</action>
	</package>
</struts>

你可能感兴趣的:(apache,spring,jsp,xml,struts)