首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Bean属性“empname”不可读或具有无效的getter方法: getter的返回类型是否与setter的参数类型匹配?

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

Stack Overflow用户
提问于 2018-06-19 04:24:01
回答 1查看 7.9K关注 0票数 1

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

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

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

白标错误页此应用程序没有显式的/error映射,因此您将其视为一种备用方法。Mon Jun 18 16:14:40美国东部时间2018年出现意外错误(type=Internal服务器错误,status=500)。解析模板时出错(模板:"class path resource templates/edit.html")

我在控制台上也得到了这个错误消息-

由: org.springframework.beans.NotReadablePropertyException: bean类的无效属性'empname‘引起的:Bean属性'empname’不可读或具有无效的getter方法: getter的返回类型是否与com.cardinalcommerce.model.Employee的参数类型匹配?在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.0org.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) ~ .6.RELEASE的春天-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 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 at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) ~thymeleaf Sprring5-3.0.9版本.jar:3.0.9释放org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~胸腺叶-3.0.9释放.jar:3.0.9释放... 67个公共帧被省略

这是我的html文档片段,其中出现了错误。

代码语言:javascript
复制
<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和getters

代码语言:javascript
复制
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;
    }
}

这是控制器代码片段-

代码语言:javascript
复制
@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";
    }

}

如果还需要什么,请告诉我。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-19 10:51:06

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50916965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档