当前位置: 首页 > 知识库问答 >
问题:

Bean属性“input title”不可读或具有无效的getter方法:getter的返回类型与setter的参数类型匹配吗?

姚臻
2023-03-14

我试图使用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)

提前谢谢你花时间帮我看这个。

共有1个答案

彭畅
2023-03-14

BalusC是对的,模型中的名称需要与JSP中的路径名称匹配。

 类似资料: