您的位置:首页 > 编程语言 > Java开发

Struts1.X 和 Struts2.X中文乱码解决方法

2012-12-31 18:48 288 查看
由于Struts框架直接把表单数据发送给了ActionForm,所以这里面没有对HttpRequestServlet进行SetCharacterEncoding,所以默认是按照ISO-8859-1(参见Tomcat 源代码中的org.apache.catalina.connector.HttpRequestBase中的protected void parseParameters()方法),解决的方法,就是在表单提交到ActionForm之前对request进行编码(Struts1.x
与 Struts2.X 处理方式不同)。

1.Struts1.x 中:

第一种方法,就是写一个过滤器,对所有请求进行过滤,过滤器代码如下:

package com.ssh.common.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

public class FilterEncoding implements Filter {

protected String encoding; // 接收字符编码

protected boolean ignore; // 是否忽略大小写

protected FilterConfig filterConfig; // 初始化配置

public void init(FilterConfig filterConfig) throws ServletException {

// 从web.xml文件中读取encoding的值

encoding = filterConfig.getInitParameter("encoding");

// 从web.xml文件中读取ignore的值

String value = filterConfig.getInitParameter("ignore");

// 以下三种情况均为忽略大小写

if(value == null) {

ignore = true;

} else if(value.equalsIgnoreCase("yes")) {

ignore = true;

} else if(value.equalsIgnoreCase("true")) {

ignore = true;

}

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

if(ignore || request.getCharacterEncoding() == null) {

// 如果为空先从web.xml中得到

String encoding = selectEncoding(request);

if(encoding != null) {

// 设置字符集编码

request.setCharacterEncoding(encoding);

}

}

// 继续执行

chain.doFilter(request, response);

}

// 得到字符编码

private String selectEncoding(ServletRequest request) {

return encoding;

}

public void destroy() {

}

}

web.xml配置:

<filter>

<filter-name>FilterEncoding</filter-name>

<filter-class>com.ssh.common.filter.FilterEncoding</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>FilterEncoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

此例,所有的请求的数据,都会经过参数encoding设定的值来编码。具体编码方式根据页面编码进行设置,如果所有页面使用UTF-8就使用UFT-8,如果GBK,就设置GBK。

第二种方法是替换默认的控制器org.apache.struts.action.ActionServlet子类代码:

package jp.co.ricoh.gtis.others.profile.controllers;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionServlet;

public class SetEncodingActionServlet extends ActionServlet {

protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

// TODO Auto-generated method stub

String encoding = getInitParameter("encoding");

request.setCharacterEncoding(encoding);

super.process(request, response);

}

}

配置文件web.xml

<servlet>

<servlet-name>testAction</servlet-name>

<servlet-class>jp.co.ricoh.gtis.others.profile.controllers.SetEncodingActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>testAction</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>
</servlet>

此例,凡是通过*.do来请求的数据,都会经过参数encoding设定的值来编码。具体编码方式根据页面编码进行设置,如果所有页面使用UTF-8就使用UFT-8,如果GBK,就设置GBK。

2.Struts2.X 中:

第一种方法,修改Struts2核心架包struts2-core-2.x.x.jar中default.properties文件的struts.i18n.encoding属性:

在struts2-core-2.x.x.jar包中路径为struts2-core-2.0.6\org\apache \struts2有一个default.properties 文件,把struts.i18n.encoding=UTF-8改为struts.i18n.encoding=GBK,或者反过来,具体编码方式根据页面编码进行设置,如果所有页面使用UTF-8就使用UFT-8,如果GBK,就设置GBK。

第二种方法,修改Struts2的默认属性配置文件struts.properties中的struts.i18n.encoding属性:

在struts.properties文件中设置struts.i18n.encoding=GBK或者struts.i18n.encoding=UTF-8,具体编码方式根据页面编码进行设置,如果所有页面使用UTF-8就使用UFT-8,如果GBK,就设置GBK。

第三种方法,在struts.xml文件内添加struts.i18n.encoding常量属性配置:

<constant name="struts.i18n.encoding" value="GBK"/>或者<constant name="struts.i18n.encoding"
value="UTF-8"/>具体编码方式根据页面编码进行设置,如果所有页面使用UTF-8就使用UFT-8,如果GBK,就设置GBK。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: