Struts1 中提交中文表单到ActionForm后出现乱码问题的原因及处理方法

时间:2022-08-22 07:48:40

问题:

在你所有页面设置的编码都是utf-8的情况下仍然会出现中文乱码现象。

一、乱码出现原因

由于Struts框架直接把表单数据发送给了ActionForm,所以这里面没有对HttpRequestServlet进行SetCharacterEncoding,所以默认是按照ISO-8859-1。

二、解决办法

方法 : 就是在表单提交到ActionForm之前对request进行编码,写一个过滤器,对所有请求进行过滤

方式一:直接在过滤器中指定编码格式

MyFilter.java

 public class MyFilter extends HttpServlet implements Filter {
private static final long serialVersionUID = 1L; public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws java.io.IOException, javax.servlet.ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("utf-8"); //设置接收编码
arg1.setContentType("text/html;charset=utf-8");
arg2.doFilter(arg0, arg1); //必须 } public void init(FilterConfig arg0) throws javax.servlet.ServletException {
// TODO Auto-generated method stub
} }

在web.xml中添加

 <filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.xidian.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

方式二:在web.xml中指定编码格式

MyFilter.java

 package com.xidian.filter;

 import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; public class MyFilter extends HttpServlet implements Filter {
private static final long serialVersionUID = 1L;
private String encoding; public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws java.io.IOException, javax.servlet.ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding(encoding);
arg2.doFilter(arg0, arg1);
} public void init(FilterConfig arg0) throws javax.servlet.ServletException {
// TODO Auto-generated method stub
encoding=arg0.getInitParameter("encoding");
} }

在web.xml添加

  <filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.xidian.filter.MyFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

下面是struts2中中文乱码的问题:

关于struts2中表单提交时,中文乱码问题的解决

顺带再说下加上对输入数据内容的过滤的情况:

再添加一个过滤程序:

SafeFilter.java

 package com.xidian.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 SafeFilter implements Filter {
private String keywords[]; public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//这里我们接收到用户名
String name=request.getParameter("name");
request.setAttribute("err", "您不允许登录<font color='red'>"+name+"</font>");
System.out.println("第二个过滤器:"+name);
if(name!=null){
//判断
for(String key:keywords){
if(key.equals(name)){
request.getRequestDispatcher("WEB-INF/ifo.jsp").forward(request, response);
return;
}
} }
chain.doFilter(request, response);
} public void init(FilterConfig filterConfig) throws ServletException {
keywords=filterConfig.getInitParameter("keywords").split(";"); } }

web.xml的配置

 <!-- 部署过滤器的顺序就是执行的顺序 -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.xidian.filter.MyFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter> <filter>
<filter-name>SafeFilter</filter-name>
<filter-class>com.xidian.filter.SafeFilter</filter-class>
<init-param>
<param-name>keywords</param-name>
<param-value>拉登;小明;凤姐</param-value>
</init-param>
</filter>