Struts中提交中文表单到ActionForm的乱码问题办理办法[Java编程]
本文“Struts中提交中文表单到ActionForm的乱码问题办理办法[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
由于Struts框架直接把表单数据发送给了ActionForm,所以这里面没有对HttpRequestServlet举行SetCharacterEncoding,所以默许是按照ISO-8859-1(拜见Tomcat 源代码中的org.apache.catalina.connector.HttpRequestBase中的protected void parseParameters()办法),办理的办法,就是在表单提交到ActionForm之前对request举行编码.
第一种办法,就是写一个过滤器,对全部恳求举行过滤
过滤器代码:
package jp.co.ricoh.gtis.others.profile.filters;
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 SetCharacterEncodingFilter implements Filter {
private String encoding;
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
this.encoding=filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding(this.encoding);
chain.doFilter(request,response);
}
public void destroy() {
// TODO Auto-generated method stub
}
}
配置文件web.xml
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>jp.co.ricoh.gtis.others.profile.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第二种办法是替换默许的掌握器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>*.testdo</url-pattern>
</servlet-mapping>
此例,但凡通过*.testdo来恳求的数据,城市经过参数encoding设定的值来编码.
以上是“Struts中提交中文表单到ActionForm的乱码问题办理办法[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |