2

I'm an beginner in Spring MVC and I can't figure out why I keep getting the same error:

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

Can anyone help?

Thanks in advance

JSP code:

    <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:

    @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");
}
}

Dispatcher:

     <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;
  }  

}

I'm getting the problem: when i do a GET request

7
  • For your object (defined in your form as modelAttribute) "Article", does it have "articleName" property as you have defined in your <form:input path="articleName"/> ? Commented Nov 26, 2011 at 21:43
  • It will be easier to help you if you post your Article object code here and if you can clarify whether you are getting the error during GET or POST. Commented Nov 26, 2011 at 21:50
  • 3
    Try this for your 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); } Commented Nov 26, 2011 at 22:51
  • Still got the same error. Thanks anyway Commented Nov 26, 2011 at 23:33
  • 1
    Glad you found the solution! Enjoy the feeling!! Commented Nov 27, 2011 at 1:55

2 Answers 2

2

It is proven I'm stupid

It is:

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

it should be:

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

same for articleDesc

Let's be honest, I found the solution thanks to: Stealth

Sign up to request clarification or add additional context in comments.

Comments

1
 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;
    }

1 Comment

could you add some context to your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.