本人采用JSP+Struts开发一软件,在输入界面中如果输入中文“——”符号保存后,数据库保证正确,且查询结果显示正确,唯独单击直接在界面中不做任何修改再次保存后,Action接收到该项内容为“?D?D”,保存到数据库数据格式也是此类错误数据。目前只发现该符号有问题,其他中文字没问题。插图附件!

解决方案 »

  1.   

    接收数据时用String a=new String (s.getBytes("ISO-8859-1"),"GB2312");转化下.
      

  2.   

    JSP乱码解决请参考:
    http://blog.csdn.net/lip009/archive/2006/09/07/1192022.aspx
      

  3.   

    写个过滤器很容易解决中文乱码的问题,不用去每个String a=new String (s.getBytes("ISO-8859-1"),"GB2312");这样转换
    public class EncodingFilter implements Filter { public void destroy() {
    // TODO Auto-generated method stub

    } public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
         request.setCharacterEncoding("GBK");
         response.setCharacterEncoding("GBK");
         chain.doFilter(request, response);
    }
    public void init(FilterConfig arg0) throws ServletException {
    // TODO Auto-generated method stub

    }}
    然后在XML里面配置就好了
      

  4.   


    JSP页面传过来的值默认编码是ISO-8859-1
      

  5.   

    我已经加了过滤器。
    package com.common;import javax.servlet.*;
    import java.io.IOException;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); }}// EOC*******************web.xml****************************<filter-name>Set Character Encoding</filter-name>
    <filter-class>com.common.SetCharacterEncodingFilter</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><filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <servlet-name>action</servlet-name>
    </filter-mapping>
      

  6.   

    是你傳遞參數的問題,用下面這個方法處理一下~
    <%=java.net.URLEncoder.encode(str,"UTF-8")%>
      

  7.   

    如果是只有个别的中文乱码问题
    那就是GB2312字符集编码问题了,GB2312字符集只包含7000左右个常用中文字符
    这个时候就只能用GBK编码了,GBK包含常用的20000个中文字符.
    呵呵,我当时也是这犯了毛病,结果弄了一天才搞明白