如何从 JSP 中访问 Struts DynaValidatorForm 属性
我有一个填充字段的 struts 操作。
public ActionForward appForm(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaValidatorForm dyna = (DynaValidatorForm) form;
String identifier = (String) request.getParameter("id");
ApplicationDTO app = manager.getApplication(identifier);
dyna.set("firstName",app.getFirstName());
return (mapping.findForward("application.success"));
}
表单 bean 如下所示:
<form-bean name="ApplicationForm" type="struts.ApplicationForm">
<form-property name="id" type="java.lang.String" initial="0" />
<form-property name="firstName" type="java.lang.String" />
...
..
.
</form-bean>
ApplicationForm 扩展了 DynaValidatorForm 并且它执行一些验证。
如何访问 jsp 中的 struts 表单属性值?
<html:text property="firstName" styleId="firstName" value="TODO Add code to access the form property fistName END TODO"/>
解决方案
使用 html:text 标记中的 name 属性。 Name 属性指定对象的名称(在任何范围内),该对象的由 property 属性指定的字段将用于用数据填充此控件。例子:
<html:text property="firstName" styleId="firstName" name="ApplicationForm"/>
I have a struts action which populates a field.
public ActionForward appForm(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaValidatorForm dyna = (DynaValidatorForm) form;
String identifier = (String) request.getParameter("id");
ApplicationDTO app = manager.getApplication(identifier);
dyna.set("firstName",app.getFirstName());
return (mapping.findForward("application.success"));
}
The form bean looks like:
<form-bean name="ApplicationForm" type="struts.ApplicationForm">
<form-property name="id" type="java.lang.String" initial="0" />
<form-property name="firstName" type="java.lang.String" />
...
..
.
</form-bean>
ApplicationForm extends DynaValidatorForm and it does some validation.
How do I access a struts form property value inside my jsp.
<html:text property="firstName" styleId="firstName" value="TODO Add code to access the form property fistName END TODO"/>
SOLUTION
user the name attribute within the html:text tag. Name attribute specifies the name of an object (in any scope) whose field, specified by the property attribute will be used to populate this control with data. Example:
<html:text property="firstName" styleId="firstName" name="ApplicationForm"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要纯文本输出,这里是访问 Dynamic ActionForm Beans 中的属性的另一种方法:
Here is another way to access properties in Dynamic ActionForm Beans if you want a plain text output: