I made a Form using Thymeleaf but running into this issue.
I read many articles but didn't find any solution.
Any solution you can suggest ?
Project Controller ->
@Controller
public class Controllers {
@GetMapping("/home")
public ModelAndView home(){
System.out.println("User is in Homepage");
return new ModelAndView("index");
}
@GetMapping("/service")
public ModelAndView service(){
System.out.println("User is in Service Page");
return new ModelAndView("service");
}
@GetMapping("/about")
public ModelAndView about(){
System.out.println("User is in About page");
return new ModelAndView("about");
}
Here's Controller Class for submitting form ->
@Controller
public class SavingUser{
@Autowired
private UserRepository userRepository;
@PostMapping("/registerUser")
public ModelAndView user(@ModelAttribute Customer customer, ModelMap model){
System.out.println("User in registration page..");
userRepository.save(customer);
model.addAttribute("saveUser", customer);
return new ModelAndView("index");
}
}
And here's my HTML Form -
<div id="form">
<form action="registerUser" th:action="@{/registerUser}" th:object="${saveUser}" method="POST">
<br />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<label for="name">Your Name:</label><br />
<input type="text" th:field="*{name}" placeholder="" /><br />
<label for="suburb">Your Suburb</label><br />
<input type="text" th:field="*{suburb}" placeholder="" /><br />
<input class="submit" type="submit" value="Submit" />
<br /><br />
</div>
</form>
</div>
I tried to remove action="", still it didn't work.