Struts乱码解决办法

 

1.页面中文显示乱码

如果页面所有中文均显示乱码,则只要更改.jsp文件开头处为<%@page pageEncoding="GB2312"%>之类能显示中文的编码即可

2.中文参数显示乱码

此类问题是因为post或者get时引起.按以下步骤解决:

      a.修改%TOMCAT_HOME%/conf/server.xml,<connector/>标签对中加入URIEncoding="GB2312"属性

      b.增加filter,新建一个CharacterEncodingFilter,并继承servlet.filter接口,重构doFilter方法,

        增加如下代码:

        request.setCharacterEncoding ("GB2312");

        chain.doFilter (request,response);

     c.修改web.xml,重新布置CharacterEncodingFilter类后重启tomcat即可

3.直接转码


public static String isoToGB(String src)
{
    String strRet=null;
    try{
      strRet = new String(src.getBytes("ISO_8859_1"),"GB2312");
    }catch(Exception e)
    {
     
    }
    return strRet;
}

4.过滤filter设置

     创建过滤器:

    /**
 * 过滤JSP页面乱码
 */
package fly.bookStore.filter;

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.HttpServlet;

/**
 * @author liyuesheng
 *
 */
public class EncodingFilter extends HttpServlet implements Filter {

 protected String encoding = null;
 protected FilterConfig filterConfig = null;
 // 设置ignore参数,当其值为no或者false时,这个filter将不工作!
 protected boolean ignore = true;

 /**
  *
  */
 public EncodingFilter() {
  // TODO Auto-generated constructor stub
 }

 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
  *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
  */
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  // TODO Auto-generated method stub
  // 判断是否需要进行编码的设置
  if (!ignore || request.getCharacterEncoding() == null) {
   // 得到配置文件中的编码类型
   String encoding = setEncoding(request);
   if (encoding != null) {
    request.setCharacterEncoding(encoding);
    response.setCharacterEncoding(encoding);
   }
  }
  chain.doFilter(request, response);
 }

 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  */
 public void init(FilterConfig filterConfig) throws ServletException {
  // TODO Auto-generated method stubthis.filterConfig = filterConfig;
  this.filterConfig = filterConfig;
  this.encoding = filterConfig.getInitParameter("encoding");
  String value = filterConfig.getInitParameter("ignore");
  if (value == null) {
   this.ignore = false;
  } else if (value.equalsIgnoreCase("false")) {
   this.ignore = true;
  } else if (value.equalsIgnoreCase("no")) {
   this.ignore = true;
  } else {
   this.ignore = false;
  }

 }

 public void destroy() {
  this.encoding = null;
  this.filterConfig = null;
 }

 protected String setEncoding(ServletRequest request) {
  return this.encoding;
 }

}
   添加Web.xml配置

<filter>
     <filter-name>SetCharacterEncodingFilter</filter-name>
     <filter-class>fly.bookStore.filter.EncodingFilter</filter-class>
     <init-param>
     <param-name>encoding</param-name>
     <param-value>GBK</param-value>
     </init-param>
 </filter>
 <filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

5.变态方法

写一个myActionServlet来并覆盖ActionServlet中的process()方法。


  protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException {
    /**@todo Override this org.apache.struts.action.ActionServlet method*/
    request.setCharacterEncoding("GB2312");//
就加着一行一切都解决了
    super.process(request, response);
  }

当然别忘了改一下web.xml里面的配置
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>strutsdemo.myActionServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

改一下servlet-class标签中的内容就可以!

真的可以,一劳用yi

具体编码的理论就不说了,google上已经够多了。

另外,如果不用struts的话,hibernate也可能碰到中文乱码问题,只要在hibernate.cfg.xml配置中如下:

<property name="hibernate.connection.url">
   jdbc:microsoft:sqlserver://Localhost:1433;SelectMethod=cursor;characterEncoding=GBK;DatabaseName=myDatabase.
  </property>

characterEncoding=GBK!就可以了。



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值