BindingResult 和 bean 名称“todooo”的普通目标对象都不能用作请求属性

问题描述 投票:0回答:1

在没有验证的情况下运行插入时,它可以工作,但是当我添加诸如目标日期必须是未来日期之类的验证并尝试以双向绑定形式显示时,就会出现此错误。

不知道我需要考虑哪个方向。

错误:


2024-04-26T12:24:20.955-04:00 DEBUG 11520 --- [ToDo] [nio-8080-exec-8] o.s.web.servlet.view.JstlView            : View name 'insert', model {todo=ToDo{id=5, username='Test1, description='HelloWorld, targetDate=2020-04-26, creationDate=2024-04-26, done=false, attachment= XX}, org.springframework.validation.BindingResult.todo=org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'todo' on field 'targetDate': rejected value [2020-04-26]; codes [Future.todo.targetDate,Future.targetDate,Future.java.time.LocalDate,Future]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [todo.targetDate,targetDate]; arguments []; default message [targetDate]]; default message [springboot-starter-validation-@size---> ID -->  ALLOWED to enter only FUTURE dates ]}
2024-04-26T12:24:20.962-04:00 DEBUG 11520 --- [ToDo] [nio-8080-exec-8] o.s.web.servlet.view.JstlView            : Forwarding to [/WEB-INF/jsp/insert.jsp]
2024-04-26T12:24:21.034-04:00 ERROR 11520 --- [ToDo] [nio-8080-exec-8] o.s.web.servlet.tags.form.InputTag       : Neither BindingResult nor plain target object for bean name 'todooo' available as request attribute

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'todooo' available as request attribute





org.apache.jasper.JasperException: An exception occurred processing [/WEB-INF/jsp/insert.jsp] at line [34]

31: 
32:             <form:form method="post" modelAttribute="todooo">
33:                 <p><label>id : </label>
34:                     <form:input type="text" path="id" required="required"   />
35: 
36:                 <p><label>username : </label>
37:                     <form:input type="text" path="username" required="required"    />


Stacktrace:
  • ToDo_Controller.java
    @RequestMapping(value  = "/insert", method= RequestMethod.GET)
    public String get_insert_todo(ModelMap modelMap){
        String u_retrived = (String) modelMap.get("uid");
        Todo todo_obj = new Todo(toDo_services.listAllToDo().size()+1,u_retrived,"HelloWorld",LocalDate.now(), LocalDate.now().plusYears(1),false );

        modelMap.put("todooo", todo_obj);

        return "insert";
    }

    @RequestMapping(value  = "/insert", method= RequestMethod.POST)
    public String post_insert_data(ModelMap modelMap, @Valid Todo todooo, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            return  "insert";  
        }

        String u_retrived = (String) modelMap.get("uid");
        toDo_services.insert_todo(String.valueOf(todooo.getId()), u_retrived, todooo.getDescription(), todooo.getCreationDate(), todooo.getTargetDate(), todooo.getDone());
        return "redirect:list";
    }

  • ToDo_Services.java
    public List<Todo> insert_todo(String id,
                                  String username,
                                  String description,
                                  LocalDate creationDate,
                                  LocalDate targetDate,
                                  boolean done) {

        int i1 = id.isEmpty() ? listToDo.size()+1 : Integer.valueOf(id);
        boolean addedorNot = listToDo.add(new Todo(  i1 , username,description,creationDate,targetDate, done));
        l1.info("insertingg(insert_todo).... given data T/F ==" + addedorNot);
        return  listToDo;
    }
  • 插入.jsp
<%@ taglib prefix="test1" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>


<html>
    <head>
        <link href="\webjars\bootstrap\5.1.3\css\bootstrap.min.css" rel="stylesheet">
        <title> Add : Todo    </title>
    </head>
    <body>
            <div class="container">
            

            <form:form method="post" modelAttribute="todooo">
                <p><label>id : </label>
                    <form:input type="text" path="id" required="required"   />

                <p><label>username : </label>
                    <form:input type="text" path="username" required="required"    />

                <p><label>description : </label>
                    <form:input type="text" path="description" required="required"   />

                <p><label>creationDate : </label>
                    <form:input type="text" path="creationDate" required="required"    />
                    
                <p><label>targetDate : </label>
                    <form:input type="text" path="targetDate" required="required"    />
                    <form:errors path="targetDate"   />
                    
                <p><label>done : </label>
                    <form:input type="text" path="done" required="required"    />

                <p><input type="submit" value=" + "  class="btn btn-success"> </input>
            </form:form>

            <p>
                Hint1:
            <p>
                Hint2:
<p><p>

        </div>
        <script src="\webjars\bootstrap\5.1.3\js\bootstrap.min.js"> </script>
        <script src="\webjars\jquery\3.6.0\jquery.min.js"> </script>
    </body>
    <html>

**

  • ToDo.java

**

package springboot.ToDo.Model;


import jakarta.validation.constraints.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.lang.NonNull;

import java.time.LocalDate;
import java.util.Date;

public class Todo {

    private int id;
    private String username;
    private String description;
    private LocalDate creationDate;
    @Future(message ="targetDate -->  ALLOWED to enter only FUTURE dates " )
    private LocalDate targetDate;
    private boolean done;



    public LocalDate getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(LocalDate creationDate) {
        this.creationDate = creationDate;
    }

    public Todo(int id, String username, String description, LocalDate creationDate , LocalDate targetDate, boolean done ) {
        this.id = id;
        this.username = username;
        this.description = description;
        this.creationDate =creationDate;
        this.targetDate = targetDate;
        this.done = done;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDate getTargetDate() {
        return targetDate;
    }

    public void setTargetDate(LocalDate targetDate) {
        this.targetDate = targetDate;
    }

    public boolean getDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }

    @Override
    public String toString() {
        return "ToDo{" +
                "id=" + id +
                ", username='" + username +
                ", description='" + description +
                ", targetDate=" + targetDate +
                ", creationDate=" + creationDate +
                ", done=" + done
                '}';
    }
}

尝试调试代码,仅当添加验证+添加towway绑定时才会触发错误。没有验证+双向绑定标签,一切正常。

spring-mvc jsp jstl
1个回答
0
投票

您的应用程序出现错误,因为您没有指定传递给

POST
方法的模型属性。将
@ModelAttribute("todooo")
添加到方法参数中:

public String post_insert_data(ModelMap modelMap, @Valid @ModelAttribute("todooo") Todo todooo, BindingResult bindingResult){
       
© www.soinside.com 2019 - 2024. All rights reserved.