struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)

 

struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)

 

原文地址

我的前台页是这样的:
< 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中的中文参数乱码问题。
 
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文件内添加常量:
< constant  name ="struts.i18n.encoding"  value ="GBK" />

我当然是用的第二种方法,简单方便.
<? 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 >

呵呵,终于解决了.希望对大家有些帮助.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值