I have a form:
<form action="user-fonts" method="post" ">
<select >
<#list fonts as font>
<option value=${font}>${font.nameFont?ifExists}</option>
</#list>
</select>
<input type="hidden" name="_csrf" value="${_csrf.token}" />
<div><input type="submit" value="Sign In"/></div>
</form>
How to get the value in the controller that I chose on the form?
@RequestMapping("/user-fonts")
public class MainController{
@GetMapping
public String main(@AuthenticationPrincipal User user, Model model)
{
Set<DBFont> fonts = user.getFont();
model.addAttribute("fonts", fonts);
return "Myfonts";
}
@PostMapping
public String mainPost(@ModelAttribute DBFont DBfont)
{
System.out.println(DBfont.getNameFont());
return "redirect:/user-fonts";
}
There is a value in the database, but I get null
, How to return the value?
You need to define a name
attribute to your select, e.g. nameFont
:
<select name="nameFont">
This will send font selected value as POST parameter nameFont