当前位置: 首页 > 知识库问答 >
问题:

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

施默
2023-03-14

我试图从一个表单执行一个简单的提交操作。我的项目使用带有百里香叶模板的Spring引导框架。使用的语言是eclipse IDE中的java。

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

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

Whitelabel错误页此应用程序没有/Error的显式映射,因此您将其视为回退。2018年6月18日星期一16:14:40美国东部时间出现意外错误(类型=内部服务器错误,状态=500)。模板分析期间出错(模板:“class path resource[templates/edit.html]”)

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

由以下原因引起:org.springframework.beans.NotReadableProperty异常:Bean 类 [com.cardinalcommerce.model.Employee] 的属性 'empname' 无效:Bean 属性 'empname' 不可读或具有无效的 getter 方法: getter 的返回类型是否与 setter 的参数类型匹配?at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:129) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at 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] atorg.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at 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] atorg.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>  

这是我的类,其中有setter和getter-

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个答案

陈琪
2023-03-14

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

 类似资料: