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

时间:2014-03-12 22:03:54

标签: java mysql spring hibernate

我正在尝试使用Java,Spring,Hibernate和MySQL制作简单的CRUD Web应用程序。我遇到的问题是我无法显示表单以向数据库添加新项目(比萨饼)。

这是我的控制器:

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>

当我只留下表格显示可用的带有链接&#34;删除&#34;的比萨时,一切都显示正常,删除也可以正常工作。

我觉得表单和控制器@ModelAttribute有问题,但我无法找到错误的内容,我在控制器中添加了相同的属性,如表格中所示 - &#34; mypizza&#34;这应该有效(至少从我在谷歌上找到的)。

有人可以启发什么阻止此应用程序工作以及如何解决问题?如果需要其他配置文件,我可以提供它们。

UPD:

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)

6 个答案:

答案 0 :(得分:7)

关于这个方法:

@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需要&#34;空&#34;与您定义的modelAttribute对应的模型。

答案 1 :(得分:5)

如果表单呈现时modelAttribute的{​​{1}}不可用。然后异常会引发,在你的情况下<form:form不可用,例外也是如此:

mypizza

您可以以任何您喜欢的方式添加模型bean /表单支持对象:

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

@ModelAttribute("mypizza")

在您的情况下@ModelAttribute("mypizza") public Pizza loadEmptyModelBean(){ return new Pizza(); } 选项是完美的,而不是在每个方法中添加将其添加到一个位置。


@ModelAttribute("mypizza")方法(其中@Shay Elkayam回答说的相同):

GET

如果你不这样做,那么@RequestMapping(method=RequestMethod.GET, value="/") public String indexPage(Model model) { List<Pizza> pizzas = pizzaDao.getAll(); model.addAttribute("pizzas", pizzas); model.addAttribute("mypizza", new Pizza());// model bean added to use in form return "index"; } 异常就会引发。

答案 2 :(得分:4)

对任何可能遇到此问题的人:

我需要添加

@ModelAttribute("mypizza") 

到“pizzalist”方法,现在看起来像这样:

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

另外,需要将Shay Elkayam建议的行添加到相同的方法中(我在“pizzalist”方法中的“return”之前添加)并且在开始时添加方法“添加”,现在看起来像

@RequestMapping(method=RequestMethod.POST, value="Pizza/pizzalist/add")
public String add(@ModelAttribute("mypizza") Pizza pizza, Model model) {
    model.addAttribute("mypizza", new Pizza());
    pizzaDao.add(pizza);
    List<Pizza> pizzas = pizzaDao.getAll();
    model.addAttribute("pizzas", pizzas);
    return "pizzalist/pizzalist";
}

现在查看表格并添加新项目可以正常工作。 如果有的话,我的WEB-INF文件夹结构是:

- jsp
   index.jsp
   - pizzalist
       pizzalist.jsp

感谢所有试图帮助并展示正确方法的人。

答案 3 :(得分:1)

我只想在这里加2美分。从其他页面复制时,我的一些按钮超出了</form>标记的末尾。 我有modelattribute和所有相关的东西。我的<form:button>之一<{>> <form:form>.... </form:form>标记。这太疯狂了......

只需提出这个答案,因为它可能有助于其他人。

答案 4 :(得分:1)

你想改变这个功能:-value =&#34; Pizza / pizzalist&#34;

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

这会对你有帮助。

答案 5 :(得分:0)

如果您在 JSP 中定义了 modelAttribute form ,那么在重新加载JSP之前,spring将检查该modelAttribute并抛出此类型例外情况如果没有。

在这种情况下,您必须首先在控制器中设置此modelAttribute以防止此类异常。

model.addAttribute("myModelAttribute", new MyModelAttribute());