Eclipse – Bean property ’empname’ is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter

eclipsespring-bootspring-mvcthymeleaf

I am trying to perform a simple submit operation from a form. I use spring boot framework with thyme leaf template for my project. Language used is java in eclipse IDE.

All I am looking to do is to take the empname and empid (refer Employee class) from the form and store it in a java object.

When I run the application, the application opens and when i navigate to edit.html, i get this error message in the browser –

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jun 18 16:14:40 EDT 2018
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/edit.html]")

I also get this error message on the console –

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'empname' of bean class [com.cardinalcommerce.model.Employee]: Bean property 'empname' 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) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:129) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:252) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:226) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
… 67 common frames omitted

This is my snippet of the html document where the error occurs.

<form class="form-horizontal" action="#" th:action="@{/employee/edit}" th:object="${employee}" method="POST">

    <div class="form-group">
      <label class="control-label col-sm-3">File Prefix:</label>
      <div class="col-sm-7">
        <input type="text" class="form-control" th:field="*{empname}"  placeholder="Enter employee name" />
      </div>
    </div>

    <div class="form-group">
      <label class="control-label col-sm-3">File Prefix:</label>
      <div class="col-sm-7">
        <input type="text" class="form-control" th:field="*{empid}"  placeholder="Enter the employee ID" />
      </div>
    </div>

    <div class="form-group">        
      <div class="col-sm-offset-3 col-sm-7">
        <button type="submit" class="btn btn-default" id="blackButton" th:value="Submit">Submit</button>
        <button type="reset" class="btn btn-default" id="blackButton" th:value="Reset">Cancel</button>
      </div>
    </div>  

This is my class where with the setters and getters –

public class Employee {
    private String empid;
    private String empname;

    public String getEmployeeId() {
        return empid;
    }
    public void setEmployeeId(String empid) {
        this.empid = empid ;
    }
    public String getEmployeeName() {
        return empname;
    }
    public void setEmployeeName(String empname) {
        this.empname = empname;
    }
}

This is the controller snippet –

@Controller
    @RequestMapping(value="/")
    public class GreetingController {

    private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);

    @Autowired
    private SomeRecord someRecord;

    @GetMapping("/")
    public String greeting() {

        return "about";
    }

    @RequestMapping("/about")
    public String about() {

        return "about";
    }

    @GetMapping("/edit")
    public ModelAndView edit() {
        ModelAndView modelAndView = new ModelAndView("edit");
        modelAndView.addObject("employee", new Employee());

        return modelAndView;
    }

    @PostMapping("/edit")
    public ModelAndView createRecord(@Valid Employee employee, BindingResult result) {
        ModelAndView modelAndView = new ModelAndView();
        if (result.hasErrors()) {
            logger.info("Validation errors while submitting form.");
            modelAndView.setViewName("CreateRecord");
            modelAndView.addObject("employee", employee);

            return modelAndView;
        }
        someRecord.addRecord(employee);
        modelAndView.addObject("allRecords", someRecord.getAllRecordData());
        modelAndView.setViewName("recordsInfo");
        logger.info("Form submitted successfully.");
        return modelAndView;
    }


    @GetMapping("/view")
    public String view() {

        return "view";
    }

}

Let me know if anything else is required.
Thanks for your help.

Best Answer

You should use *{employeeName} and *{employeeId} rather than *{empname} and *{empid}. (Matching the getters and setters, rather than your private variables.)

Related Topic