我试图使用Spring MVC将验证添加到表单中,但我总是得到一个Http Status500错误,我很难找到根本原因。每次我为了修复它而做一个小的更改时,我都会得到一个不同的错误,但它们最终都是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)
提前谢谢你花时间帮我看这个。
BalusC是对的,模型中的名称需要与JSP中的路径名称匹配。
这是控制器: 这是要编辑的jsp页面: 我试图编辑一个jsp页面,但我无法编辑。它显示了getters和setters方法的错误。欢迎提出任何建议。提前感谢!KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
我试图从一个表单执行一个简单的提交操作。我的项目使用带有百里香叶模板的Spring引导框架。使用的语言是eclipse IDE中的java。 我要做的就是从表单中获取empname和empid(参考员工类)并将其存储在java对象中。 当我运行应用程序时,应用程序打开,当我导航到edit.html时,我在浏览器中收到这条错误消息- Whitelabel错误页此应用程序没有/Error的显式映射,因
问题内容: 我正在开发示例。我开发了XML文件并能够成功编译代码,但是在运行mu示例代码时出现以下错误。不知道出了什么问题? jdbc-composite-item-reader-job.xml Employee.java 客户.java CompositeMain.java 我的数据库详细信息: 我看到问题已经存在:http : //forum.spring.io/forum/spring- p
现在在用spring框架spring boot version = ' 1 . 5 . 9 . release '做java。但是每次我运行我的网络应用程序时 org.springframework.beans.NotReadable属性异常: bean类的无效属性'id'[java.util.ArrayList]: Bean属性'id'不可读或具有无效的getter方法:getter的返回类型是
我是Spring的新手,我有个例外。 我有一个jsp,有很多选择。它们都工作正常,但有一个显示了这个例外: 我正在以与其他工作正常的人相同的方式实施selct。下面是jsp代码: 这里是控制器: 这里,实体Expediente: 最后是TipoExpedinte实体 我认为这可能发生,因为PharmantePadre属性是同一实体Pharmante的外键,但我不知道如何解决它。。。 要帮忙吗?谢谢