java - 找不到属性的 setter 方法:spring boot 中的错误
问题描述
我正在使用 spring boot。我的 pom.xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ashwin</groupId>
<artifactId>spring_app_vemployee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_app_vemployee</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.27</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我的控制器是:
@Controller
@SessionAttributes("name")
public class TodoController {
@Autowired
private ToDoService toDoService;
@RequestMapping(value="/add-todo",method= RequestMethod.GET)
public String showAddToDoPage(ModelMap model){
model.addAttribute("todo", new Todo(0, "abc", "Default Desc",
new Date(), false));
return "todo";
}
}
它正在运行的jsp是todo.jsp
。当我添加spring表单时,我开始看到错误:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<title>First web app</title>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<form:form method="post" commandName="todo">
<fieldset class="form-group">
<form:label path="desc">Description</form:label>
<form:input path="desc" type="text" class="form-control" required="required"/>
</fieldset>
</form:form>
</div>
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
我试图在模型和控制器之间绑定的类是:
public class Todo {
private int id;
private String user;
private String desc;
private Date targetDate;
private boolean isDone;
public Todo() {
super();
}
public Todo(int id, String user, String desc, Date targetDate,
boolean isDone) {
super();
this.id = id;
this.user = user;
this.desc = desc;
this.targetDate = targetDate;
this.isDone = isDone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Date getTargetDate() {
return targetDate;
}
public void setTargetDate(Date targetDate) {
this.targetDate = targetDate;
}
public boolean getDone() {
return isDone;
}
public void setDone(boolean isDone) {
this.isDone = isDone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Todo other = (Todo) obj;
if (id != other.id) {
return false;
}
return true;
}
@Override
public String toString() {
return String.format(
"Todo [id=%s, user=%s, desc=%s, targetDate=%s, isDone=%s]", id,
user, desc, targetDate, isDone);
}
}
但我收到错误:
There was an unexpected error (type=Internal Server Error, status=500).
/WEB-INF/jsp/todo.jsp (line: [11], column: [0]) Unable to find setter method for attribute: [commandName]
org.apache.jasper.JasperException: /WEB-INF/jsp/todo.jsp (line: [11], column: [0]) Unable to find setter method for attribute: [commandName]
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:292)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:115)
at org.apache.jasper.compiler.Generator$GenerateVisitor.evaluateAttribute(Generator.java:3015)
at org.apache.jasper.compiler.Generator$GenerateVisitor.generateSetters(Generator.java:3235)
解决方案
尝试在您modelAttribute
的. 那应该可以解决它。commandName
todo.jsp
推荐阅读
- corda - Flow 挂在 Corda 中收集交易对手签名处
- vespa - 如何在 yql 查询中应用测试和设置条件
- python - 读取json文件时处理条件语句python中的KeyError
- php - 在 Woocommerce 中更改后端订单股票票据上的“→”字符
- scala - spark 捕获所有异常并打印到字符串
- excel - 单元格中的时间戳 - 不同的时间戳,但它们必须留在单元格中
- regex - 选择所有以一个字符开头的行并只保留一个条目
- html - 在材料设计选项卡底部添加三角形 mat-ink-bar
- c - 这个令人困惑的 typedef 的 C 语言问题
- reactjs - 状态回调触发对象无效错误