我是Spring MVC的新手,我在表单验证时遇到错误,我不知道原因。
我在网上查看了几乎所有与此问题相关的答案,但无法在我的代码中找出问题。
这是我的JSP页面。
<div class="modal-body">
<f:form method="POST" commandName="categorie" enctype="multipart/form-data"
action="categorie/add" class="pure-form pure-form-aligned">
<%-- <f:form action="addcategorie" method="post"> --%>
<p><label for="nomcategorie">Nom catégorie </label><f:input type="text" id="nomcatg" path="nomcategorie"/></p>
<p><label for="typecategorie">Type catégorie </label><f:input type="text" id="typecatg" path="typecategorie"/></p>
<div class="pure-control-group">
<label for="image">Image</label>
<%-- <c:if test="${famille.idfamille!=null}">
<c:if test="${famille.image!=null}">
<img alt="" src="photofamille?idfam=${famille.idfamille }" class="img">
</c:if>
</c:if> --%>
<!-- <br>
<br> -->
<input type="file" name="file" >
</div>
<br>
<div class="pure-control-group">
<label for="couleurfond">Couleur de Fond</label>
<f:input type="color" name="your favourite color" pattern="#[a-f0-9]{9}" value="#fad345" list="" path="couleur"/>
<!-- Color: <input class="jscolor" value="ab2567"> -->
</div>
<f:input path="idcategorie" type="text" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" >Save changes</button>
</div>
</f:form>
我的控制员:
@Controller
public class CategorieController {
@Autowired
CategorieService categorieService;
@RequestMapping(value = "/categories", method = RequestMethod.GET)
public String liste(Model model,HttpServletRequest request) {
// model.addAttribute("article", new Article());
model.addAttribute("categories", categorieService.listCategories());
return "listCategories";
}
@RequestMapping(value= "/categorie/add",method = RequestMethod.POST,headers="content-type=multipart/*")
public String addFamille(@ModelAttribute("categorie") Categorie c,BindingResult bindingResult, Model model,MultipartFile file) throws Exception{
model.addAttribute("categorie", new Categorie());
if(c.getIdcategorie() == null){
categorieService.addCategorie(c);
}
return "redirect:/categories";
}
}
我发现了这个错误:
RAVE: "Servlet.service()" pour la servlet jsp a lancé une exception
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'categorie' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:117)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.listCategories_jsp._jspx_meth_f_005finput_005f0(listCategories_jsp.java:757)
at org.apache.jsp.WEB_002dINF.views.listCategories_jsp._jspx_meth_f_005fform_005f0(listCategories_jsp.java:683)
at org.apache.jsp.WEB_002dINF.views.listCategories_jsp._jspService(listCategories_jsp.java:538)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
févr. 08, 2016 2:57:15 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [appServlet] in context with path [/elolink] threw exception [An exception occurred processing JSP page /WEB-INF/views/listCategories.jsp at line 417
414: <f:form method="POST" commandName="categorie" enctype="multipart/form-data"
415: action="categorie/add" class="pure-form pure-form-aligned">
416: <%-- <f:form action="addcategorie" method="post"> --%>
417: <p><label for="nomcategorie">Nom cat�gorie </label><f:input type="text" id="nomcatg" path="nomcategorie"/></p>
418: <p><label for="typecategorie">Type cat�gorie </label><f:input type="text" id="typecatg" path="typecategorie"/></p>
419: <div class="pure-control-group">
420: <label for="image">Image</label>
问题是什么,请帮我解决一下
答案 0 :(得分:0)
您的JSP正在寻找名为categorie.
的绑定bean您可以在liste
方法中向表单添加新bean。
也就是说,将此行移至您的GET方法:
model.addAttribute("categorie", new Categorie());
然后,确保在Categorie
bean中找到页面上的所有属性。
此外,更新的方法是使用modelAttribute
代替commandName
。
您可以查看this post类似情况。