posts - 14, comments - 0, trackbacks - 0, articles - 0

          在FreeMarker中綁定表單域

          Posted on 2007-01-11 00:04 憶了又憶 閱讀(2087) 評論(0)  編輯  收藏

          你想使用FreeMarker做的最后一件事是將表單域綁定到命令屬性中。在第8章中,你使用JSP的 <spring:bind> 標簽,而在第9.1.6節中,你是使用#springBind Velocity宏來實現這一點的。類似地,Spring提供了一組FreeMarker宏來進行這種綁定。

          等價的FreeMarker宏是< @spring.bind>和<@spring.bindEscaped>。例如,程序清單9.4節選了 registerStudent.ftl中的一段,演示了如何使用<@spring.bind>指令將status信息綁定到表單中。

           程序清單9.4? 在FreeMarker模板中使用<@spring.bind>
          <@spring.bind "command.phone" />

          ? phone: <input type="text"

          ????? name="${spring.status.expression}"

          ??? ??value="${spring.status.value}">

          ? <font color="#FF0000">${spring.status.errorMessage}</font><br>

          <@spring.bind "command.email" />

          ????? email: <input type="text"

          ????? name="${spring.status.expression}"

          ????? value="${spring.status.value}">

          ? <font color="#FF0000">${spring.status.errorMessage}</font><br>

          你可能已經注意到程序清單9.4和程序清單9.2非常相像。但這里有兩個不同。首先,FreeMarker版中不是使用Velocity的#springBind宏,而是使用< @spring.bind>指令。其次,<@spring.bind>將狀態信息綁定到${spring.status}而不是$ {status}。

          正如Sping的Velocity宏,為了使用這些宏,必須設置FreeMarkerViewResolver的exposeMacroHelpers屬性為true:

          ?

          ? <bean id="viewResolver" class="org.springframework.

          ????????? ?web.servlet.view.freemarker.FreeMarkerViewResolver">

          ? …

          ??? <property name="exposeSpringMacroHelpers">

          ????? <value>true</value>

          ??? </property>

          ? </bean>

          ?

          最后,你還需要做一件事才能使用FreeMarker宏。在所有需要使用<@spring.bind>和<@spring.bindEscaped>的FreeMarker模板的頂部增加以下一行:

          ?

          ? <#import "/spring.ftl" as spring />

          ?

          這一行會在模板中導入Spring的FreeMarker宏。

          主站蜘蛛池模板: 和林格尔县| 冀州市| 大同市| 郓城县| 澎湖县| 施甸县| 兴城市| 永州市| 长岛县| 洛宁县| 宜州市| 灵台县| 灌阳县| 三亚市| 瑞金市| 星子县| 东至县| 射阳县| 宝清县| 武清区| 阿拉尔市| 永仁县| 连平县| 饶河县| 巴南区| 常熟市| 全州县| 临漳县| 长沙市| 安新县| 太康县| 庄河市| 神木县| 房山区| 黄山市| 姜堰市| 德惠市| 乌拉特前旗| 香港 | 阿鲁科尔沁旗| 饶河县|