java - 找不到属性 : Error in spring boot 的设置方法

标签 java spring spring-boot jsp

我正在使用 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 是:
@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>

我试图在模型和 Controller 之间绑定(bind)的类是:
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 中的表格中.那应该可以解决它。

关于java - 找不到属性 : Error in spring boot 的设置方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58805047/

相关文章:

java - 如何在Policy中授予用户权限?

java.lang.IllegalStateException : Cannot load configuration class: com. javainuse.SpringBootHelloWorldApplication

java - spring 中的自定义注解

java - 将 Spring Boot war 部署到 Jenkins 中

spring-boot - MapStruct 向 dto 添加一个新的计算字段

java - Firefox 启动等待 45 秒超时

java - Spring消息TTL不起作用

java - 安卓支持库 v17

java - 如何在基于 XML 的任务调度程序中使固定速率动态化?

java - 运行时Logback java配置