WebWork服务器端Validation的实现.
使用WebWork做web系统的demo已经基本完成了,经过几天的学习对webwork已经有一个基本的认识了. 今天做了一个登陆界面,试了一试服务器端的validation.
遇到了几个问题,记一下开发过程:
webwork安装问题: TutorialLesson02中写到: validators.xml: Again, at the root of the classpath, create a file called validators.xml, with the following content: ..... This file defines the validators that are available for use.
因为安装时没用到validation所以没太注意,随便以为就放到WEB-INF目录了,结果今天遇到问题了。说没找到校验类实现,查了半天,hehe.后来把validators.xml放到 /WEB-INF/classes就可以了。 回头看看文档,其实上面说胡 root of the classpath 是指 /WEB-INF/classes.以后一定要注意看文档了,其实遇到问题时还是应静下心来想问题原因,可能是什么问题引起的,而不是胡乱试,结果浪费时间(其实每次解决完问题后我都会这么想,可是到下次遇到问题是我就又是胡乱试了,呵呵)
言归正传,开发步骤如下:
流程: login.jsp中输入用户名和密码提交给Login.action处理,login.action对应配置LoginAction.class实现LoginAction-validation.xml配置验证内容和提示信息,如果验证失败返回login.jsp并显示提示信息。
1.写login.jsp页面。 2.写LoninAction.java 3.在LoginAction.java同一个包下写LoginAction-validation.xml。注意: LoginAction-validation.xml 和LoginAction.java要放在同一目录。-validation.xml文件要以前的名字要为类的名字并注意大小写。还有噢,如果错误提示信息为中文时要设置xml的encoding为gb2312(GBK应该也可以没试过。)(又另:为了对观众负责又特意把LoginAction-validation.xml前的LoginAction改为别的试了一下,确定要与文件名要用类名开头否则不执行验证。) 4.配置xwork.xml.注意配置中要加<interceptor-ref name="validationWorkflowStack" />以启用验证,验证不通过时返回的结果为“input",所以为了验证错误时返回输入页要配置名字为"input"的result
顺便说一下,验证失败是要显示错误信息应该使用:fieldErrors 详见login.jsp
以下为实例代码: webwork2.1.7 tomcat5.5.4下测试通过:
login.jsp:
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib uri="webwork" prefix="ww" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>综合结算系统查询平台登陆界面</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <ww:if test="hasErrors()"> ERROR:<br /> <font color="red"> <ww:iterator value="fieldErrors"> <ww:property/><br /> </ww:iterator> </font> </ww:if> <form name="form1" method="post" action="login.action"> <p> </p> <table width="75%" border="1" align="center"> <tr> <td colspan="2"><div align="center">登录</div></td> </tr> <tr> <td width="50%"><div align="right">用户名:</div></td> <td width="50%"><input name="UserName" type="text" value="<ww:property value="UserName" />"></td> </tr> <tr> <td><div align="right">密码:</div></td> <td><input name="Password" type="text" id="Password4"></td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"> </div></td> </tr> </table> <p> </p> <p> </p> </form> </body> </html>
LoginAction.java 就是验证通过后执行功能的类了,比如查数据库比较密码等。没具体实现。 import com.opensymphony.xwork.ActionSupport;
/** * @author leo *登陆系统检查 */ public class LoginAction extends ActionSupport { private String userName; private String password; public String execute() throws Exception{ return ERROR; } //get set 方法略 }
LoginAction-validation.xml(再说一次要与LoginAction.xml同一目录噢。。。。)
<?xml version="1.0" encoding="GB2312"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <field name="UserName"> <field-validator type="requiredstring"> <message>你必须输入一个用户名.</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">4</param> <message>用户名长度必须在 ${minLength} 和 ${maxLength} 之间.</message> </field-validator> </field> <field name="Password"> <field-validator type="requiredstring"> <message>你必须输入一个密码.</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">8</param> <message>密码长度必须在 ${minLength} 和 ${maxLength} 之间.</message> </field-validator> </field> </validators>
xwork.xml配置: <action name="login" class="com.longshine.action.LoginAction"> <result name="success" type="dispatcher">index.htm</result> <result name="input" type="dispatcher">login.jsp</result> <interceptor-ref name="validationWorkflowStack" /> </action>
注意 要配置name="input" 噢
明天再试试客户端的javascript验证,不过听说webwork这方面不如struts. 现在还不知道怎么做呢,明天看看文档再说,19:31累饿了,吃饭先。。 噢,不明天不行,明天要到厦门开年会。。。
webwork的文档的确没有struts多,不过我还是选择了webwork因为我觉得它比struts清析,比较符合我的想法,struts如何呢,我只写过hello world不敢乱说,不过感觉来说配置比较多,流程比较复杂,对,只是感觉....
------------------------------------------------------ 回头看看写得实在是太乱了。。。。。晕。。 再一次的提醒自己下一次写东西要先列提纲。。。。
再次提醒。。。

|