如何使用<表单 :输入>对于非表单对象表单>
Spring标签
可以生成带有id和name属性的标签。我认为这个功能很有用,我想在使用非表单对象时使用它。 请看下面的代码。
“dto”对象被添加到“model”对象以及“form”中,然后我想自动生成id属性。但是,
标签似乎能够用于绑定表单对象。我是否必须制作自定义标签才能实现类似的功能?任何帮助将不胜感激?
[Controller]
@RequestMapping(method = RequestMethod.GET)
public String show(Model model, HttpServletRequest request) {
SampleForm form = new SampleForm();
form.setName("Name of Form Object");
SampleDto dto = new SampleDto();
dto.setName("Name of Dto Object");
model.addAttribute("form", form);
model.addAttribute("dto", dto);
return "sample/input";
}
[JSP]
<body>
<form:form modelAttribute="form" method="post">
<%-- Generate with id attribute like <input id="name" name="name" type="text" value="Name of Form Object"/> --%>
<form:input path="name" />
<%-- I tried below but an error occured--%>
<%-- <form:input path="${dto.name}" /> --%>
<%-- Just a String display like "Name of Dto Object" --%>
${dto.name}
<input type="submit" name="register" value="register" />
</form:form>
</body>
[Form]
public class SampleForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[Dto]
public class SampleDto {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Spring tag <form:input>
can generate <input>
tag with id and name attributes. I think this feature is useful and I want to use this when using non form object.
Please take a look at codes below.
"dto" object is added to "model" object as well as "form" then I want to generate id attribute automatically. However, <form:input>
tag seems to be able to use for binding form object. Do I have to make a custom tag in order to realize the similar feature? Any help will be appreciated?
[Controller]
@RequestMapping(method = RequestMethod.GET)
public String show(Model model, HttpServletRequest request) {
SampleForm form = new SampleForm();
form.setName("Name of Form Object");
SampleDto dto = new SampleDto();
dto.setName("Name of Dto Object");
model.addAttribute("form", form);
model.addAttribute("dto", dto);
return "sample/input";
}
[JSP]
<body>
<form:form modelAttribute="form" method="post">
<%-- Generate with id attribute like <input id="name" name="name" type="text" value="Name of Form Object"/> --%>
<form:input path="name" />
<%-- I tried below but an error occured--%>
<%-- <form:input path="${dto.name}" /> --%>
<%-- Just a String display like "Name of Dto Object" --%>
${dto.name}
<input type="submit" name="register" value="register" />
</form:form>
</body>
[Form]
public class SampleForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[Dto]
public class SampleDto {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个表单只能有一个支持对象。在您的示例中,支持对象是 SampleForm 的实例。您可以在 SampleForm 类中添加对 SampleDto 实例的引用:
然后您可以在 JSP 中执行此操作:
A form can only have one backing object. In your example the backing object is an instance of SampleForm. You could add a reference to a SampleDto instance in your SampleForm class:
Then you could do this in your JSP:
如果你想从 dto 生成 id 属性,那么它应该是
If you want to generate id attribute from
dto
then it should be