未显示字符串列表
我使用 spring 3。我试图在 jsp 中显示对象的值。
public class UserForm implements Serializable {
private List<String> values;
private String date;
...
}
这是我的控制器:
@Controller
@RequestMapping("/user.htm")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public String userStat(Model model) {
UserForm stat = userService.populateStatistique();
stat.setDate("today");
model.addAttribute("statistiqueForm", stat);
return "userStat";
}
}
这是我的 jsp:
<form:form commandName="userForm" name="userForm">
Date: <form:input path="date"></form:input>
<br/>
Expediteur:
<form:select path="values" items="${values}" />
<br/>
<input type="submit" value="create" />
</form:form>
在 jsp 中,我可以看到日期字段的今天值,但列表框是空的。
有什么想法吗?
谢谢
I use spring 3. I'm trying to display a value of an object in a jsp.
public class UserForm implements Serializable {
private List<String> values;
private String date;
...
}
Here is my controller:
@Controller
@RequestMapping("/user.htm")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public String userStat(Model model) {
UserForm stat = userService.populateStatistique();
stat.setDate("today");
model.addAttribute("statistiqueForm", stat);
return "userStat";
}
}
Here is my jsp:
<form:form commandName="userForm" name="userForm">
Date: <form:input path="date"></form:input>
<br/>
Expediteur:
<form:select path="values" items="${values}" />
<br/>
<input type="submit" value="create" />
</form:form>
In the jsp I can see the today value for the date field, but the listbox is empty.
any idea?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,如果您希望在 jsp 中可以访问它,请使用
addAttribute("values", list)
。您当前尚未设置它,因此它是空的。如果该列表包含在
statistiqueForm
对象中,则使用items="${statistiqueForm.values}"
。Well, use
addAttribute("values", list)
if you want it accessible in the jsp. You are currently not setting it and so it is empty.If that list is contained in the
statistiqueForm
object, then useitems="${statistiqueForm.values}"
.您正确地将表单对象传递到 jsp 页面。在该页面中,您有一个列表框,其中包含潜在值列表和选定值列表。对象表单包含所选值的列表,但您还应该设置包含所有潜在值的列表。事实上,您在 jsp 中引用了 ${values},但没有将其传递给 jsp。您应该将此代码添加到您的控制器中:
我还建议您更改该列表的名称以避免混淆,这样就很容易理解选定值与潜在值之间的差异。
You're correctly passing the form object to the jsp page. In that page you have a list box, which has a list of potential values and a list of selected values. The object form contains the list of selected values, but you should also setup the list with all potential values. In fact you reference ${values} in the jsp, but you don't pass it to the jsp. You should add this code to your controller:
I also suggest you to change the name of that list to avoid confusion, so it will be easy to understand the difference from selected values to potential values.