kha*_*ali 1 eclipse spring-mvc thymeleaf spring-boot
我正在尝试从表单执行简单的提交操作。我在我的项目中使用带有百里香叶模板的 Spring Boot 框架。eclipse IDE中使用的语言是java。
我想做的就是从表单中获取 empname 和 empid (参考 Employee 类)并将其存储在 java 对象中。
当我运行应用程序时,应用程序将打开,当我导航到 edit.html 时,我在浏览器中收到此错误消息 -
Whitelabel 错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。2018 年 6 月 18 日星期一 16:14:40 EDT 出现意外错误(类型=内部服务器错误,状态=500)。模板解析时发生错误(模板:“类路径资源[templates/edit.html]”)
我还在控制台上收到此错误消息 -
引起:org.springframework.beans.NotReadablePropertyException:bean 类 [com.cardinalcommerce.model.Employee] 的无效属性“empname”: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] 在 org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) 〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 位于 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] 在 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] 在 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>
Run Code Online (Sandbox Code Playgroud)
这是我的课程,其中有 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器片段 -
@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";
}
}
Run Code Online (Sandbox Code Playgroud)
如果还需要什么,请告诉我。感谢您的帮助。
您应该使用*{employeeName}and*{employeeId}而不是*{empname}and *{empid}。(匹配 getter 和 setter,而不是你的私有变量。)
| 归档时间: |
|
| 查看次数: |
11459 次 |
| 最近记录: |