gpt4 book ai didi

java - 不可读属性异常 : Invalid property 'names' of bean class

转载 作者:搜寻专家 更新时间:2023-11-01 02:58:18 24 4
gpt4 key购买 nike

我选择学习 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 Controller 代码 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 对象注入(inject)到 html 表单中而不会出现该异常?

最佳答案

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

签名应改为getNames()

如果你看过 the javadocs ,你可以看到:

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

关于java - 不可读属性异常 : Invalid property 'names' of bean class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45618278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com