struts2中遇到的中文乱码问题

野猪在用struts2的时候遇到了中文问题~哎~弄了一中午,感觉对着呢,能配的都配了~就当最后的时候突然就可以了~哈哈……好高兴,有的时候也许只要在坚持一小下,也许野猪就能得到他想要的了!野猪,记住啦!嗯哪!

现在总结一下:

解决之前在网上搜了一大堆,感觉比较有用的有:

  1. 在struts2里面,最好将所有字符都设成utf-8

    <%@ page contentType=”text/html; charset=UTF-8″%>
    <%@ page pageEncoding=”UTF-8″ %>

  2. 在struts.properties 添加:

    struts.devMode=false
    struts.enable.DynamicMethodInvocation=true
    struts.i18n.reload=true
    struts.ui.theme=simple

    struts.locale=zh_CN
    struts.i18n.encoding=UTF-8

    struts.serve.static.browserCache=false
    struts.url.includeParams=none

  3. 在web.xml加个filter

    <!– zh-cn encoding –>
    <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>

  4. 跟上述方法,类似还有在action中设定字符编符.

    HttpServletResponse response = null;
    response = ServletActionContext.getResponse();
    request.setCharacterEncoding(“utf-8″);
    response.setContentType(“text/html;charset=utf-8″);

  5. tomcate5.5是会乱码的,而在tomcate6中就不会。这边就涉 及到tomcate connector字符的设置了

    <Connector port=”80″ maxHttpHeaderSize=”8192″
    maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
    enableLookups=”false” redirectPort=”8443″ acceptCount=”100″
    connectionTimeout=”20000″ disableUploadTimeout=”true” URIEncoding=”GBK” />

  6. 这个方法是下下策了,只有在前面的方法都无效时才使用

    在action中直接使用request.getParameter()时;还是出现乱码。原因分析如下:

    1、getParameter()是有带字符参数的。例:

    String s = (String)request.getParameter(“txt”).getBytes(“iso-8859-1″);

    2、String也可以带有字符参数。

    String(byte[] bytes, String charsetName)

    构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。

    例:String s = new String(“中文”,”utf-8″);

    3、综合上述两点,编写一个类来完成此项任务

    public class ConvertCharacter{

    public String Convert(String s){

    String result;

    byte[] temp ;

    try{

    temp = s.getBytes(“iso-8859-1″);

    result = new String(temp,”utf-8″);

    }

    return result;

    }

    }

    但是我试了这些方法不行,我也很纳闷为什么……(可能是我自己马虎了?应该是吧……野猪就是爱犯这样错误~更何况这两天脑子进水了呢~)

    tomcat - 5.5.28  

    struts2 - 2.1.6

    strurs.xml中必须有:

    <constant name="struts.i18n.encoding" value="gb2312"></constant>

    web.xml中的配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    </web-app>

    jsp页面中:

    <%@ page language="java" contentType="text/html; charset=gb2312"%>

    这应该可以用于表单传值,当页面传值时:

    try {
                    subvalue = new String(subvalue.getBytes("ISO-8859-1"), "gb2312");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                searchInfo.setSubvalue(subvalue);

    重新设置一下你的中文属性!

    这样就可以了~嘿嘿……感觉开始自己没有弄出来,肯定是自己又马虎了,脑子有进水了!我都有点瘦不了我自己了~晕~仔细~

    野猪!听见没!你要是在敢给我马马虎虎,我就~你自己看着办~

 

你可能感兴趣的:(struts2,中文乱码,职场,休闲)