我的问题与此问题非常相似:Spring MVC Multiple Controllers with same @RequestMapping
我正在使用Spring Boot构建简单的人力资源Web应用程序。我有一份工作清单和每个工作的个人网址:
本地主机:8080 /作业/ 1
此页面包含作业发布详细信息和未经身份验证的用户 - 申请人(在这种情况下)可用于应用此作业的表单。经过身份验证的用户-HR Manager-只能查看发布详细信息,而不能查看表单。我在验证表单输入方面遇到了麻烦。
我先尝试了什么:
@Controller
public class ApplicationController {
private final AppService appService;
@Autowired
public ApplicationController(AppService appService) {
this.appService = appService;
}
@RequestMapping(value = "/jobs/{id}", method = RequestMethod.POST)
public String handleApplyForm(@PathVariable Long id, @Valid @ModelAttribute("form") ApplyForm form, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "job_detail"; //HTML page which contains job details and the application form
}
appService.apply(form, id);
return "redirect:/jobs";
}
@RequestMapping(value = "/applications/{id}", method = RequestMethod.GET)
public ModelAndView getApplicationPage(@PathVariable Long id) {
if (null == appService.getAppById(id)) {
throw new NoSuchElementException(String.format("Application=%s not found", id));
} else {
return new ModelAndView("application_detail", "app", appService.getAppById(id));
}
}
}
你猜这不起作用,因为我无法得到模型。所以我将handleApplyForm()
添加到JobController
并进行了一些更改:
@Controller
public class JobController {
private final JobService jobService;
private final AppService appService;
@Autowired
public JobController(JobService jobService, AppService appService) {
this.jobService = jobService;
this.appService = appService;
}
@RequestMapping(value = "/jobs/{id}", method = RequestMethod.POST)
public ModelAndView handleApplyForm(@PathVariable Long id, @Valid @ModelAttribute("form") ApplyForm form, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return getJobPage(id);
}
appService.apply(form, id);
return new ModelAndView("redirect:/jobs");
}
@RequestMapping(value = "/jobs/{id}", method = RequestMethod.GET)
public ModelAndView getJobPage(@PathVariable Long id) {
Map<String, Object> model = new HashMap<String, Object>();
if (null == jobService.getJobById(id)) {
throw new NoSuchElementException(String.format("Job=%s not found", id));
} else {
model.put("job", jobService.getJobById(id));
model.put("form", new ApplyForm());
}
return new ModelAndView("job_detail", model);
}
}
通过这种方式,验证可以正常工作,但我仍然无法获得与刷新页面相同的效果here,以便所有有效输入都消失,并且不会显示错误消息。
顺便说一句,job_detail.html
是这样的:
<h1>Job Details</h1>
<p th:inline="text"><strong>Title:</strong> [[${job.title}]]</p>
<p th:inline="text"><strong>Description:</strong> [[${job.description}]]</p>
<p th:inline="text"><strong>Number of people to hire:</strong> [[${job.numPeopleToHire}]]</p>
<p th:inline="text"><strong>Last application date:</strong> [[${job.lastDate}]]</p>
<div sec:authorize="isAuthenticated()">
<form th:action="@{/jobs/} + ${job.id}" method="post">
<input type="submit" value="Delete this posting" name="delete" />
</form>
</div>
<div sec:authorize="isAnonymous()">
<h1>Application Form</h1>
<form action="#" th:action="@{/jobs/} + ${job.id}" method="post">
<div>
<label>First name</label>
<input type="text" name="firstName" th:value="${form.firstName}" />
<td th:if="${#fields.hasErrors('form.firstName')}" th:errors="${form.firstName}"></td>
</div>
<!-- and other input fields -->
<input type="submit" value="Submit" name="apply" /> <input type="reset" value="Reset" />
</form>
</div>
答案 0 :(得分:0)
检查百里叶文件here
th:field属性的值必须是选择表达式(* {...}),
此外,ApplyForm
已曝光,您可以在表单中捕获它。
然后您的表单应如下所示:
<form action="#" th:action="@{/jobs/} + ${job.id}" th:object="${applyForm}" method="post">
<div>
<label>First name</label>
<input type="text" name="firstName" th:value="*{firstName}" />
<td th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"></td>
</div>
<!-- and other input fields -->
<input type="submit" value="Submit" name="apply" /> <input type="reset" value="Reset" />
</form>