当前位置: 首页 > 知识库问答 >
问题:

BindingResult和bean的普通目标对象都不能用作请求属性

程祺
2023-03-14
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

@Controller
public class PizzaController {

  @Autowired private PizzaDao pizzaDao;

  @RequestMapping(method=RequestMethod.GET, value="/")
  public String indexPage(Model model) {
    List<Pizza> pizzas = pizzaDao.getAll();
    model.addAttribute("pizzas", pizzas);
    return "index";
  }
  @RequestMapping(method=RequestMethod.GET, value="Pizza/pizzalist")
  public String pizzalist(Model model) {
    List<Pizza> pizzas = pizzaDao.getAll();
    model.addAttribute("pizzas", pizzas);
    return "pizzalist/pizzalist";
  }
  @RequestMapping(method=RequestMethod.POST, value="Pizza/pizzalist/add")
  public String add(@ModelAttribute("mypizza") Pizza pizza) { //, BindingResult result,         SessionStatus status, ModelMap mmap) {
    //mmap.addAttribute("pizza",pizza);
    pizzaDao.add(pizza);
    //status.setComplete();
    return "pizzalist/pizzalist";
  }
  @RequestMapping(method=RequestMethod.GET, value="Pizza/pizzalist/delete")
  public String delete(@RequestParam("id")long id, Model model) {
    pizzaDao.delete(id);
    List<Pizza> pizzas = pizzaDao.getAll();
    model.addAttribute("pizzas", pizzas);
    return "pizzalist/pizzalist";
  }
  @RequestMapping(method=RequestMethod.POST, value="Pizza/edit")
  public String edit(Model model) {
    List<Pizza> pizzas = pizzaDao.getAll();
    model.addAttribute("pizzas", pizzas);
    return "pizzalist/pizzalist";
  }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/jsp/includes.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Available pizzas</title>
</head>
<body>
    <h1>List of All Pizzas</h1>
    <table border="1">
        <c:forEach var="p" items="${pizzas}">
            <tr>
                <td>${p.name}</td><td>${p.price}</td><td><a href="pizzalist/delete?id=${p.id}">Delete</a></td>
            </tr>
        </c:forEach>
    </table>
    <br />
    <h1>Add new pizza</h1>
    <form:form modelAttribute="mypizza" method="POST" action="pizzalist/add">
    <table>
        <tr>
            <td>Name</td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td>Price</td>
            <td><form:input path="price" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="action" value="Add" />
            </td>
        </tr>
    </table>
    </form:form>
    <br />
    <a href="/pizzashop">Go back to home page</a>
</body>
</html>

当我只留下表显示可用的比萨饼与链接“删除”,一切显示正常,删除也正常工作。

我想表单和控制器@modelAttribute有问题,但我找不到确切的问题,我在controller中添加了与表单相同的属性--“mypiza”,这应该可以工作(至少从我在Google上找到的东西来看)。

有人能告诉我们是什么阻碍了这个应用程序的工作,以及如何解决这个问题吗?如果需要其他配置文件,我可以提供它们。

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'mypizza' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:127)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.WEB_002dINF.jsp.pizzalist.pizzalist_jsp._jspx_meth_form_005finput_005f0(pizzalist_jsp.java:243)
org.apache.jsp.WEB_002dINF.jsp.pizzalist.pizzalist_jsp._jspx_meth_form_005fform_005f0(pizzalist_jsp.java:194)
org.apache.jsp.WEB_002dINF.jsp.pizzalist.pizzalist_jsp._jspService(pizzalist_jsp.java:103)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
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:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

共有1个答案

杭柏
2023-03-14

关于这种方法:

@RequestMapping(method=RequestMethod.GET, value="/")
  public String indexPage(Model model) {
    List<Pizza> pizzas = pizzaDao.getAll();
    model.addAttribute("pizzas", pizzas);
    return "index";
  }

您应该添加以下行:

model.addAttribute("mypizza", new Pizza());

form:form需要一个与您定义的modelAttribute相对应的“空”模型。

 类似资料: