Reputation: 669
I'm a bit at a loss here.
I have a thymeleaf
page and a spring-boot
backend that takes in a user
object, getting the object to the page is fine, my problem comes in when I'm trying to get it to the back end to do stuff with it.
I keep getting the following
2021-09-15 09:21:07.834 WARN 3624 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
and on the browser
Failed to load resource: the server responded with a status of 405 ()
for my controller I have the following
@Controller("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private ModelMapper modelMapper;
@RequestMapping(value = "/add")
public String addUser(@ModelAttribute("user") final UserDto userDto) {
//do stuff
//userService.save(modelMapper.map(userDto, User.class));
return "/user";
}
}
as for my thymeleaf
page
<form th:action="@{/user/add}" th:object="${user}" method="post">
<label for="fullName">Full Name</label>
<input id="fullName" class="form-control form-group" type="text" th:field="*{fullName}">
<label for="email">Email</label>
<input id="email" class="form-control form-group" type="email" th:field="*{email}">
<label for="password">Password</label>
<input id="password" class="form-control form-group" type="password" th:field="*{password}">
<p>
<button class="form-group form-control btn btn-primary" type="submit" value="Submit">Submit</button>
</p>
</form>
What am I missing here?
I did try to mess around the @GetMapping
, @PostMapping
, @RequestMapping(method = GET)
, @RequestMapping(method = POST)
, @RequestMapping(method = {GET, POST})
I also tried <form ... th:method="post">
and <form ... th:method="get">
But none of these seems to work.
Upvotes: 2
Views: 325
Reputation: 2396
You add global /user
in @Controller
. this annotation is used to implement Web Application not for path and it is better to give global path in application.properties
like below code. Inside addUser()
method you want to return with page name like return "user"
if go to the url then put return "redirect:/user"
Here down is modified code:
application.properties
server.servlet.contextPath=/user/
Controller
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private ModelMapper modelMapper;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user") final UserDto userDto) {
//do stuff
//userService.save(modelMapper.map(userDto, User.class));
return "pagename"; // enter page name where you wanna go not url
}
}
Template
<form th:action="@{/add}" th:object="${user}" method="post">
<label for="fullName">Full Name</label>
<input id="fullName" class="form-control form-group" type="text" th:field="*{fullName}">
<label for="email">Email</label>
<input id="email" class="form-control form-group" type="email" th:field="*{email}">
<label for="password">Password</label>
<input id="password" class="form-control form-group" type="password" th:field="*{password}">
<p>
<button class="form-group form-control btn btn-primary" type="submit" value="Submit">Submit</button>
</p>
</form>
Upvotes: 1