I try to bind a bean to a form snippet from the form:
[#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>
this is submitted to a controller, snippet to this controller:
@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
}
the bean discoveryProjectDetailsBean contains property which is actually another bean discoveryProjectBean this bean’s properties are always null however I bind them myself in the ftl like in the dataSource property example above, all the values of properties to the discoveryProjectBean are always null.
I have found two answers for my problem.
the first way is to bind each property alone, see I was only binding the command object while I should’ve binded each property to its specific controller.
like this
what I did above is that I get the property I need from the command object set it as the name and ID of the select tag and binded the tag to the property using
@spring.bind.this is the best solution
an alternative is to do it like this:
no this provide less coding but gives limitations with the list of options in your select tag, see you get a list of string to bind it to the
@spring.formSingleSelectthis is thedataSourcesin the code above if this list is anything but list of string it will generate a freemarker exception of freemarker.template.TemplateException:.I hope this would help somebody