【问题标题】:Bean Bind to form using Freemarker使用 Freemarker 绑定到表单
【发布时间】:2012-10-20 12:50:32
【问题描述】:

我尝试将一个 bean 从表单绑定到一个表单 sn-p:

[#ftl/]
[#import "spring.ftl" as spring /]
[#import "panda.ftl" as panda /]

[#import "discoveryProject.ftl" as discoveryProject/]
[#macro defineProjectForm newProject]
[@spring.bind "discoveryProjectDetailsBean"/]

[#if newProject?has_content && newProject =="true"]
<table class="transparentTable">
    <tr>
        <!--Left Part-->
        <td>
            <table>
                <tr>
                    <td>
                        [@spring.showErrors " " "errors"/]
                        <span>Data Source<sup><span style="color: red; ">*</span></sup></span>
                    </td>
                    <td>
                        <select id="dataSourceSelect" onchange="checkSelectChanges()">
                            [#if discoveryProjectLookupBean.dataSources?has_content]
                                [#list discoveryProjectLookupBean.dataSources as dataSource]
                                    <option id="${dataSource.id}" value="${dataSource.name}">${dataSource.name}</option>
                                [/#list]
                            [/#if]
                        </select>
                    [@spring.bind path="discoveryProjectDetailsBean.discoveryProjectBean.dataSource"/]
                    </td>
                </tr>
            </table>

这是提交给一个控制器,sn-p到这个控制器:

@RequestMapping("/navigateDiscoveryProject")
ModelAndView navigateDiscoveryProject(@RequestParam("index")String i,@RequestParam("direction")String direction,
        @ModelAttribute("discoveryProjectDetailsBean")DiscoveryProjectDetailsBean discoveryProjectDetailsBean,BindingResult result,HttpSession session)throws Exception{
    logger.info("method invoked");
    int index=Integer.parseInt(i);
    //another code
}

bean discoveryProjectDetailsBean 包含的属性实际上是另一个 bean discoveryProjectBean 这个 bean 的属性始终为 null 但是我自己在 ftl 中将它们绑定到上面的 dataSource 属性示例中,属性的所有值到 discoveryProjectBean始终为空。

【问题讨论】:

    标签: spring-mvc el freemarker


    【解决方案1】:

    我为我的问题找到了两个答案。 第一种方法是单独绑定每个属性,请参阅我只是绑定命令对象,而我应该将每个属性绑定到其特定的控制器。 像这样

    [#ftl/]
    [#import "spring.ftl" as spring /]
    [#import "panda.ftl" as panda /]
    
    [#import "discoveryProject.ftl" as discoveryProject/]
    [#macro defineProjectForm newProject]
    // this is useless [@spring.bind "discoveryProjectDetailsBean"/]
    
    [#if newProject?has_content && newProject =="true"]
    <table class="transparentTable">
        <tr>
            <!--Left Part-->
            <td>
                <table>
                    <tr>
                        <td>
                            [@spring.showErrors " " "errors"/]
                            <span>Data Source<sup><span style="color: red; ">*</span></sup></span>
                        </td>
                        <td>
                            <select id="discoveryProjectBean.dataSource" name="discoveryProjectBean.dataSource" onchange="checkSelectChanges()">
                                [#if discoveryProjectLookupBean.dataSources?has_content]
                                    [#list discoveryProjectLookupBean.dataSources as dataSource]
                                        <option id="${dataSource.id}" value="${dataSource.name}">${dataSource.name}</option>
                                    [/#list]
                                [/#if]
                            </select>
    //the following binding work just fine
                            [@spring.bind path="discoveryProjectDetailsBean.discoveryProjectBean.dataSource"/]
                        </td>
                    </tr>
                </table>
    

    我上面所做的是从命令对象中获取我需要的属性,将其设置为选择标签的名称和 ID,并使用@spring.bind 将标签绑定到属性。

    这是最好的解决方案

    另一种方法是这样做:

    [#ftl/]
    [#import "spring.ftl" as spring /]
    [#import "panda.ftl" as panda /]
    
    [#import "discoveryProject.ftl" as discoveryProject/]
    [#macro defineProjectForm newProject]
    [@spring.bind "discoveryProjectDetailsBean"/]
    
    [#if newProject?has_content && newProject =="true"]
    <table class="transparentTable">
        <tr>
            <!--Left Part-->
            <td>
                <table>
                    <tr>
                        <td>
                            [@spring.showErrors " " "errors"/]
                            <span>Data Source<sup><span style="color: red; ">*</span></sup></span>
                        </td>
                        <td>
                            [@spring.bind "dataSources"/]
                    [@spring.formSingleSelect "discoveryProjectDetailsBean.discoveryProjectBean.dataSource" dataSources "disabled='disabled' multiple='multiple' class='singleList' onchange='checkValidations()'" /]
    
                        </td>
                    </tr>
                </table>
    

    不,这提供了更少的编码,但限制了您的选择标记中的选项列表,请参阅您获得一个字符串列表以将其绑定到 @spring.formSingleSelect 这是上面代码中的 dataSources 如果此列表是除了字符串列表之外的任何内容都会生成 freemarker.template.TemplateException: 的 freemarker 异常。 我希望这会对某人有所帮助

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 2011-01-24
      • 2010-12-15
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 2011-02-04
      相关资源
      最近更新 更多