我可以看到List在Thymeleaf表单中成功使用 http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields
<tr th:each="row,rowStat : *{rows}">
<td th:text="${rowStat.count}">1</td>
<td>
<select th:field="*{rows[__${rowStat.index}__].variety}"><!-- how to do this with Set? -->
<option th:each="var : ${allVarieties}"
th:value="${var.id}"
th:text="${var.name}">Thymus Thymi</option>
</select>
</td>
..
</tr>
我可以使用Set而不是List执行相同的操作吗?特别是代码中带注释的行。 如何将Set元素分配给th:field?
更新1
我在我的项目中使用了这个并继续使用Set并在提交/添加新行时,它给出了以下错误。它甚至没有达到相应的控制器方法。
org.springframework.beans.InvalidPropertyException:bean类的无效属性'catDescrDts [0]'[ae.tbits.atn.aiwacore.common.model.CatDescr]:非法尝试获取属性'catDescrDts'引发异常;嵌套异常是org.springframework.beans.InvalidPropertyException:bean类的无效属性'catDescrDts [0]'[ae.tbits.atn.aiwacore.common.model.CatDescr]:无法从大小为0的Set中获取索引为0的元素,使用属性路径'catDescrDts [0]'
访问
CatDescrDts对应于示例中的行,它与CatDescr实体有多对一的关系。
堆栈跟踪
Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'catDescrDts[0]' of bean class [ae.tbits.atn.aiwacore.common.model.CatDescr]: Cannot get element with index 0 from Set of size 0, accessed using property path 'catDescrDts[0]'
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:661)
... 51 more
2017-07-13 16:23:57 ERROR ErrorController:19 - Exception during execution of SpringSecurity application
org.springframework.beans.InvalidPropertyException: Invalid property 'catDescrDts[0]' of bean class [ae.tbits.atn.aiwacore.common.model.CatDescr]: Illegal attempt to get property 'catDescrDts' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'catDescrDts[0]' of bean class [ae.tbits.atn.aiwacore.common.model.CatDescr]: Cannot get element with index 0 from Set of size 0, accessed using property path 'catDescrDts[0]'
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:708)
at org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:836)
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:813)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:270)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:834)
at org.springframework.validation.DataBinder.doBind(DataBinder.java:730)
at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:192)
at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:106)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:150)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:114)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'catDescrDts[0]' of bean class [ae.tbits.atn.aiwacore.common.model.CatDescr]: Cannot get element with index 0 from Set of size 0, accessed using property path 'catDescrDts[0]'
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:661)
... 51 more
更新2
提供一些代码..
控制器
@GetMapping({"/create"})
public String create(ModelMap model){
CatDescr catDescr = new CatDescr();
//add 1 empty row for catDescrDt
int noOfCatDescrDts = 1;
catDescr.setCatDescrDts(getNewCatDescrDts(noOfCatDescrDts, catDescr));
model.addAttribute("catDescr", catDescr);
return "catDescr/create";
}
@PostMapping(value="/create", params={"save"})
public String save(final CatDescr catDescr
, final BindingResult bindingResult, final ModelMap model){
...
CatDescr updatedCatDescr = catDescrService.save(catDescr);
model.clear();
return "redirect:/catDescr/create";
}
@RequestMapping(value="/create", params={"addCatDescrDtRow"})
public String addCatDescrDtRow(ModelMap model, final CatDescr catDescr, final BindingResult bindingResult) {
catDescr.getCatDescrDts().add(new CatDescrDt());
...
return "catDescr/create";
}
查看
<ul th:each="item, stat: *{catDescrDts}">
<li>
<input type="text" th:field="*{catDescrDts[__${stat.index}__].name}" th:id="${'catDescrDts'+stat.index+'.name'}">
</li>
答案 0 :(得分:0)
必须在实体中使用自定义getter / setter才能返回Set as List,而是绑定Thymeleaf中的List,以便可以通过索引访问它。
在控制器中必须将List成员与Set成员同步。
所有归功于@Dark,精彩的解释以及有用的链接。