gpt4 book ai didi

java - Spring 启动: Getting Null inputs from Form Submission

转载 作者:太空宇宙 更新时间:2023-11-04 10:36:41 25 4
gpt4 key购买 nike

我有一个基本的表单提交 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>

在我的 Controller 中,我有用于 get 方法的 url/form 和用于 post 方法的/save得到结果后,我尝试记录输入。我不确定我是否通过此处的这些输入填充模型?如果是这样,为什么输入显示为空。我的 Controller 代码:

@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

提前致谢!

最佳答案

这是在我的环境中运行的代码,我只会向您展示我所做的更改:

@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() 获取参数,这向我证明它确实如此,这是您的代码有问题而不是绑定(bind)...

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

关于java - Spring 启动: Getting Null inputs from Form Submission,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49387911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com