在提交页面提交信息,提交经拦截器拦截,后经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;
}}

解决方案 »

  1.   

    jsp页面都用utf-8web.xml配置文件里加上 <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>SetCharacterEncodingFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    </filter-mapping>
      

  2.   

    前台jsp,后台action,数据库编码这些都同一为utf-8,应该没问题了
      

  3.   

    在jsp中制定
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
      

  4.   

    http://topic.csdn.net/u/20120822/22/c60adffa-acab-4c5a-a262-40e65ed6dcd4.html?seed=2094767989&r=79481081#r_79481081
      

  5.   

    过滤器代码:
    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;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class Filter1 implements Filter {
    private FilterConfig fg;
    public void destroy() {
    System.out.println("销毁...");
    } public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    System.out.println("filter1....");
    String charset = fg.getInitParameter("charset");
    System.out.println(charset);
    if(request instanceof HttpServletRequest && response instanceof HttpServletResponse){
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse)response;
    req.setCharacterEncoding(charset);
    resp.setCharacterEncoding(charset);
    resp.setContentType("text/html;charset=UTF-8");
    chain.doFilter(req, resp);
    }
    } public void init(FilterConfig filterConfig) throws ServletException {
    System.out.println("初始化...");
    this.fg = filterConfig;
    }}
      

  6.   

    界面和数据库都设成gbk 或utf-8 就可以
      

  7.   

    jsp页面都用utf-8就解决了