两个文件。一个jsp文件,一个action文件。原先 jsp文件编码 utf-8 内容如下<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<form ...>
   <input type="text" name="name" class='validate-ajax-/user/check_nameExist.do?&what=&value;'/>
</form>
action内容简单,就是普通的action,内容如下public String nameExist() throws Exception{
     System.out.println(name.trim());  //这儿输出input中传递来的value值。 
     return "userExsitResult";
}
以上两个文件,其中jsp中的input 的 &what=&value,分别意思是取得当前input的名称,&what 就是取得当前是name,和取得当前name input匡内输入的值.&value能取得值。
比如我输入 csdntest,那么就代表validate-ajax-/user/check_nameExist.do?name=csdntest以上两个文件传递英文没问题。但传递中文,试了很久,利用<%=java.net.URLEncoder.encode(value,"UTF-8")%>或者其他,也许可能对于标点把握不是太好,或者其他原因。始终不能正确在action中得到正确的中文。
这应该属于url传递中文参数问题。不过我这儿存在一些标点符号,屡次使用不的方法。特来求救。<input type="text" name="name" class='validate-ajax-/user/check_nameExist.do?&what='+<%=java.net.URLEncoder.encode(value,"UTF-8")%>+';'/>  //运行就出错。

解决方案 »

  1.   


    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 {
        protected String encoding = null;
        protected FilterConfig filterConfig = null;
        protected boolean ignore = true;
        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
    [code=XML]
    <filter>
    <filter-name>setCharacterEncodingFilter</filter-name>
    <filter-class>
    ems.ssh.filter.SetCharacterEncodingFilter
    </filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    </filter>[/code]
      

  2.   

    添加个过滤器把。
    如上面,
           <filter-class>
                ems.ssh.filter.SetCharacterEncodingFilter
            </filter-class>
    其中 ems.ssh.filter是自己创建的包名。
      

  3.   


    郁闷阿。用了你的还是不行。仔细看了一下,大致跟我早过滤差不多。并且struts.xml内也配置了字符。现在详细贴出来。struts.xml内    <constant name="struts.i18n.encoding" value="UTF-8"/>web.xml内    <!-- 利用的spring的过滤器来设置编码方式 -->
    <filter>
    <filter-name>Spring character encoding filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>Spring character encoding filter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

      <filter>
       <filter-name>encoding</filter-name>
       <filter-class>com.nbchina.filter.EncodingFilter</filter-class>
       <init-param>
       <param-name>charset</param-name>
       <param-value>utf-8</param-value>
       </init-param>
      </filter> 
      <filter-mapping>
       <filter-name>encoding</filter-name>
       <url-pattern>*.do</url-pattern>
      </filter-mapping>
      <filter-mapping>
       <filter-name>encoding</filter-name>
       <url-pattern>*.jsp</url-pattern>
      </filter-mapping>  
      
    EncodingFilter.java内容简要如下public class EncodingFilter implements Filter {
    private String charset = null;
    public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp,
    FilterChain chain) throws IOException, ServletException {
    req.setCharacterEncoding(this.charset);
    chain.doFilter(req,resp); } public void init(FilterConfig config) throws ServletException { this.charset=config.getInitParameter("charset"); }}
    把jsp内还原如下<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <form ...>
       <input type="text" name="name" class='validate-ajax-/user/check_nameExist.do?&what=&value;'/>
    </form>莫非我设置过滤过多??
    现在传递的中文参数,比如 中文文字 ,在action中  
    System.out.println(name.trim());
    System.out.println(URLDecoder.decode(name.trim(),"UTF-8"));
    System.out.println(URLDecoder.decode(new String(name.trim()),"UTF-8"));输出的都是???????????????但英文却正常!
      

  4.   

    每个中文,比如  中  那么输出action则是???  三个问号
      

  5.   

    下掉web.xml内两个过滤。只保留struts.xml内的过滤。测试了,还是一样。 一个 中文,输出 三个问号!!!晕倒!
      

  6.   

    按照道理来说,struts.xml内 
    struts.xml内  <constant name="struts.i18n.encoding" value="UTF-8"/>即表示了req.setCharacterEncoding(“utf-8”);还需要为web.xml内配置过滤?
    晕倒。
      

  7.   

    tomcat->conf->server.xml
     <Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443" 
    URIEncoding="utf-8" 
    />这里是设置url传参的字符集
      

  8.   

    <input type="text" name="name" class='validate-ajax-/user/check_nameExist.do?&what='+<%=java.net.URLEncoder.encode(value,"UTF-8")%>+';'/> //运行就出错。
    这里就不用用这个方法,就直接写你原来的写法
      

  9.   

    十分感谢蛋疼兄。你的答案是对的。
    这个方法非常好,节约了大量代码。直接设置容器内url传参。也感谢closewbq兄的热情。上次一个排序问题帮忙的解决!谢谢。结贴,不过有兴趣的人可以在回答我最后一个问题。struts中已经设置了utf-8,有必要在web.xml内在设置使用spring的过滤utf-8或者自己写的filter utf-8么?会否冲突?或者无所谓。多多益善??
      

  10.   

    struts里设置了,就不用自己写filter了,哈哈!
    还有,不管你怎么设置字符集,配置也好,过滤器也罢,对get传参好像不起作用的!
    忘记了,以前做了很多的测试!