一、是前台发送到后台时就出现乱码
1、确保项目编码格式为utf-8
2、确保所有前台页面的编码格式为utf-8
3、确保jsp编码pageEncoding="UTF-8"
4、前台提交到后台,如果不加method属性方法,则默认为get。所以需要确定输入的方法为method="post"。
通过上面检查和提交方法的确认一般可确定发送到后台的中文无乱码,可输出日志查询检验。
二、后台读取数据返回到前台出现乱码
1、在struts中配置拦截器
package sun.interceptor;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class EncodingInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext actionContext = actionInvocation.getInvocationContext();
ServletActionContext.getResponse().setCharacterEncoding("utf-8");
ServletActionContext.getRequest().setCharacterEncoding("utf-8");
return actionInvocation.invoke();
}
}
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
struts2
*.action