0

我尝试将 bean 绑定到表单中的表单片段:

[#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>

这被提交给一个控制器,这个控制器的片段:

@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
}

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

4

1 回答 1

0

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

[#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.formSingleSelectdataSources代码中,如果此列表不是字符串列表,它将生成一个freemarker.template.TemplateException 的 freemarker 异常:。我希望这会对某人有所帮助

于 2012-11-19T16:11:28.263 回答