DynaActionForm学习
时间:2010-03-31 来源:shihao138096
今天在学习DynaActionForm的过程中,遇到了一个问题:就是进行ActionForm的reset操作,以及validate操作。看书上说,如果要做reset操作,需要使定义一个DynaActionForm的扩展类,然后在其中重载ActionForm的reset()方法,而validate()验证操作,最好时通过Validate框架实现。但是,这里考虑,如果按照以前的ActionForm的写法,如何去做呢?
开始的时候,定义了一个类似与ActionForm的类,并且让它extends DynaActionForm类,然后,在其中定义了JSP form页面中的form元素变量(和相关的get/set方法),然后,重载了reset()和validate()方法,但是,在配置完以后,运行却出现了无法找到变量的get/set方法。后来看到书上说,DynaActionForm使用get(String name)和set(String name, Object value),来获取和设置form变量。但是考虑,如果是这样的话,这里应该怎么写呢,然后,又去网上查资料,后来在一篇英文文献上看到了相关的说明:应该在struts配置文件中的<form-beans>的子标签<form-bean>内定义form变量,然后,新建一个扩展自DynaActionForm的Form Bean,在其中只做reset()和validate()的重载(注,验证用户输入,可以通过get()方法获得),最后在struts中将刚才的那个<form-bean>的type属性设置了新建的那个Form Bean即可,然后运行,就没有错误了。以下给出示例:
index.jsp页面主体:
<center>
<html:form action="/login">
<table width="35%" height="30%" border="0">
<tr>
<td>
<bean:message key="index.jsp.frm.txt.username" />
</td>
<td>
<html:text property="username" />
</td>
</tr>
<tr>
<td>
<bean:message key="index.jsp.frm.txt.password" />
</td>
<td>
<html:password property="password" />
</td>
</tr>
<tr>
<td>
<html:submit>
<bean:message key="index.jsp.frm.btn.submit" />
</html:submit>
</td>
<td>
<html:reset>
<bean:message key="index.jsp.frm.btn.reset" />
</html:reset>
</td>
</tr>
</table>
</html:form>
<html:errors/>
</center>
</body> ActionForm bean代码: public class LoginForm extends DynaActionForm {
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.initialize(mapping);
} public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors(); if (this.get("username") == null || get("username").toString().length() < 1) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"loginform.java.validate.error.username"));
}
if (get("password") == null || get("password").toString().length() < 1) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"loginform.java.validate.error.username"));
}
return errors;
}
} Struts配置文件(局部): <form-bean name="loginForm" dynamic="true"
type="com.dynaactionform.struts.form.LoginForm">
<form-property name="password" type="java.lang.String" />
<form-property name="username" type="java.lang.String" />
</form-bean> <global-forwards>
<forward name="index" path="/index.jsp" />
</global-forwards>
<action-mappings>
<action attribute="loginForm" input="/index.jsp" name="loginForm"
path="/login" scope="request" type="com.dynaactionform.struts.action.LoginAction">
<forward name="display" path="/pages/display.jsp" />
</action>
</action-mappings> Action对象: public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String username = loginForm.get("username").toString();
String password = loginForm.get("password").toString();
request.removeAttribute(mapping.getAttribute());
request.setAttribute("user", username);
request.setAttribute("pwd", password); return mapping.findForward("display");
}
} display页面用于显示用户输入的用户名和密码,比价simple,这里就不贴了。 资源文件,可以自己建一个,这里也不贴了。 然后运行,页面就可以正常显示,并达到了表单验证和重置效果。 刚看DynaActionForm,因此理解不是很深刻,还望各位高手指教!
<html:form action="/login">
<table width="35%" height="30%" border="0">
<tr>
<td>
<bean:message key="index.jsp.frm.txt.username" />
</td>
<td>
<html:text property="username" />
</td>
</tr>
<tr>
<td>
<bean:message key="index.jsp.frm.txt.password" />
</td>
<td>
<html:password property="password" />
</td>
</tr>
<tr>
<td>
<html:submit>
<bean:message key="index.jsp.frm.btn.submit" />
</html:submit>
</td>
<td>
<html:reset>
<bean:message key="index.jsp.frm.btn.reset" />
</html:reset>
</td>
</tr>
</table>
</html:form>
<html:errors/>
</center>
</body> ActionForm bean代码: public class LoginForm extends DynaActionForm {
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.initialize(mapping);
} public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors(); if (this.get("username") == null || get("username").toString().length() < 1) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"loginform.java.validate.error.username"));
}
if (get("password") == null || get("password").toString().length() < 1) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"loginform.java.validate.error.username"));
}
return errors;
}
} Struts配置文件(局部): <form-bean name="loginForm" dynamic="true"
type="com.dynaactionform.struts.form.LoginForm">
<form-property name="password" type="java.lang.String" />
<form-property name="username" type="java.lang.String" />
</form-bean> <global-forwards>
<forward name="index" path="/index.jsp" />
</global-forwards>
<action-mappings>
<action attribute="loginForm" input="/index.jsp" name="loginForm"
path="/login" scope="request" type="com.dynaactionform.struts.action.LoginAction">
<forward name="display" path="/pages/display.jsp" />
</action>
</action-mappings> Action对象: public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String username = loginForm.get("username").toString();
String password = loginForm.get("password").toString();
request.removeAttribute(mapping.getAttribute());
request.setAttribute("user", username);
request.setAttribute("pwd", password); return mapping.findForward("display");
}
} display页面用于显示用户输入的用户名和密码,比价simple,这里就不贴了。 资源文件,可以自己建一个,这里也不贴了。 然后运行,页面就可以正常显示,并达到了表单验证和重置效果。 刚看DynaActionForm,因此理解不是很深刻,还望各位高手指教!
相关阅读 更多 +