SpringMVC-Java.lang.IllegalStateException:Bean名称既不是BindingResult也不是普通目标对象

芬佐克斯:

我是Spring MVC的新手,正在制作一个简单的待办事项Web应用程序。绑定数据时出现以下错误。我相信,带有Springmvc形式的jsp文件中有些东西会弄乱绑定过程。我假设bindingresult将再次返回该表单,但是由于某种原因它不会返回。

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'todo' available as request attribute
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1257)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

我的updateToDo.jsp表单

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>update task</title>
<link href="webjars/bootstrap/4.5.0/css/bootstrap.min.css"
    rel="stylesheet">

</head>
<body>
    <div class="container">
        <H1>update your task!</H1>
    
        <form:form method="POST" commandName="todo">
            
            <fieldset class="form-group">
                <form:label path="description">Description:</form:label>
                <!-- required validates nulll -->
                <form:input path="description" type="text" class="form-control"
                    required="required" />
                <form:errors path="description" cssClass="text-warning" />
            </fieldset>

            <fieldset class="form-group">
                <form:label path="targetDate">Target Date</form:label>
                <form:input path="targetDate" type="Date" class="form-control"
                    required="required" />
                <form:errors path="targetDate" cssClass="text-warning" />

            </fieldset>
            <fieldset class="form-group">
                <form:radiobutton path="completion" value="true" />
                <form:radiobutton path="completion" value="false" />
                <form:errors path="targetDate" cssClass="text-warning" />
            </fieldset>

            <button type="submit" class="btn btn-success">Submit Update</button>
        </form:form>
        
    </div>
    <script src="webjars/jquery/3.5.1/jquery.min.js"></script>
    <script src="webjars/bootstrap/4.5.0/js/bootstrap.min.js"></script>

</body>
</html>

控制者

@Controller
public class ToDoController {

@Autowired
private ToDoService service;

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("mm/DD/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}

@RequestMapping(value = "/list-todo", method= RequestMethod.GET)
// HttpSession allows access to the session
public String showToDo(ModelMap model,  HttpSession httpSession) {
    String user = (String) httpSession.getAttribute("name");
    model.addAttribute("todos", service.retrieveTodos(user));
    return "list-todos";
}

// redirect to update form
@RequestMapping(value = "/update-todo", method= RequestMethod.GET)
public String getUpdateForm(ModelMap model, @RequestParam int id) {
    // To work with command bean
    model.addAttribute("todo", service.retrieveTodo(id));
    model.clear();
    return "updateToDo";
}

@RequestMapping(value = "/update-todo", method= RequestMethod.POST)
public String submitUpdate(ModelMap model, @Valid ToDo todo, BindingResult result) {
    if (result.hasErrors()) {
        return "redirect:/update-todo";
    }
    service.updateToDo(todo);
    model.clear();
    return "redirect:/list-todo";
}

// Will be executed first
@RequestMapping(value = "/add-todo", method= RequestMethod.GET)
public String showAddForm(ModelMap model) {
    model.addAttribute("todo", new ToDo());
    return "addToDo";
}


/*
 * Will be executed after form is submitted
 * @Valid ToDo - command bean from addToDo.jsp. 
 * @Valid to validate the information
 * @BindingResult showcases the result of the validation
 */
@RequestMapping(value = "/add-todo", method= RequestMethod.POST)
public String submitAddForm(ModelMap model , @Valid ToDo todo,  HttpSession httpSession, BindingResult result) {
    System.out.println("running" + result);
    // If there is validation error , return to addToDos page for user to fix the error
    if (result.hasErrors()) {
        return "redirect:/showAddForm";
    }
    String user = (String) httpSession.getAttribute("name");
    service.addTodo(user, todo.getDescription(), todo.getTargetDate(), false);      
    // Clears the url e.g. name?=jyj123
    model.clear();
    // return to the url which executes the showToDO
    return "redirect:/list-todo";
}

    // delete to do entry
 @RequestMapping(value = "/delete-todo", method= RequestMethod.GET) 
 public String deleteToDo(ModelMap model, @RequestParam int id) { 
     service.deleteTodo(id);
     model.clear();
     return "redirect:/list-todo"; }

}

马哈茂德峰:

用外行的话来说,这就是Spring MVC的工作方式-

  • 创建一个GET要绑定数据的bean(通常在处理程序中)。(此bean称为命令对象

  • 将命令对象放入模型

  • 返回视图名称(Spring将解析视图并将模型发送到视图)

  • 将数据(用户输入)绑定到命令对象

  • 在控制器(POST或其他类似的处理程序)中使用数据检索命令对象


解决问题的方法:

你在呼唤model.clear();你的GET处理程序。因此,在视图层中,模型为空,并且没有目标Bean(即命令对象)可用于将数据绑定到该目标Bean

因此,删除mode.clear();通话。

注意

另一个常见的错误是在模型中和的值中commandName命令对象的键使用不同的名称<form:form/>

即,如果您将命令对象放入模型中model.put("fooBar", someFooBar);,则您需要执行<form:form commandName="fooBar" .../>


进一步阅读

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.lang.IllegalStateException:Bean名称“ versetmonth”的BindingResult和普通目标对象都不能用作请求属性

java.lang.IllegalStateException:Bean名称“会话”的BindingResult和普通目标对象都不能用作请求属性

是什么原因导致“java.lang.IllegalStateException:无论BindingResult也不是为bean名称‘命令’平原目标对象可以作为请求属性”?

是什么原因导致“java.lang.IllegalStateException:无论BindingResult也不是为bean名称‘命令’平原目标对象可以作为请求属性”?

Thymeleaf Spring MVC表单-Bean名称既不是BindingResult也不是普通目标对象

Thymeleaf既不是BindingResult也不是bean名称的普通目标对象

java.lang.IllegalStateException:BindingResult和普通目标对象都不能用作请求属性

java.lang.IllegalStateException:BeanResult'ecole'的BindingResult和普通目标对象都不能用作请求属性

Spring Boot:java.lang.IllegalStateException:BeanResult'edit'的BindingResult或普通目标对象都不能用作请求属性

java.lang.IllegalStateException:BeanResult的BindingResult和普通目标对象都不可用作请求属性

java.lang.IllegalStateException:BeanResult'LoginPage'的BindingResult或普通目标对象都不能用作请求属性

java.lang.IllegalStateException:BeanResult'display'的BindingResult和普通目标对象都不可用作请求属性

java.lang.IllegalStateException:BeanResult'register'的BindingResult和普通目标对象都不能用作请求属性

java.lang.IllegalStateException:BeanResult'userForm'的BindingResult和普通目标对象都不能用作请求属性

java.lang.IllegalStateException:不是JSON对象

BeanResult'command'既不是BindingResult也不是普通目标对象。春季例外

HTTP状态500-java.lang.IllegalStateException:BeanResult'app'的BindingResult和普通目标对象都不能用作请求属性

解析json错误:java.lang.IllegalStateException:不是JSON对象:

java.lang.IllegalStateException:不是 JSON 对象:Azure 操作上的 []

简单的Java Web验证Bean名称“数字”的BindingResult和普通目标对象都不能用作请求属性

java.lang.IllegalStateException:这不是JSON数组

java.lang.illegalStateException:牛刀

EJB java.lang.IllegalStateException

java.lang.IllegalStateException:打开

java.lang.IllegalStateException牛刀

Hibernate:java.lang.IllegalArgumentException:对象不是声明类的实例

JPA:java.lang.IllegalArgumentException:不是实体

tensorflow java api 错误:java.lang.IllegalStateException:张量不是标量

java.lang.IllegalStateException:目标主机为空