Spring Boot:从表单提交中获取空输入

时间:2018-03-20 15:14:07

标签: java spring spring-mvc spring-boot web

我有一个基本的表单提交spring boot应用程序。 我的问题是当我提交表单时,我得到的结果为空。

这是我的表单代码:

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

在我的控制器中,我有url / form for get方法和/ save for post方法 获得结果后,我试图记录输入。 我不确定我是否通过这些输入来填充模型?如果是这样,为什么输入显示为空。 我的控制器代码:

@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";
}

我的模型代码:

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


}

任何人都可以查看我遗失的内容吗? 我进入控制台:

User Submission: firstname = null, lastname = null, country = null, date = null

提前致谢!

2 个答案:

答案 0 :(得分:0)

将您的GET请求方法更新为:

@RequestMapping(value = "/form", method = RequestMethod.GET)
public String index(Info info, Model model) {
    model.addAttribute("info", new Info());
    return "form";
}

您需要向表单提供Info对象才能将其恢复。同样在@ModelAttribute

之前/之后将@Valid放入POST方法中

答案 1 :(得分:0)

这是在我的环境中工作的代码,我将向您展示我所做的更改:

@RequestMapping(value = "/form", method = RequestMethod.GET)
    public String index(Info info, Model model) {
        model.addAttribute("info", info);
        return "myform";
    }

@RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("info") Info info, HttpServletRequest request, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            return "myform";
        }

请注意HttpServletRequest不需要它,我添加它以便我可以看到Spring是否能够通过request.getParameterNames()获取params,这证明了它确实存在,但是你的代码没有问题与绑定......

private String firstName;
    private String _LastName;
    private String _Country;
    private Date _Date;
public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String set_LastName() {
        return _LastName;
    }

有趣的是,唯一一个被调用的setter是setFirstName(),其他名称未更改的setter都没有工作......所以问题在于命名。

html文件

    <div class="form-group row">

        <input type="text" th:field="*{firstName}" class="form-control" placeholder="Enter First Name">