办法1:?
对于处理 Struts Framework 中文编码问题有多种方式,在此我们尝试继承 ActionServlet 类并覆盖 process 方法,于子类process 方法中设定请求编码。由此展现ActionServlet 扮演前端Controller 之特质。?
package myown.struts;?
import javax.servlet.http.HttpServletRequest;?
import javax.servlet.http.HttpServletResponse;?
import org.apache.struts.action.ActionServlet;?
public class ActionServletUTF8Encoding extends ActionServlet {?
protected void process(HttpServletRequest request,HttpServletResponse response)?
throws java.io.IOException, javax.servlet.ServletException {?
//如果JSP用GB2312?
request.setCharacterEncoding("GB2312");?
super.process(request, response);?
}?
}?
然后,在web.xml中,做如下设定即可:?
<servlet-name>action </servlet-name>?
<servlet-class> myown.struts.ActionServletUTF8Encoding </servlet-class>?
办法2:?
更改文件\org\apache\struts\util\PropertyMessageResources.java?
将第289行的:messages.put(messageKey(localeKey, key),props.getProperty(key));?
改成下面的代码:?
String sProps = props.getProperty(key);?
try?
{?
sProps = new String(sProps.getBytes("ISO-8859-1"),"GB2312");?
}?
catch (Exception e)?
{?
e.printStackTrace();?
}?
messages.put(messageKey(localeKey, key),sProps);?
编译后替换原有STRUTS.JAR中的PropertyMessageResources类?
办法3:?
在工程中加入2个类filters.RequestDumperFilter、filters.SetCharacterEncodingFilter,修改WEB.XML代码如下:?
<filter>?
<filter-name>Set Character Encoding </filter-name>?
<filter-class>filters.SetCharacterEncodingFilter </filter-class>?
<init-param>?
<param-name>encoding </param-name>?
<param-value>GB2312 </param-value>?
</init-param>?
<init-param>?
<param-name>ignore </param-name>?
<param-value>true </param-value>?
</init-param>?
</filter>?
<filter-mapping>?
<filter-name>Set Character Encoding </filter-name>?
<url-pattern>*.do,/* </url-pattern>?
</filter-mapping>?
/**?
*SetCharacterEncodingFilter 代码如下?
*/?
package filters;?
import javax.servlet.*;?
import java.io.IOException;?
public class SetCharacterEncodingFilter implements Filter {?
protected String encoding = null;?
protected FilterConfig filterConfig = null;?
protected boolean ignore = true;?
public void destroy() {?
this.encoding = null;?
this.filterConfig = null;?
}?
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)?
throws IOException, ServletException {?
if (ignore || (request.getCharacterEncoding() == null)) {?
String encoding = selectEncoding(request);?
if (encoding != null)?
request.setCharacterEncoding(encoding);?
}?
chain.doFilter(request, response);?
}?
public void init(FilterConfig filterConfig) throws ServletException {?
this.filterConfig = filterConfig;?
this.encoding = filterConfig.getInitParameter("encoding");?
String value = filterConfig.getInitParameter("ignore");?
if (value == null) this.ignore = true;?
else if (value.equalsIgnoreCase("true")) this.ignore = true;?
else if (value.equalsIgnoreCase("yes")) this.ignore = true;?
else this.ignore = false;?
}?
protected String selectEncoding(ServletRequest request) {?
return (this.encoding);?
}?
}?
详细解决方案
struts中文编码解决方法(三种)
热度:91 发布时间:2012-09-05 15:19:34.0
相关解决方案
- IE不支持JQuery吗?解决方法
- struts HTTP Status 500解决办法
- boa+cgi+sqlite3【基于web的数据采集并动态显示】解决方法
- Struts-config.xml配置action-地图pings元素略计
- cgi提取文件内容?解决方法
- Struts-config.xml配置文件《action-地图pings》元素的详解
- java.lang.NoSuchMethodException: setId([Ljava.lang.String;)解决方法
- 404,500跳转到登录页面,疏失:The Struts dispatcher cannot be found
- java struts 文件上载
- struts hibernate 测试小例子 不报错 但数据库没反应 求分析项目在小弟我的资源里面
- 求SSM分页 struts +spring+mybatis 给小弟我发个学习学习吧 多谢大神们
- 关于 jsp struts-tag <c:if> 判断有关问题
- struts+spring+mybatis出现错误(java.lang.ClassNotFoundException: Entity)为提示位置
- 求解 struts+spring+mybatis sqlsession为空 debug发现没有执行set方法 检查配置好像没有关问题
- s标签 图片src 当action 解析?解决方法
- CAPTCHA 模块中 ajax form 导致CAPTCHA session reuse attack detected异常,解决方法
- struts + hibernate 后台页面修改数据后,前台页面显示的内容新旧数据交替出现。请高手帮忙分析上原因
- java编程思想.解决方法
- struts+spring+hibernate 开发,在action层调用service层接口,结果替null 注入失败,需高手帮忙
- SSH框架导入struts,hibernate,spring分先后顺序吗>>解决方法
- 有个有关问题,请各位帮忙看看:java.lang.ClassNotFoundException: org.apache.struts.action.Act
- struts.xml资料中的namespace"/"当在/后加东西时没起作用
- 生手求教,dwr+struts+spring 页面跳提示框error
- struts checkbox 页面显示
- Servlet struts is not available要如何解决
- struts+hibernate数据库链接池有关问题
- 关于遮罩层的。解决方法
- JSP运行异常 STRUTS + SQL SERVER 2005
- oracle 最近的时间(而不是前一天的时间) 跪求sql语句 。解决方法
- web service No services have been found.解决方法