在提交页面提交信息,提交经拦截器拦截,后经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;
}}
在显示页面标题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;
}}
<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>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
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;
}}