java-NotReadablePropertyException:Bean类的无效属性“名称”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-NotReadablePropertyException:Bean类的无效属性“名称”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4252字,纯文字阅读大概需要7分钟。
内容图文
我选择学习Java和Spring MVC Web框架来创建Spring Boot Application.
目标是创建一个简单的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引发的异常.
> 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;
}
}
Java代码使Spring Boot运行
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表单?
解决方法:
您对getter getNames(String name)有问题. Thymeleaf期望零时需要一个参数-只是找不到合适的吸气剂.
签名应更改为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-NotReadablePropertyException:Bean类的无效属性“名称”全部内容,希望文章能够帮你解决java-NotReadablePropertyException:Bean类的无效属性“名称”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。