Search code examples
javaspringspring-mvcspring-bootthymeleaf

NotReadablePropertyException: Invalid property 'names' of bean class


I opt to learn java and the spring MVC web framework to create a Spring Boot Application.

The goal is to create a simple html form and pass a java object to the form.

Note: The template engine I am using is Thymeleaf

This is the exception raised in String Toolkit Suite in my situation.

org.springframework.beans.NotReadablePropertyException: Invalid property 'names' of bean class [testingmvc.FormFields]: Bean property 'names' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

I found a question NotReadablePropertyException: Invalid property 'moduleName' of bean class which raised a similar issue but this was an exception raised by java.lang.String in the link.

  1. The html code is below

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<head lang="en">
  <title>Search</title>
</head>

<body>
  <h4 class="indigo-text center">Please enter a search term</h4>
  <form action="/post_search" th:object="${fields}" method="POST">
    <div class="row center">
      <div class="input-field col s6 offset-s3">
        <i class="mdi-action-search prefix"></i>
        <input id="names" th:field="*{names}" name="names" type="text" class="validate" placeholder="please enter your name" />
        <input id="address" name="address" type="text" class="validate" th:field="*{address}" placeholder="please enter your address" />
        <label for="search">Search</label>
        <button type="submit">submit</button>
      </div>
    </div>
  </form>
</body>

</html>

My Java Controller Code Java is below

package testingmvc;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class TestController {
    @RequestMapping("/welcome")
    public ModelAndView firstPage() {
        return new ModelAndView("welcome");
    }
    @RequestMapping("/thyme")
    public ModelAndView thyme(@RequestParam( value = "product" , required=false) String qString,Model model) {
        System.out.println(qString);
        model.addAttribute("name", "John Public");  
        return new ModelAndView("sample");
    }
    @RequestMapping(value="/form_test",method = RequestMethod.GET)
    public String form(@ModelAttribute("fields") FormFields fields,BindingResult res, Model model)
    {
        model.addAttribute("fields", fields);
        return "form";
    }
    @RequestMapping(value = "/post_search", method = RequestMethod.POST)
    public String formTest(@ModelAttribute("fields") FormFields fields,BindingResult res,HttpServletRequest request,RedirectAttributes redirect, Model model){
        //System.out.println("This is request parameters: "+request.getParameterNames());
        Enumeration<String> t=request.getParameterNames();
        while(t.hasMoreElements())
        {
            System.out.println("This is request parameters: "+t.nextElement());
        }
        model.addAttribute("fields", fields);
        return "welcome";
    }

}
class FormFields{
    private String names;
    private String address;
    public void setName(String name) {
        this.names=name;

    }
    public void setAddress(String address) {
        this.address=address;

    }   
    public String getName(String name) {
        return this.names;

    }   
    public String getAddress(String address) {
        return this.address;

    }       
}

Java Code to Get Spring Boot Running

package testingmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BootGradleApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootGradleApplication.class, args);
    }
}

Question

I would like to understand with a working example. How can I successfully inject the Java Object into the html form without that exception?


Solution

  • You have a problem with the getter getNames(String name). It takes one argument while Thymeleaf expects zero - it just couldn't find a proper getter.

    The signature should be changed to getNames().

    If you looked at the javadocs, you could see:

    Exception thrown on an attempt to get the value of a property that isn't readable, because there's no getter method.