Got stuck for ~4 hours wondering where is the mistake in Spring MVC/thymeleaf app.
My local goal is to render admin.html after submitting login/pass form at home page.
Controller:
@Controller
public class HomeController {
@GetMapping("/")
public String getHome(Model m) {
m.addAttribute( "user",new User());
return "/home";
}
@PostMapping("/")
public String getSubmit(@ModelAttribute User user){
return "/admin";
}
}
home.html:
<form action="#" th:action="@{/admin}" th:object="${user}" method="post">
<p class="txt">Name: <input type="text" th:field="*{name}"/></p>
<p class="txt">Password: <input type="text" th:field="*{password}"/></p>
<p><input class="button" type="submit" value="Submit" />
<input class="button" type="reset" value="Reset" /></p>
</form >
User class:
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
private boolean isAdmin;
private String address;
}
So I have googled loads of ideas, removed spring security from pom.xml,
tried to use @RequestedMapping with RequestMethod.PUT , - no way , it doesn't work.
Your controller should be like this:
@Controller
public class HomeController {
@GetMapping("/")
public String getHome(Model m) {
m.addAttribute("user", new User());
return "/home";
}
@PostMapping("/admin")
public String getSubmit(User user) {
return "/admin";
}
}
home.html shoud be like:
<form action="@{/admin}" th:object="${user}" method="post">
<p class="txt">Name: <input type="text" th:field="*{name}"/></p>
<p class="txt">Password: <input type="text" th:field="*{password}"/></p>
<p><input class="button" type="submit" value="Submit" />
<input class="button" type="reset" value="Reset"/></p>
</form >