struts2怎样可以解决乱码问题(比如怎么写个过滤器)

解决方案 »

  1.   

    <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>
    ...CharacterEncodingFilter </filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GB2312</param-value>
    </init-param>
    <init-param>
    <param-name>ignore</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    放在web.xml里,红色根据自己需求写,那个过滤类有开源包
      

  2.   

    你可以先写一个过虑类 
    package com.database;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;public class SetCharacterEncodingFilter implements Filter {
    public void destroy() {
    // TODO Auto-generated method stub

    } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
    arg0.setCharacterEncoding("GBK");
    arg2.doFilter(arg0,arg1);
    } public void init(FilterConfig arg0) throws ServletException { }
     
    }
    然后在Web.xml中配制
    <filter>
       <filter-name>encodingFilter</filter-name>
       <filter-class>com.database.SetCharacterEncodingFilter</filter-class>
      </filter>
      <filter-mapping>
       <filter-name>encodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
      </filter-mapping>
    如果这样不行我在给你写段例子;
    String lname=new String(request.getParameter("name").getBytes("ISO-8859-1"),"GB2312");
    这样应该没问题
    看的懂吗?
      

  3.   

    楼上的代码限制得太死,给你个可以在web.xml配置字符集的过滤器:
    public class SetCharacterEncodingFilter implements Filter {
        // ----------------------------------------------------- Instance Variables
        /**
         * The default character encoding to set for requests that pass through
         * this filter.
         */
        protected String encoding = null;
        /**
         * The filter configuration object we are associated with.  If this value
         * is null, this filter instance is not currently configured.
         */
        protected FilterConfig filterConfig = null;
        /**
         * Should a character encoding specified by the client be ignored?
         */
        protected boolean ignore = true;
        // --------------------------------------------------------- Public Methods
        /**
         * Take this filter out of service.
         */
        public void destroy() {        this.encoding = null;
            this.filterConfig = null;    }
        /**
         * Select and set (if specified) the character encoding to be used to
         * interpret request parameters for this request.
         *
         * @param request The servlet request we are processing
         * @param result The servlet response we are creating
         * @param chain The filter chain we are processing
         *
         * @exception IOException if an input/output error occurs
         * @exception ServletException if a servlet error occurs
         */
        public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain chain)
    throws IOException, ServletException {        // Conditionally select and set the character encoding to be used
            if (ignore || (request.getCharacterEncoding() == null)) {
                String encoding = selectEncoding(request);
                if (encoding != null)
                    request.setCharacterEncoding(encoding);
            } // Pass control on to the next filter
            chain.doFilter(request, response);    }
        /**
         * Place this filter into service.
         *
         * @param filterConfig The filter configuration object
         */
        public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
            this.encoding = filterConfig.getInitParameter("encoding");
            String value = filterConfig.getInitParameter("ignore");
            if (value == null)
                this.ignore = true;
            else if (value.equalsIgnoreCase("true"))
                this.ignore = true;
            else if (value.equalsIgnoreCase("yes"))
                this.ignore = true;
            else
                this.ignore = false;    }
        // ------------------------------------------------------ Protected Methods
        /**
         * Select an appropriate character encoding to be used, based on the
         * characteristics of the current request and/or filter initialization
         * parameters.  If no character encoding should be set, return
         * <code>null</code>.
         * <p>
         * The default implementation unconditionally returns the value configured
         * by the <strong>encoding</strong> initialization parameter for this
         * filter.
         *
         * @param request The servlet request we are processing
         */
        protected String selectEncoding(ServletRequest request) {        return (this.encoding);    }
    }web.xml添加这样的代码:<filter>
            <filter-name>Set Character Encoding</filter-name>
            <filter-class>filters.SetCharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>GBK</param-value>
            </init-param>
        </filter>
    <filter-mapping>
            <filter-name>Set Character Encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
      

  4.   

    楼上的过滤器我瞅着可真眼熟
    http://topic.csdn.net/u/20071122/11/87bbcbc6-949a-48cc-981b-3b3094fed3cc.html
      

  5.   

    struts2中所有文件都用UTF-8编码,使用struts2自带的filter,就不会出现乱码。 <filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.ActionContextCleanUp
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  6.   

    package com.smcc.uer.web.actions;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class SysRequestProcessor
    extends org.apache.struts.action.RequestProcessor {

    /**
     * 设置输入输出字符集
     */
    protected boolean processPreprocess(HttpServletRequest request,HttpServletResponse response) {

    try {
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html;charset=GBK");
    }
    catch (Exception e) {

    }

    return true;

    } }
    struts-config里面的配置:<controller
      contentType="text/html;charset=GBK"
      locale="true"
      nocache="true"
      processorClass="com.smcc.uer.web.actions.SysRequestProcessor"/>
      

  7.   

    在struts.xml中配置一下属性
    struts.i18n.encoding=UTF-8
      

  8.   

    在struts.xml中配置
    <constant name="struts.i18n.encoding value="GBK"/>
      

  9.   

    STRUTS2还是通过SERVLET来过滤挖?
      

  10.   

    public class CharacterEncodingFilter extends HttpServlet implements Filter {
        private FilterConfig filterConfig;
        //Handle the passed-in FilterConfig
        public void init(FilterConfig filterConfig) throws ServletException {
            this.filterConfig = filterConfig;
        }    //Process the request/response pair
        public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain filterChain) {
            try {
                //request.setCharacterEncoding("GBK");
                //response.setCharacterEncoding("GBK");
                request.setCharacterEncoding("utf-8");
                response.setCharacterEncoding("utf-8");
                filterChain.doFilter(request, response);
            } catch (ServletException sx) {
                filterConfig.getServletContext().log(sx.getMessage());
            } catch (IOException iox) {
                filterConfig.getServletContext().log(iox.getMessage());
            }
        }    //Clean up resources
        public void destroy() {
        }
    }
      

  11.   

     <filter>
        <filter-name>characterencodingfilter</filter-name>
        <filter-class>Filter.CharacterEncodingFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>characterencodingfilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      

  12.   

     楼上的兄的,在web.xml配置自己的过滤器(关于转码),那struts2自带的过滤器就用不了,有没有即用strust自带过滤器又用自己自配的过滤器吗??。
      

  13.   

    楼上说Servlet的骚年们,看清楚楼主说的是struts2啊 拜托,还什么dofilter?专业点行不行