我在控制器中创建了两个模型属性。当我想访问不起作用的模型属性之一时。没有错误,但我没有得到想要的结果
这是我的控制器方法:
@RequestMapping(value = {"/jobList/applyJob/{id}"}, method =
RequestMethod.GET)
public String applyJob(@PathVariable("id") Integer id,Model model)
{
Applicant applicant=new Applicant();
model.addAttribute("applicantRegister",applicant);
model.addAttribute("selectedJob",jobService.findById(id));
return "apply";
}
这是我的apply.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-
springsecurity3">
<head>
<title>Hello World!</title>
</head>
<p th:value="${selectedJob.jobTitle}">
<form method="post" th:action="@{/jobList/applicantRegister}">
Name:<br>
<input type="text" th:field="${applicantRegister.name}"><br>
Email:<br>
<input type="text" th:field="${applicantRegister.email}"><br>
Phone:<br>
<input type="text" th:field="${applicantRegister.phone}"><br>
Address:<br>
<input type="text" th:field="${applicantRegister.address}"><br>
Thoughts On Job:<br>
<input type="text" th:field="${applicantRegister.thoughtsOnJob}">
<input type="hidden" th:field="${applicantRegister.fkJobId}"
th:value="${selectedJob.jobId}"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
当我想访问apply.html文件中的selectedJob属性时,该属性返回null。但是当我调试时,行:
model.addAttribute("selectedJob",jobService.findById(id));
工作完美。
我在哪里弄错了?
答案 0 :(得分:0)
<p>
HTML标记没有value
属性。您必须使用text
:
<p th:text="${selectedJob.jobTitle}">