我正在尝试使用Spring MVC向表单添加验证,并且我不断收到Http Status 500错误,我感到困惑,找不到根本原因。每次我修改它的希望稍微改变一下,我会得到一个不同的错误,但它们最终都是500,而且似乎都指向了jsp文件(此时我不知道'我认为是问题的原因,但那是我得到的错误)。如果我从表单和输入中删除sf:
标记,则错误会消失,但验证表单的能力也会消失。
这是(据我所知)我的pom.xml(已编辑)
的相关依赖项<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
这是jsp文件本身
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Company Contacts</title>
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet"> <!-- Bootstrap core CSS -->
<link rel="shortcut icon" href="${pageContext.request.contextPath}/favicon-32x32.png"> <!-- SWC Icon -->
</head>
<body>
<div class="container">
<div class="form-horizontal">
<h1>My DVDs</h1>
</div>
<hr/>
<div class="form-horizontal">
<h1>New DVD Form</h1>
<a href="displayDvdCollection">Back to DVD Collection</a>
<hr/>
<sf:form class="form-horizontal" role="form" modelAttribute="dvd" action="addNewDvdToCollection" method="post">
<div class="form-group">
<label for="add-title" class="col-md-4 control-label">Title: </label>
<div class="col-md-8">
<sf:input type="text" cssClass="form-control" path="inputTitle" placeholder="Title" />
<sf:errors path="inputTitle" />
</div>
</div>
<div class="form-group">
<label for="add-releaseDate" class="col-md-4 control-label">Release Date: </label>
<div class="col-md-8">
<sf:input type="date" cssClass="form-control" path="inputReleaseDate" placeholder="Release Date" />
<sf:errors path="inputReleaseDate" />
</div>
</div>
<div class="form-group">
<label for="add-mpaaRating" class="col-md-4 control-label">MPAA Rating: </label>
<div class="col-md-8">
<sf:input type="text" cssClass="form-control" path="inputMpaaRating" placeholder="MPAA Rating" />
<sf:errors path="inputMpaaRating" />
</div>
</div>
<div class="form-group">
<label for="add-director" class="col-md-4 control-label">Director: </label>
<div class="col-md-8">
<sf:input type="text" cssClass="form-control" path="inputDirector" placeholder="Director" />
<sf:errors path="inputDirector" />
</div>
</div>
<div class="form-group">
<label for="add-studio" class="col-md-4 control-label">Studio: </label>
<div class="col-md-8">
<sf:input type="text" cssClass="form-control" path="inputStudio" placeholder="Studio" />
<sf:errors path="inputStudio" />
</div>
</div>
<div class="form-group">
<label for="add-note" class="col-md-4 control-label">Note: </label>
<div class="col-md-8">
<input type="text" class="form-control" id="add-note" path="inputNote" placeholder="Note" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-8">
<button type="submit" id="add-button" class="btn btn-default">Add New DVD</button>
</div>
</div>
</sf:form>
</div>
</div>
<!-- Placed at the end of the document so the pages load faster -->
<script src="${pageContext.request.contextPath}/js/jquery-1.11.1.min.js"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
</body>
</html>
以下是我的模型中的注释:
@NotEmpty(message="Title is required")
@Length(max=50, message="Title cannot exceed 50 characters")
private String title;
@DateTimeFormat(pattern="MM-dd-yyyy")
@NotNull(message="Release date is required")
private Date releaseDate;
@NotEmpty(message="Rating is required")
@Length(max=5, message="Rating options are G, PG, PG-13, R, NC-17")
private String mpaaRating;
@NotEmpty(message="Director is required")
@Length(max=50, message="Director's name cannot exceed 50 characters")
private String director;
@NotEmpty(message="Studio is required")
@Length(max=50, message="Studio's name cannot exceed 50 characters")
private String studio;
private String note;
以下是我的控制器的相关方法
@ModelAttribute("dvd")
public Dvd getDvdObject() {
return new Dvd();
}
@RequestMapping("/addNewDvdToCollection")
public String validateAdd(Model model, Dvd thisDvd) {
model.addAttribute("wigwam", thisDvd);
return "newDvdForm";
}
@RequestMapping(value = "/addNewDvdToCollection", method = RequestMethod.POST)
//modelattr, dvd, bindingres, model
public String addNewDvdToCollection(Model model, @Valid @ModelAttribute("dvd") HttpServletRequest request, BindingResult result, Dvd thisDvd) {
thisDvd = new Dvd();
if (result.hasErrors()) {
return validateAdd(model, thisDvd);
}
try {
DateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
thisDvd.setTitle(request.getParameter("inputTitle"));
thisDvd.setReleaseDate(sdf.parse(request.getParameter("inputReleaseDate")));
thisDvd.setMpaaRating(request.getParameter("inputMpaaRating"));
thisDvd.setDirector(request.getParameter("inputDirector"));
thisDvd.setStudio(request.getParameter("inputStudio"));
thisDvd.setNote(request.getParameter("inputNote"));
} catch (ParseException e) {
e.getStackTrace();
}
dao.add(thisDvd);
return "redirect:displayDvdCollection";
}
最后这是我上次尝试通过按钮点击访问addDvdForm.jsp
时的错误输出
org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/newDvdForm.jsp at line 27
24: <div class="form-group">
25: <label for="add-title" class="col-md-4 control-label">Title: </label>
26: <div class="col-md-8">
27: <sf:input type="text" cssClass="form-control" path="inputTitle" placeholder="Title" />
28: <sf:errors path="inputTitle" />
29: </div>
30: </div>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1217)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.springframework.beans.NotReadablePropertyException: Invalid property 'inputTitle' of bean class [com.swcguild.dvdlibrarymvc.model.Dvd]: Bean property 'inputTitle' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:725)
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:716)
org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:229)
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.jsp.newDvdForm_jsp._jspx_meth_sf_005finput_005f0(newDvdForm_jsp.java:386)
org.apache.jsp.jsp.newDvdForm_jsp._jspService(newDvdForm_jsp.java:158)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1217)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
提前感谢您抽出时间为我查看此内容。
答案 0 :(得分:0)
BalusC是对的,模型中的名称需要匹配JSP中的路径名称。