我用Eclipse创建了个web project 。
就一个简单的页面,一个文本框输入数据的(我的目的是想测试中文乱码),没有Hibernate。只有Struts。页面上的中文没有问题,都能正确显示,但是一旦提交,我在Action那边,用控制台输出的就都是乱码。
求高手指点一下迷津,小弟感激不尽
下面是代码

解决方案 »

  1.   

    在action中加上这个 request.setCharacterEncoding("GBK");如果还不行就做过过滤器,网上很多,Google一下
      

  2.   

    下面是jsp 代码:<%@ page language="java" pageEncoding="utf-8"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
    <%
    request.setCharacterEncoding("utf-8"); 
    %>
    <html> 
    <head>
    <title>JSP for TtForm form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <html:form action="/tt" method="post"> 
    <br>中文 : <html:text property="china"/><html:errors property="china"/><br/>
    <html:submit/><html:cancel/>
    </html:form>
    </body>
    </html>下面是Action代码:public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    TtForm ttForm = (TtForm) form;// TODO Auto-generated method stub
    String test = ttForm.getChina();

    System.out.println(" this is the test + " + test);
    request.setAttribute("china", test);

    return mapping.findForward("su");
    }
      

  3.   

    我看到网上说:通用的是utf-8,所以我就都改成了utf-8的编码方式!这样有什么不妥?
    劳烦各位详细讲解下
      

  4.   

    request.setCharacterEncoding("UTF-8"); 设置请求字符编码
    或者你设置过滤器,只要涉及到提交的页面都可通过这个过滤器,简化代码!
      

  5.   

    写一个过滤器,给你个例子
    public class SetEncodeFilter implements Filter {

    protected FilterConfig fc=null;
    protected String defaultEncoding=null;
    public void destroy() {
    this.defaultEncoding=null;
    this.fc=null;
    } public void doFilter(ServletRequest arg0, ServletResponse arg1,
    FilterChain arg2) throws IOException, ServletException {
    arg0.setCharacterEncoding("UTF-8");
    arg2.doFilter(arg0, arg1);
    } public void init(FilterConfig arg0) throws ServletException {
    this.fc=arg0;
    this.defaultEncoding=fc.getInitParameter("defaultencoding");
    }}
      

  6.   

    这个也行
    public class CodingFilter implements Filter{

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

    }
    public void destroy() {
    // TODO Auto-generated method stub

    }
    }
    然后在web.xml中加一段
    <filter>
    <filter-name>coding</filter-name>
    <filter-class>过滤器路径</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>coding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  7.   

    request.setCharacterEncoding("gb2312"); 
      

  8.   

    utf-8可以,但是支持的中文有限,
    你要是在项目中开发,
    建议使用gb2312,或者是gbk
      

  9.   

    写个中文字符处理的函数如:
     
    public String codeToString(String str)
    {
       String s=str;
       try
        {
           byte tempB[]=s.getBytes(ISO-8859-1");
           s=new String(tempB);
           return s;
       }
    catch(Exception e)
      {
         return s ;
      }
    }在Action中可以这样用上面的函数:
      String s=codeToString(request.getParameter("username");
    其中s是要接收的字符串变量,username是你传给action的参数。
    你也可以写个过滤器如:
      ackage action.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;public class CharacterEncoding implements Filter {

    public void init(FilterConfig config) throws ServletException
    {
    System.out.print("CharacterEncodingFilter");
    }
    public void doFilter(ServletRequest request,ServletResponse response,
    FilterChain chain)throws IOException,ServletException
    {
    request.setCharacterEncoding("gb2312");
    chain.doFilter(request, response);
    }
    public void destroy()


    }}
    把它编译后放在你的class文件中
    服务器会自动帮你转换,你不用管。
      

  10.   

    写个中文字符处理的函数如:
     
    public String codeToString(String str)
    {
       String s=str;
       try
        {
           byte tempB[]=s.getBytes(ISO-8859-1");
           s=new String(tempB);
           return s;
       }
    catch(Exception e)
      {
         return s ;
      }
    }在Action中可以这样用上面的函数:
      String s=codeToString(request.getParameter("username");
    其中s是要接收的字符串变量,username是你传给action的参数。
    你也可以写个过滤器如:
      ackage action.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;public class CharacterEncoding implements Filter {

    public void init(FilterConfig config) throws ServletException
    {
    System.out.print("CharacterEncodingFilter");
    }
    public void doFilter(ServletRequest request,ServletResponse response,
    FilterChain chain)throws IOException,ServletException
    {
    request.setCharacterEncoding("gb2312");
    chain.doFilter(request, response);
    }
    public void destroy()


    }}
    把它编译后放在你的class文件中
    服务器会自动帮你转换,你不用管。