随笔-57  评论-202  文章-17  trackbacks-0
      我在JSP页面中,当处理<a href></a>时,经常不用<html:link/>的方式处理,而是用下面这样的方式处理:
      <a href="foo.do?param1=<bean:write name="n1" property="p1"/>&param2=<bean:write name="n2" property="p2"/>">Test</a>
      这样在处理多个参数时,就不需要先放到一个Collection中。按照<html:link/>的多个参数的处理方法去做,有时确实是很繁琐。不过这样带来一个新的问题,在处理中文参数值时,这样就行不通了。用request.getParameter("param1")获取的中文,在中文的个数为奇数个时,就会显示不正确。例如“三个字”在getParameter中获取的值是“三个?”。
      这个问题是由于<bean:write />没有用URLEncoder的encode方法处理值,因为<bean:write />主要是用来在页面上显示bean的信息,并不是用在链接中当作参数的值。这个问题可以通过给<bean:write />增加新的属性来解决。
      从Struts的网站上下载Struts 1.2.4的源代码:http://apache.freelamp.com/struts/source/jakarta-struts-1.2.4-src.zip
      解压到本地目录,然后修改文件jakarta-struts-1.2.4-src\src\share\org\apache\struts\taglib\bean\WriteTag.java。新增一个属性charset,表示需要用什么编码进行编码。代码片断如下:

    /**
     * Added by George Hill, the string value charset to encoding.
     * 05/19/2005
     
*/

    
protected String charset = null;

    
public String getCharset() {
        
return (this.charset);
    }


    
public void setCharset(String charset) {
        
this.charset = charset;
    }

      修改方法formatValue,把这段


        
if (value instanceof java.lang.String) {
                
return (String) value;
        }
 else {

        }


      替换为:


        
if (value instanceof java.lang.String) {
            
if (charset != null && charset.length() != 0)
                
return TagUtils.getInstance().encodeURL((String) value, charset);
            
else
                
return (String) value;
        }
 else {

        }


      保存修改后的文件。然后需要修改文件jakarta-struts-1.2.4-src\doc\userGuide\struts-bean.xml。这个文件在ant编译时会变成strtus-bean.tld文件。在tag write后面增加一个attribute,如下所示:


  
<tag>
    
<name>write</name>

    
<attribute>
      
<name>charset</name>
      
<required>false</required>
      
<rtexprvalue>true</rtexprvalue>
      
<info>
      
<p>Added by George Hill, use this charset to encoding the value.</p>
      
</info>
    
</attribute>

  
</tag>

      然后修改一下ant的编译配置文件build.xml,有几个部分需要修改:
      1.属性catalina.home需要修改成你安装的tomcat的目录;
      2.compile.classpath需要修改,把jar文件的路径指向正确;
      3.prepare.library taget部分的copy,把相关的jar文件的路径指向正确。
      运行ant,编译后的jar文件保存在jakarta-struts-1.2.4-src\target\library目录中,把原来的struts.jar和struts-bean.tld文件替换掉,就可以用自定义的<bean:write /> tag了。如果不写charset属性,那么和原来的<bean:write />处理是一样的。
      这样,类似于下面的链接地址:
      <a href="foo.do?param1=<bean:write name="n1" property="p1"/>&param2=<bean:write name="n2" property="p2"/>">Test</a>
      就可以修改成:
      <a href="foo.do?param1=<bean:write name="n1" property="p1" charset="UTF-8"/>&param2=<bean:write name="n2" property="p2" charset="UTF-8"/>">Test</a>
      在request.getParameter("param1")中将会获得正确的中文值。
      对于struts 1.1,程序又稍微有些不同。我将在下一篇随笔中介绍。
posted on 2005-05-20 00:50 小米 阅读(2070) 评论(5)  编辑  收藏 所属分类: Struts

评论:
# re: 用URLEncoder输出&lt;bean:write /&gt的值 2005-05-20 11:18 | Dennis
雖然老實說... struts tag 的設計真的很差...
其實要應用類似 jsp:forward 的 jsp:param
加上 html:linkParam 才對

但, 以你這個例子, 肯定是用 html:link 會比較好

1. foo.do 是一個 struts action, 而它的 path 和 .do 是可能轉換的

2.事實上, 可應用 EL 是可以完全替代 bean:write 的
寫一個 EL function 來作 escape 會比較簡單
也不必改動 struts 的 source code

或根本放在 bean 中的 properties 就已經是 escape 了的 string

3. 不滿意 struts 的 taglib, 可以轉用其他(e.g. jstl)
或 extends 它, 甚至自行寫一套也可以...
以 patch 的想法, 改動小許 source 的做法, 會引起相容性問題.

例如 copy bean.tld 為 mybean.tld
也 extends org.apache.struts.taglib.bean.WriteTag
  回复  更多评论
  
# re: 用URLEncoder输出&lt;bean:write /&gt的值 2005-05-20 11:31 | 小米
确实,struts在View这一层做的相当不好,设计人员没有很好的考虑实际开发人员是否方便使用。我一般也是迫不得以才考虑修改struts的源代码,因为如果随便修改源代码的话,在团队开发中会带来很大的麻烦,自己还需要维护一套struts的代码。
我也在考虑着在View这一层不使用struts的实现。多谢你的指教。  回复  更多评论
  
# re: 用URLEncoder输出&lt;bean:write /&gt的值[未登录] 2008-06-03 11:40 | aaa
aaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaa  回复  更多评论
  
# re: 用URLEncoder输出&lt;bean:write /&gt的值[未登录] 2008-06-03 11:40 | aaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  回复  更多评论
  
# re: 用URLEncoder输出&lt;bean:write /&gt的值[未登录] 2008-06-03 11:44 | aaa
卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙卧虎藏龙  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: