Spring Boot表单输入为空

时间:2016-08-16 19:11:04

标签: java spring spring-boot freemarker

我试图让Spring启动将我的表单数据转换为Object表示形式(ExampleInputForm),但由于某种原因它似乎没有绑定(emailName总是null)。我在这里错过了什么吗?

ExampleInputForm.java

public class ExampleInputForm {
    @NotNull
    public String emailName;


    public ExampleInputForm() {
        super();
    }

    @Override
    public String toString() {
        return "ExampleInputForm{" +
                "emailName='" + emailName + '\'' +
                '}';
    }
}

MyController.java

@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,
        );
    }
}

form.ftl

<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>

1 个答案:

答案 0 :(得分:3)

您需要将 emailName 属性的 getter / setter 添加到ExampleInputForm类。