【问题标题】:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'beanName' available as request attributejava.lang.IllegalStateException:Bean 名称“beanName”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2011-11-26 13:27:09
【问题描述】:

我是 Spring MVC 的初学者,但我不知道为什么我总是遇到同样的错误:

java.lang.IllegalStateException:既不是BindingResult也不是plain bean 名称“articleName”的目标对象可作为请求提供 属性

谁能帮忙?

提前致谢

JSP 代码:

    <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add article</title>
</head>
<body>
    <h1>Add article</h1>

    <c:url var="viewArticlesUrl" value="/articles.htm"/>
    <a href="${viewArticlesUrl}">Show all Articles</a>
    <br/><br/>
    <c:url var="saveArticleUrl" value="/articles/save.htm"/>
<from:form modelAttribute="article" method="POST" action="${saveArticleUrl}">
    <form:label path="articleName">Article name:</form:label>
    <form:input path="articleName"/>       
    <br />
    <form:label path="articleDesc">Article Desc:</form:label>
    <form:textarea path="articleDesc" />
    <br />

    <input type="submit" value="Save Article" />
</from:form>
</body>

控制器:

    @Controller
    @RequestMapping("/articles")
    public class ArticleController {

@Autowired
private ArticleService articleService;

@RequestMapping(value="/save", method=RequestMethod.POST)
public ModelAndView saveArticle(@ModelAttribute("article")Article article, BindingResult result){
    articleService.addArticle(article);
    return new ModelAndView("redirect:/articles.html");
}

@RequestMapping(method=RequestMethod.GET)
public ModelAndView listArticles(){
    Map<String, Object> model=new HashMap<String, Object>();
    model.put("articles", articleService.listArticles());

    return new ModelAndView("articlesList", model);
}

@RequestMapping(value="/add", method=RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article, BindingResult result){
    return new ModelAndView("addArticle");
}
}

调度员:

     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
            <prop key="articles.htm">articleController</prop>
            <prop key="articles/add.htm">articleController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->

 <bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

<!-- article controller -->
<bean name="articleController" class="controller.ArticleController"/>

           @Entity
   @Table(name="article")
  public class Article {
@Id
@GeneratedValue
@Column(name="article_id")
private Long articleId;

@Column(name="article_name", nullable=false, length=20)
private String articleName;

@Column(name="article_desc", nullable=false)
private String articleDesc;

@Column(name="date_added")
@Temporal(javax.persistence.TemporalType.DATE)
private Date addedDate;

  public Article() {    
  }

  public Long getArticleId() {
    return articleId;
  }

  public void setArticleId(Long articleId) {
    this.articleId = articleId;
  }

  public String getArticleName() {
    return articleName;
  }

  public void setArticleName(String articleName) {
        this.articleName = articleName;
  }

  public String getArticleDesc() {
    return articleDesc;
  }

  public void setArticleDesc(String articleDesc) {
    this.articleDesc = articleDesc;
  }

  public Date getAddedDate() {
    return addedDate;
  }

  public void setAddedDate(Date addedDate) {
    this.addedDate = addedDate;
  }  

}

我遇到了问题:当我发出 GET 请求时

【问题讨论】:

  • 对于您的对象(在您的表单中定义为 modelAttribute)“Article”,它是否具有您在 中定义的“articleName”属性?跨度>
  • 如果您在此处发布您的文章对象代码,并且您可以澄清您是否在 GET 或 POST 期间收到错误,将会更容易为您提供帮助。
  • 为你的 addArticle 试试这个:@RequestMapping(value="/add", method=RequestMethod.GET) public ModelAndView addArticle(@ModelAttribute("article") Article article, BindingResult result, ModelMap map) { map.put("article", article"); return new ModelAndView("addArticle", map); }
  • 仍然出现同样的错误。还是谢谢
  • 很高兴您找到了解决方案!享受这种感觉!

标签: jsp spring-mvc


【解决方案1】:

事实证明我很愚蠢

它是:

  <form:label path="articleName">Article name:</form:label> 
  <form:input path="articleName"/> 

应该是:

  <form:label path="article.articleName">Article name:</form:label> 
  <form:input path="article.articleName"/>

articleDesc 相同

说实话,我找到了解决方案,感谢:Stealth

【讨论】:

    【解决方案2】:
     protected ModelAndView onSubmit(HttpServletRequest request,
                HttpServletResponse response, Object command, BindException errors) throws Exception {
    
            System.out.println("On Submit");
    
            ModelAndView mv = super.onSubmit(command, errors);
            mv.addObject("result","success");
            return mv;
        }
    

    【讨论】:

    • 您能在回答中添加一些上下文吗?
    猜你喜欢
    • 2016-02-17
    • 2020-09-26
    • 2018-01-07
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多