【问题标题】:NotReadablePropertyException: Invalid property 'names' of bean classNotReadablePropertyException:bean 类的无效属性“名称”
【发布时间】:2018-01-18 23:20:26
【问题描述】:

我选择学习 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代码如下

<!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>

我的 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 表单中,而不会出现该异常?

【问题讨论】:

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


    【解决方案1】:

    FormFields 类中的 getter 不正确。 只是一个小错字,您在方法名称中缺少“s”。

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

    【讨论】:

    • 谢谢....这是一个快速的检查! :D。 Andrew 对引起的异常添加了一个很好的小解释。
    【解决方案2】:

    您的 getter getNames(String name) 有问题。它需要一个参数,而 Thymeleaf 期望为零 - 它只是找不到合适的 getter。

    签名应改为getNames()

    如果您查看the javadocs,您会看到:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 2021-10-07
      • 2012-01-20
      • 2016-01-14
      相关资源
      最近更新 更多