I have a basic form submission spring boot application. My problem is when I am submitting the form I am getting null results.
Here is my form code :
<form method="post" action="#" th:action="@{/save}" th:object="${info}" commandName ="info">
<div class="form-group row">
<label for="firstname">First Name</label>
<input type="text" th:field="*{_FirstName}" class="form-control" placeholder="Enter First Name">
</div>
<div class="form-group row">
<label for="lastname">Last Name</label>
<input type="text" th:field="*{_LastName}" class="form-control" placeholder="Enter Last Name">
</div>
<div class="form-group">
<label>Select Country</label><br>
<select class="form-control" th:field="*{_Country}">
<option value="United State">United State</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Hungary">Hungary</option>
<option value="Malaysia">Malaysia</option>
<option value="India">India</option>
</select>
</div>
<div class="form-group row">
<label for="date">Date</label>
<input th:field="*{_Date}" class="form-control" type="date" value="2011-08-19" />
</div>
<div class="pull-right">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
In my controller I have url /form for get method and /save for post method after getting result I am trying to log the inputs. I am not sure am I populating a model through those inputs here ? if so why inputs are showing null. My Controller Code :
@RequestMapping(value = "/form", method = RequestMethod.GET)
public String index(Info info) {
//model.addAttribute("info", new Info());
return "form";
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@Valid Info info, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "form";
}
model.addAttribute("_FirstName", info.get_FirstName());
model.addAttribute("_LastName", info.get_LastName());
model.addAttribute("_Country", info.get_Country());
model.addAttribute("_Date", info.get_Date());
String submitted = String.format("User Submission: firstname = %s, lastname = %s, country = %s, date = %s",
info.get_FirstName(), info.get_LastName(), info.get_Country(), info.get_Date());
log.info(submitted);
return "cv";
}
And My Model Code :
public class Info {
private String _FirstName;
private String _LastName;
private String _Country;
private Date _Date;
public Info() {
}
public Info(String _FirstName, String _LastName, String _Country, Date _Date) {
super();
this._FirstName = _FirstName;
this._LastName = _LastName;
this._Country = _Country;
this._Date = _Date;
}
public String get_FirstName() {
return _FirstName;
}
public void set_FirstName(String _FirstName) {
this._FirstName = _FirstName;
}
public String get_LastName() {
return _LastName;
}
public void set_LastName(String _LastName) {
this._LastName = _LastName;
}
public String get_Country() {
return _Country;
}
public void set_Country(String _Country) {
this._Country = _Country;
}
public Date get_Date() {
return _Date;
}
public void set_Date(Date _Date) {
this._Date = _Date;
}
}
Can anyone please check what I am missing ? I am getting in console :
User Submission: firstname = null, lastname = null, country = null, date = null
Thanks in Advance !
Aucun commentaire:
Enregistrer un commentaire