NotReadablePropertyException:无效的属性'名称'豆类

时间:2017-08-10 15:50:19

标签: java spring spring-mvc spring-boot thymeleaf

我选择学习java和spring MVC web框架来创建Spring Boot应用程序。

目标是创建一个简单的html表单并将java对象传递给表单。

注意:我使用的模板引擎是Thymeleaf

在我的情况下,这是String Toolkit Suite中引发的异常。

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?

我发现问题NotReadablePropertyException: Invalid property 'moduleName' of bean class引发了类似的问题,但这是java.lang.String在链接中引发的异常。

  1. html代码位于
  2. 之下

    
    
    <!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>
    &#13;
    &#13;
    &#13;

    我的Java控制器代码Java低于

    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;
    
        }       
    }
    

    获取Spring Boot运行的Java代码

    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);
        }
    }
    

    问题

    我想通过一个有效的例子来理解。如何在没有异常的情况下成功地将Java对象注入到html表单中?

2 个答案:

答案 0 :(得分:2)

您遇到了问题getNames(String name)。当Thymeleaf期望为零时,它需要一个参数 - 它只是找不到合适的吸气剂。

签名应更改为getNames()

如果您查看the javadocs,可以看到:

  

尝试获取不可读的属性值时抛出异常,因为没有getter方法。

答案 1 :(得分:1)

FormFields课程中的getter不正确。 只是一个小错字,你在方法名称中缺少''。

吸气者不应该接受任何争论。

以下是有效版本:

class FormFields{
    private String names;
    private String address;

    public void setNames(String names) {
        this.names=names;

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

    }   
    public String getNames() {
        return this.names;

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

    }       
}