我尝试将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
}
bean discoveryProjectDetailsBean
包含的属性实际上是另一个bean discoveryProjectBean
这个bean的属性总是为null但是我自己在ftl中将它们绑定在上面的dataSource属性示例中,所有属性的值都是discoveryProjectBean
始终为空。
答案 0 :(得分: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>
我上面做的是我从命令对象获取我需要的属性,将其设置为select标记的名称和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>
这没有提供更少的编码,但是对select标签中的选项列表提供了限制,请参阅获取字符串列表以将其绑定到@spring.formSingleSelect
这是上面代码中的dataSources
如果此列表不是字符串列表,则会生成freemarker.template.TemplateException的freemarker异常:
我希望这会对某人有所帮助