我正在使用spring boot,jpa和Thymeleaf编写示例代码。这是我的代码,我可以看到数据到控制器但由于以下异常而无法显示。非常感谢您的帮助。
Controller:
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
AccountRepository accountRepository;
@RequestMapping("/")
public String list(ModelMap modelMap){
List<Account> accounts = (List<Account>) accountRepository.findAll();
modelMap.put("accounts", accounts);
//return accountRepository.findAll();
return "account";
}
Entity:
@Entity
public class Account {
@Id
@GeneratedValue
private Integer accountId;
private String name;
private String accType;
public Integer getAccountId() {
return accountId;
}
public String getName() {
return name;
}
public String getAccType() {
return accType;
}
public Account(Integer accountId, String name, String accType) {
super();
this.accountId = accountId;
this.name = name;
this.accType = accType;
}
account.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1"/>
<title>Insert title here</title>
</head>
<body>
<h1> Account Management</h1>
<table border="2">
<tr>
<th>Account Id</th>
<th>Name</th>
<th>Account Type</th>
</tr>
<tr th:each="account : accounts">
<td th:text="${account.accountId}"/>
<td th:text="${account.name}"/>
<td th:text="${account.accType}"/>
</tr>
</table>
<a href="/">Back</a>
</body>
</html>
Exception Stack trace:
2016-10-12 14:51:56.263 ERROR 32572 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "account": Exception evaluating SpringEL expression: "account.accountId" (account:17)
2016-10-12 14:51:56.269 ERROR 32572 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "account.accountId" (account:17)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 8): Property or field 'accountId' cannot be found on object of type 'java.lang.String' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224) ~[spring-expression-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94) ~[spring-expression-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46) ~[spring-expression-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:374) ~[spring-expression-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88) ~[spring-expression-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120) ~[spring-expression-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:267) ~[spring-expression-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:139) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:154) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:103) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:133) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:120) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.standard.processor.attr.AbstractStandardTextChildModifierAttrProcessor.getText(AbstractStandardTextChildModifierAttrProcessor.java:68) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor.getModifiedChildren(AbstractTextChildModifierAttrProcessor.java:59) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.processor.attr.AbstractChildrenModifierAttrProcessor.processAttribute(AbstractChildrenModifierAttrProcessor.java:59) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
答案 0 :(得分:2)
此:
<tr th:each="account : accounts">
应该是
<tr th:each="account : ${accounts}">
它没有将“帐户”解析为变量,所以当您以后使用$ {account.accountId}时,它无法正确评估该表达式。
答案 1 :(得分:0)
在我的情况下,我正在使用
对列表进行排序 th:each="company : ${#lists.sort(allCompanys)}"
但忘了实施Comparable<Company>