我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)

          我的前臺頁是這樣的:
          <body>
                
          <form action="test.action" method="post">
                    測試文件:
          <input type="file" id="doc" name="path" value=""/>
                    
          <input type="submit" value="提交" onclick=""/>
                
          </form>
            
          </body>
          Action:
          package com;

          import java.io.UnsupportedEncodingException;


          import com.opensymphony.xwork2.ActionSupport;

          @SuppressWarnings(
          "serial")
          public class TestAction extends ActionSupport
          {
              
          private String path;

              
          public String getPath()
              
          {
                  
          return path;
              }


              
          public void setPath(String path)
              
          {
                  
          this.path = path;
              }



              
          public String test() throws Exception
              
          {
                  System.out.println(path.replace(
          "\\""\\\\"));
                  
          return SUCCESS;
              }


          }


          剛開始的時候一選中文路徑就輸出???.
          后來終于找到解決方法.
          在struts.xml文件中加上:
          為了解決form提交到action中的中文參數(shù)亂碼問題。
           
          1.在struts2-core-2.0.0-SNAPSHOT.jar包中路徑為struts2-core-2.0.6\org\apache \struts2
          有一個default.properties 文件,把struts.i18n.encoding=UTF-8改為
          struts.i18n.encoding=GBK 

          2.或者在struts.xml文件內(nèi)添加常量:
          <constant name="struts.i18n.encoding" value="GBK"/>

          我當(dāng)然是用的第二種方法,簡單方便.
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd"
          >
          <struts>
              
          <constant name="struts.i18n.encoding" value="GBK"/>
              
          <package name="com" extends="struts-default">
                  
          <action name="test" class="com.TestAction" method="test">
                      
          <result>/ok.jsp</result>
                  
          </action>
              
          </package>
          </struts>

          呵呵,終于解決了.希望對大家有些幫助.

          posted on 2007-12-26 12:24 々上善若水々 閱讀(12705) 評論(5)  編輯  收藏 所屬分類: Struts2

          評論

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評論   

          呵呵 謝謝 !
          2008-12-22 15:46 | 雪楓

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評論   

          不錯 問題解決。
          2009-05-15 21:47 | 杜棟

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑) [未登錄]  回復(fù)  更多評論   

          真的解決了,謝謝你啊,牛逼
          2011-05-26 06:06 | rose

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評論   

          太謝謝你了,解決了困擾幾天的問題。
          2012-09-27 11:10 | Ajian

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評論   

          非常感謝,解決了我花費(fèi)一天時間都沒解決的問題
          2013-05-06 18:46 | *
          主站蜘蛛池模板: 乌兰察布市| 绍兴市| 延安市| 叶城县| 澄城县| 泰兴市| 钦州市| 白沙| 左权县| 商河县| 蓬莱市| 宾阳县| 小金县| 若尔盖县| 宁波市| 临泉县| 如皋市| 女性| 沁源县| 阿克陶县| 甘孜| 峨眉山市| 梁山县| 阿拉善右旗| 饶阳县| 普兰县| 玉山县| 油尖旺区| 墨脱县| 乌审旗| 甘谷县| 乌拉特前旗| 天长市| 色达县| 新营市| 龙游县| 连城县| 凤庆县| 延寿县| 阜南县| 临桂县|