当我覆盖名为Attribute的Model类的equals方法时出错。错误如下:
2016-08-05 16:28:38.945 [0; 39m [31mERROR [0; 39m [35m2434 [0; 39m] [2m --- [0; 39m [2m [nio-8079-exec-5] [0; 39m [36morg.thymeleaf.TemplateEngine [0; 39m [2m:[0; 39m] [THYMELEAF] [http-nio-8079-exec-5] 异常处理模板 " / attribute-value-new":执行处理器时出错 ' org.thymeleaf.spring4.processor.attr.SpringOptionFieldAttrProcessor' (/ attribute-value-new:96)[2m2016-08-05 16:28:38.953 [0; 39m [31mERROR [0; 39m [35m2434 [0; 39m [2m --- [0; 39m] [2M [NIO-8079-EXEC-5] [0;39米 [36mo.a.c.c.C。[。[。[/]。[dispatcherServlet] [0; 39m [2m:[0; 39m] 的> servlet [dispatcherServlet]的Servlet.service()与path一起使用 []引发异常[请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:期间出错 执行处理器 ' org.thymeleaf.spring4.processor.attr.SpringOptionFieldAttrProcessor' (/ attribute-value-new:96)]具有根本原因
java.lang.ClassCastException:java.lang.String无法强制转换为 my.com.core.model.Attribute at my.com.core.model.Attribute.equals(Attribute.java:119)〜[classes /:na] 在 org.springframework.util.ObjectUtils.nullSafeEquals(ObjectUtils.java:297) 〜[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.web.servlet.tags.form.SelectedValueComparator.isSelected(SelectedValueComparator.java:75) 〜[spring-webmvc-4.2.5.RELEASE.jar:2.1.4.RELEASE] at org.springframework.web.servlet.tags.form.SelectedValueComparatorWrapper.isSelected(SelectedValueComparatorWrapper.java:34) 〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.thymeleaf.spring4.processor.attr.SpringOptionFieldAttrProcessor.doProcess(SpringOptionFieldAttrProcessor.java:69) 〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:103) 〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) 〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) 〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) 〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:972) 〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695
这个等于方法的覆盖是:
@Override
public boolean equals(Object obj) {
return ((Attribute)obj).getId()==this.id;
}
Thymeleaf电话:
<div th:if="${account!= null}" id="attribute-value-attribute">
<select th:field="*{attribute}" class="form-control">
<option th:each="attribute : ${account.attributes}"
th:value="${{attribute}}" th:text="${attribute.name}" />
</select>
</div>
如果我删除重写的equals方法,则错误得到解决。似乎Thymeleaf无法使用被覆盖的平等。