2017-08-10 145 views
2

我选择学习java和Spring MVC Web框架来创建Spring Boot应用程序。NotReadablePropertyException:bean类的无效属性“名称”

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

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

这是我的情况在字符串工具包套房引发的异常。

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; 

    }  
} 

的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

你有问题,吸气getNames(String name)。当Thymeleaf期望为零时,需要一个论证 - 它只是找不到一个合适的获取者。

签名应改为getNames()

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

抛出异常就尝试着让不是读取的属性值,因为没有getter方法。

1

您在FormFields类中的吸气器不正确。 只是一个小错字,你在方法名中缺少''。

而且getters不应该采取任何论点。

这里是一个有效的版本:

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; 

    }  
} 
+0

谢谢....这是一个快速检查! :d。安德鲁对引起的异常做了一个很好的解释。 – repzero

相关问题