日期:2014-05-17  浏览次数:20711 次

中文显示乱码
在提交页面提交信息,提交经拦截器拦截,后经action处理后
在显示页面标题title和内容content显示时数字符号英文显示正常,中文显示乱码



为什么这样
这么解决呢?


改了显示页面的编码方式为utf-8也还是一样
望大虾帮忙

struts2 处理的


提交页为:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
<head></head>
<body>
<center>
 网友评论<br>
 <s:form action="public" method="post">
  <s:textfield name="title" label="标题" maxLength="20"></s:textfield>
  <s:textarea name="content" cols="40" rows="5" label="内容"></s:textarea>
  <s:submit value="提交"></s:submit>
 </s:form>
</center>
</body>
</html>

显示页面为:


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
  <head>
  <title></title>
  </head>
  <body>
  标题:<s:property value="title"/><br>
  内容:<s:property value="content"/>
  </body>
</html>

拦截器拦为:

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor {

/**

*/
private static final long serialVersionUID = -8506473737334781571L;

public String intercept(ActionInvocation ai) throws Exception {
// TODO Auto-generated method stub

Object object=ai.getAction();
if(object != null)
{
if(object instanceof PublicAction)
{
PublicAction action=(PublicAction)object;
String content=action.getContent();
if(content.contains("不"))
{
content=content.replace("不", "*");

action.setContent(content);

}
return ai.invoke();

}else{return Action.LOGIN;}
}else{return Action.LOGIN;}
}

}

处理action为:

package cn.g.action;

import com.opensymphony.xwork2.ActionSupport;

public class PublicAction extends ActionSupport {

/**

*/
private static final long serialVersionUID = -8372785893649843388L;

private String title;
private String content;

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}

public String execute(){
return SUCCESS;
}

}





------解决方案--------------------
jsp页面都用utf-8

web.xml配置文件里加上
Java code

    <filter>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>
            org.jb.common.filter.SetCharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SetCharacterEncodingF