标签:spring-boot thymeleaf spring java spring-mvc
我选择学习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.
标签:spring-boot,thymeleaf,spring,java,spring-mvc 来源: https://codeday.me/bug/20191111/2017200.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。