我一直在做一个基本的应用程序,其中可能包含我最近遇到的所有问题。
我尝试将代码中指定的端点映射到“ / example /”,“ / example / add”和“ / example / add2”下,但不幸的是,当我尝试提交表单时得到了404。
如果我不使用子域,则代码可以正常工作,突出显示“ /”下的所有内容。
这是我的相关代码:
//@RequestMapping("/example") --> also tried to put it here
public class AttractionController {
private AttractionService attractionService;
@Autowired
public AttractionController(AttractionService attractionService) {
this.attractionService = attractionService;
}
@RequestMapping("/example")
@GetMapping("/")
String main(Model model) {
model.addAttribute("attractions", attractionService.findAll());
return "main";
}
@PostMapping("/add")
String add(@ModelAttribute Attraction attraction) {
attractionService.save(attraction);
return "redirect:/";
}
@PostMapping("/add2")
String add2(@RequestParam String name, @RequestParam Double latitude,
@RequestParam String city, Model model) {
attractionService.save(new Attraction(name, latitude, city));
return "redirect:/";
//also tried to use return "redirect:/example"
}
HTML:
<form th:method="POST" th:action="@{add}">
<input type="text" name="name">
<input type="text" name="latitude">
<input type="text" name="city">
<input type="submit">
</form>
<form th:method="POST" th:action="@{add2}">
<input type="text" name="name">
<input type="text" name="latitude">
<input type="text" name="city">
<input type="submit">
</form>
我该如何解决这个问题?