gpt4 book ai didi

Spring Boot 多个 Controller 具有相同的映射

转载 作者:行者123 更新时间:2023-12-02 07:04:57 28 4
gpt4 key购买 nike

我的问题与此非常相似:Spring MVC Multiple Controllers with same @RequestMapping

我正在使用 Spring Boot 构建简单的人力资源 Web 应用程序。我有一个职位列表和每个职位的单独网址:

localhost:8080/jobs/1

此页面包含职位发布详细信息和未经身份验证的用户(在本例中为申请人)可以用来申请此职位的表单。经过身份验证的用户 - 人力资源经理 - 只能看到发布详细信息,而看不到表单。我在验证表单输入时遇到问题。

我首先尝试的:

@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>

最佳答案

检查 thymeleaf 文档 here

Values for th:field attributes must be selection expressions (*{...}),

此外,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>

关于Spring Boot 多个 Controller 具有相同的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175166/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com