我是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请求时
答案 0 :(得分:2)
事实证明我是愚蠢的
是:
<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
答案 1 :(得分: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;
}