Freemarker中访问Struts2的select标签的问题以及解决办法
用Struts2做MVC,Freemarker做表现层。因为Freemarker中可以访问Struts2的一些标签,在加上Freemarker本身的一些功能,个人觉得这种组合很是方便。
但是这里碰到一个问题:我参考Struts2的标签说明文档,做了一个下拉框。这个下拉框默认就是三个值,一个空,一个是,一个否。用Struts2的select标签很容易解决。JSP代码如下:
<s:select name="person.is_emergency" list="#{'-1':'','0':'是','1':'否'}" value="-1"/>
注意,这里采用了一个特殊的处理方式,在页面上来生成一个HashMap,OGNL的表达式方式。#{}
大家可以看一下Struts2的Tag Reference中,关于select标签的说明有如下的例子:
<s:select label="Months"?name="months"?headerKey="-1"?headerValue="Select Month"?list="#{'01':'Jan', '02':'Feb', [...]}"?value="selectedMonth"?required="true"?/>
?
现在问题来了,在Freemarker中,对#{}的使用有自己的含义:输出一个数字值,可以按照格式进行输出。使用的格式如下:
<#assign x=2.582>
#{x; m1M2} <#-- 输出2.58 -->
相关资料可以参考http://freemarker.sourceforge.net/docs/ref_depr_numerical_interpolation.html。
这样一来,如果在ftl中使用struts2的select标签,就不能用#{}这种方式来生成一个默认的内容了。否则会跟Freemarker的标签冲突。ftl模板首先会把这个内容当作数字输出来解析,然后发现其中有引号和非数字类东西,就会报错。
?
<@s.select label="Foo label - ${foo}" name="${name}" list="%{{1, 2, 3}}"/>What will happen here is that each attribute will be evaluated to a String as best it can. This may involve calling the?toString?method on the internal FreeMarker objects. In this case, all objects will end up being exactly what you would expect. Then, when the tag runs, the?listattribute will be converted from a String to a List using?OGNL's advanced collection support.
But suppose you wish to use FreeMarker's list or hash support instead? You can do this:
<@s.select label="Foo label - ${foo}" name="${name}" list=[1, 2, 3]/>Notice that the list attribute no longer has quotes around it. Now it will come in to the tag as an object that can't easily be converted to a String. Normally, the tag would just call?toString, which would return "[1, 2, 3]" and be unable to be converted back to a List by OGNL. Rather than go through all this back and forth, the frameworks's FreeMarker tag support will recognize collections and not pass them through the normal tag attribute. Instead, the framework will set them directly in the?parameters?Map, ready to be consumed by the template.
In the end, everything tends to do what you would expect, but it can help to understand the difference of when OGNL is being used and when it isn't, and how attribute types get converted.
他给出了一种用List做为select参数的写法,但是Hash呢?????他没有说明。
我试验用这种方式来处理写成['-1':'','0':'是','1':'否']各种形式都不行......
后来我就采用了一个折中的办法,通过在服务器上生成一个Session或者Application参数,来保存这个Hash,然后ftl模板通过#application来访问。这样居然可以了。
首先,在Login的Action中,访问并设定Application参数:
?
?
?
但是这里有两个问题:
1.所有的Select值都要用这种方式定义在Application中,岂不是......相当的不卫生!:(
2.这种方式生成的Hash,顺序完全是不可控制的。有可能下拉框第一个值是“否”,第二个是"空",第三个是"是",这.......
?
最终在今天早上的时候,突然想到,既然Freemarker要解析这个东西,那么我就不让他解析,我把它当作字符串参数按照原样传递给Struts2的Taglib,这样是不是可行呢?
?
OK,一切搞定。是不是很简单,我也觉得是。