Bean属性' empname'不可读或getter方法无效:getter的返回类型是否与setter的参数类型匹配?

时间:2018-06-18 20:24:01

标签: eclipse spring-mvc spring-boot thymeleaf

我正在尝试从表单执行简单的提交操作。我为我的项目使用带有百里香叶模板的spring boot框架。使用的语言是eclipse IDE中的java。

我要做的就是从表单中获取empname和empid(引用Employee类)并将其存储在java对象中。

当我运行应用程序时,应用程序打开,当我导航到edit.html时,我在浏览器中收到此错误消息 -

  

Whitelabel错误页面       此应用程序没有/ error的显式映射,因此您将此视为回退。       星期一6月18日16:14:40 EDT 2018       出现意外错误(type = Internal Server Error,status = 500)。       模板解析期间发生错误(模板:"类路径资源[templates / edit.html]")

我也在控制台上收到此错误消息 -

  

引起:org.springframework.beans.NotReadablePropertyException:无效的属性' empname' bean类[com.cardinalcommerce.model.Employee]:Bean属性' empname'不可读或getter方法无效:getter的返回类型是否与setter的参数类型匹配?           在org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)〜[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]           在org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612)〜[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]           在org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104)〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]           在org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228)〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]           在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:129)〜[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]           在org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)〜[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]           at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)~ [thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]           at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]           在org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:252)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]           at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:226)~ [thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]           在org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]           at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]           ...省略了67个常见帧

这是我发生错误的html文档片段。

<form class="form-horizontal" action="#" th:action="@{/employee/edit}" th:object="${employee}" method="POST">

    <div class="form-group">
      <label class="control-label col-sm-3">File Prefix:</label>
      <div class="col-sm-7">
        <input type="text" class="form-control" th:field="*{empname}"  placeholder="Enter employee name" />
      </div>
    </div>

    <div class="form-group">
      <label class="control-label col-sm-3">File Prefix:</label>
      <div class="col-sm-7">
        <input type="text" class="form-control" th:field="*{empid}"  placeholder="Enter the employee ID" />
      </div>
    </div>

    <div class="form-group">        
      <div class="col-sm-offset-3 col-sm-7">
        <button type="submit" class="btn btn-default" id="blackButton" th:value="Submit">Submit</button>
        <button type="reset" class="btn btn-default" id="blackButton" th:value="Reset">Cancel</button>
      </div>
    </div>  

这是我的课程,在那里有制定者和吸气者 -

public class Employee {
    private String empid;
    private String empname;

    public String getEmployeeId() {
        return empid;
    }
    public void setEmployeeId(String empid) {
        this.empid = empid ;
    }
    public String getEmployeeName() {
        return empname;
    }
    public void setEmployeeName(String empname) {
        this.empname = empname;
    }
}

这是控制器代码段 -

@Controller
    @RequestMapping(value="/")
    public class GreetingController {

    private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);

    @Autowired
    private SomeRecord someRecord;

    @GetMapping("/")
    public String greeting() {

        return "about";
    }

    @RequestMapping("/about")
    public String about() {

        return "about";
    }

    @GetMapping("/edit")
    public ModelAndView edit() {
        ModelAndView modelAndView = new ModelAndView("edit");
        modelAndView.addObject("employee", new Employee());

        return modelAndView;
    }

    @PostMapping("/edit")
    public ModelAndView createRecord(@Valid Employee employee, BindingResult result) {
        ModelAndView modelAndView = new ModelAndView();
        if (result.hasErrors()) {
            logger.info("Validation errors while submitting form.");
            modelAndView.setViewName("CreateRecord");
            modelAndView.addObject("employee", employee);

            return modelAndView;
        }
        someRecord.addRecord(employee);
        modelAndView.addObject("allRecords", someRecord.getAllRecordData());
        modelAndView.setViewName("recordsInfo");
        logger.info("Form submitted successfully.");
        return modelAndView;
    }


    @GetMapping("/view")
    public String view() {

        return "view";
    }

}

如果需要其他任何内容,请与我们联系。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您应该使用*{employeeName}*{employeeId}而不是*{empname}*{empid}。 (匹配getter和setter,而不是你的私有变量。)