当前位置: 代码迷 >> Web前端 >> struts中文编码解决方法(三种)
  详细解决方案

struts中文编码解决方法(三种)

热度:91   发布时间:2012-09-05 15:19:34.0
struts中文编码解决办法(三种)

办法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);?
}?
}?

  相关解决方案