我试图让Spring启动将我的表单数据转换为Object表示形式(ExampleInputForm
),但由于某种原因它似乎没有绑定(emailName
总是null
)。我在这里错过了什么吗?
public class ExampleInputForm {
@NotNull
public String emailName;
public ExampleInputForm() {
super();
}
@Override
public String toString() {
return "ExampleInputForm{" +
"emailName='" + emailName + '\'' +
'}';
}
}
@Controller
class MyController {
@RequestMapping(method = RequestMethod.POST, value="/save")
@ResponseBody
public Map<String, String> saveBrands(@Valid ExampleInputForm form, BindingResult bindingResult) {
LOG.info("Saving brands: " + form);
return ImmutableMap.of(
"emailName", form.emailName,
);
}
}
<div class="container">
<div class="row text-center">
<form name="input" action="/save" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="emailName">
</div>
<input type="submit" value="Save" class="btn btn-default" />
</form>
</div>
</div>
答案 0 :(得分:3)
您需要将 emailName 属性的 getter / setter 添加到ExampleInputForm类。