Struts中文的乱码经典解决方法
2012-10-08 17:12
218 查看
初学Struts的人一定遇到过中文的乱码问题 ,目前解决的方法有很多。我这有两种经典的方法和大家分享。
1,重写 RequestProcessor 这是最简单的方法。
public class MyRequestProcessor extends RequestProcessor {
@Override
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding(“UTF-8”); //在这加一行设置编码的的代吗。
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.processPreprocess(request, response);
}
}
页面的编码也要保持一致
<%@ page language=“java” contentType=“text/html; charset=UTF-8”%>
另外在struts-config.xml 中配置 <controller> 元素
<controller processorClass=“com.cao.struts.MyRequestProcessor”/>
不过该方法只对 post 请求有作用
如果对get 方法也有作用processPreprocess 方法写成
if( request.getMethod()。compareToIgnoreCase(“post”)>=0){
try {
request.setCharacterEncoding(“UTF-8”);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Iterator iter=request.getParameterMap()。values()。iterator();
while(iter.hasNext())
{
String[] parames=(String[])iter.next();
for (int i = 0; i < parames.length; i++) {
try {
parames[i]=new String(parames[i].getBytes(“iso8859-1”),“UTF-8”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} } } }
2,通过过滤器使用请求代理
public class EncodingFilter extends javax.servlet.http.HttpServlet implements Filter {
FilterConfig filterConfig;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
MyRequest req = new MyRequest( (HttpServletRequest)request);//设置代理请求
filterChain.doFilter(req, response);
}
public void init(FilterConfig arg0) throws ServletException {
this.filterConfig = arg0;
}
}
class MyRequest extends javax.servlet.http.HttpServletRequestWrapper{//自定义的代理请求类
HttpServletRequest request;
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
public String[] getParameterValues(String name){
String strs[] = super.getParameterValues(name);
for(int i=0;i<strs.length;i++){
strs[i] = this.myEncoding(strs[i]);
}
return strs;
}
private String myEncoding(String input){
String output =“”;
try {
output = new String(input.getBytes(“ISO-8859-1”),“UTF-8”); //注意同页面的编码保持一致
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
}
}
在web.xml中进行配置
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.cao.struts.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
该方法既可以用于post请求又可以用于get请求。
希望对大家有所帮助!
1,重写 RequestProcessor 这是最简单的方法。
public class MyRequestProcessor extends RequestProcessor {
@Override
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding(“UTF-8”); //在这加一行设置编码的的代吗。
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.processPreprocess(request, response);
}
}
页面的编码也要保持一致
<%@ page language=“java” contentType=“text/html; charset=UTF-8”%>
另外在struts-config.xml 中配置 <controller> 元素
<controller processorClass=“com.cao.struts.MyRequestProcessor”/>
不过该方法只对 post 请求有作用
如果对get 方法也有作用processPreprocess 方法写成
if( request.getMethod()。compareToIgnoreCase(“post”)>=0){
try {
request.setCharacterEncoding(“UTF-8”);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Iterator iter=request.getParameterMap()。values()。iterator();
while(iter.hasNext())
{
String[] parames=(String[])iter.next();
for (int i = 0; i < parames.length; i++) {
try {
parames[i]=new String(parames[i].getBytes(“iso8859-1”),“UTF-8”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} } } }
2,通过过滤器使用请求代理
public class EncodingFilter extends javax.servlet.http.HttpServlet implements Filter {
FilterConfig filterConfig;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
MyRequest req = new MyRequest( (HttpServletRequest)request);//设置代理请求
filterChain.doFilter(req, response);
}
public void init(FilterConfig arg0) throws ServletException {
this.filterConfig = arg0;
}
}
class MyRequest extends javax.servlet.http.HttpServletRequestWrapper{//自定义的代理请求类
HttpServletRequest request;
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
public String[] getParameterValues(String name){
String strs[] = super.getParameterValues(name);
for(int i=0;i<strs.length;i++){
strs[i] = this.myEncoding(strs[i]);
}
return strs;
}
private String myEncoding(String input){
String output =“”;
try {
output = new String(input.getBytes(“ISO-8859-1”),“UTF-8”); //注意同页面的编码保持一致
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
}
}
在web.xml中进行配置
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.cao.struts.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
该方法既可以用于post请求又可以用于get请求。
希望对大家有所帮助!
相关文章推荐
- struts中文乱码解决方法详解
- spring 解决中文乱码问题 方法超经典
- struts中文乱码解决方法
- struts中文乱码解决方法
- Struts中文乱码解决实例(绝对经典)
- struts中文乱码解决方法
- Struts2.1.8 JSP提交Action后,获取中文乱码解决方法
- struts中文乱码解决方法
- 解决Struts中文乱码的方法
- 在struts2种解决url中文乱码的方法(其中一种方法)
- linux+tomcat struts 2 Jsp页面传值中文乱码问题解决方法
- JSP中用 javabean传递中文乱码的解决方法
- iPhone ZBar库 中文乱码解决方法--重新编译libzbar.a 请勿转载
- 解决javaWEB中前台传数据到后台中文乱码问题的3种方法
- Java Swing中文乱码解决方法
- 【转】get方法中文乱码解决
- Mysql中插入不了中文或者乱码的解决方法
- 基于tomcat的spring开发中文乱码解决方法。
- CCS6中文件出现中文乱码问题解决方法
- 树莓派 中文乱码 解决方法