Как исправить NotReadablePropertyException: недопустимое свойство 'freePasses' класса компонента - PullRequest
0 голосов
/ 06 марта 2020

Я разрабатываю небольшое приложение на пружине mvc и хочу, чтобы в поле ввода были введены «свободные проходы», чтобы пользователь не мог добавить число, которое не находится в диапазоне от 0 до 10.

когда я нажимаю кнопку подтверждения, появляется следующая ошибка:

org.springframework.beans.NotReadablePropertyException: Invalid property 'freePasses' of bean class [com.luv2code.springdemo.mvc.Customer]: Bean property 'freePasses' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612)
    at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104)
    at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228)
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:129)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:149)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:139)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:460)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:357)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:87)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:83)
    at org.apache.jsp.WEB_002dINF.view.customer_002dform_jsp._jspx_meth_form_005finput_005f2(customer_002dform_jsp.java:374)

проблема в том, что я действительно не знаю, откуда эта ошибка.

это мой код класса клиента :

package com.luv2code.springdemo.mvc;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;



public class Customer {


    private String firstName;

    @NotNull(message="is required !")
    @Size(min=1, message="is required !")
    private String lastName;


    @Min(value=0, message="le champs doit avoir une valeur entre 0 et 10")
    @Max(value=10, message="le champs doit avoir une valeur entre 0 et 10 !!!")
    private int freePasses;

    public int getFreePasses() {
        return freePasses;
    }

    public void setFreePasses(int freePasses) {
        this.freePasses = freePasses;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

и это мой код класса customerController:

package com.luv2code.springdemo.mvc;

import javax.validation.Valid;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {

    StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);

    dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);


    }
    @RequestMapping("/showForm")
    public String showForm(Model theModel) {

        theModel.addAttribute("customer", new Customer());
        return "customer-form";
    }


    @RequestMapping("/processForm")
    public String processForm(@Valid @ModelAttribute("customer") Customer theCustomer, BindingResult theBindingResult) {

        System.out.println("Last name : |" + theCustomer.getLastName()+ "|");

        if(theBindingResult.hasErrors()) {
            return "customer-form";
        }

        else {
            return "customer-confirmation";
        }


    }
}

и это код моей формы клиента. jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html>

<html>

<head>

    <title>Customer form</title>
    <style type="text/css">

        .error{color:red}

    </style>

</head>

<body>

    <form:form action="processForm" modelAttribute="customer"> 

        First name: <form:input path="firstName"/>

        <br><br>
        Last name (*) : <form:input path="lastName"/>
                        <form:errors path="lastName" cssClass="error" />

        <br><br>

        Free passes : <form:input path="freePasses"/>
                      <form:errors path="freePasses" cssClass="error" />

                    <input type="submit" value="Valider" />
    </form:form>

</body>


</html>

and this is my code of customer-confirmation.jsp :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>

<!DOCTYPE html>

<html>

<head>

    <title>Customer Confirmation</title>
</head>

<body>

the customer is confirmed : ${customer.firstName} ${customer.lastName}

<br><br>

Free passes : ${customer.freePasses}

</body>

</html>

чтобы лучше объяснить, я хочу, чтобы пользователь мог вводить только число от 0 до 10 в поле бесплатных проходов, и когда он нажимает на подтвержденный, он отображает число, которое он ввел в поле ввода "свободных проходов"

Может кто-нибудь помочь мне решить эту ошибку, пожалуйста?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...