Struts校验框架.docx
- 文档编号:5885009
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:8
- 大小:17.84KB
Struts校验框架.docx
《Struts校验框架.docx》由会员分享,可在线阅读,更多相关《Struts校验框架.docx(8页珍藏版)》请在冰豆网上搜索。
Struts校验框架
SSH开发---之Struts校验框架
Struts校验框架,大体有如下几部分组成
--[if!
supportLists]-->1.
--[endif]-->校验提示信息
--[if!
supportLists]-->2.
--[endif]-->校验提交页面
--[if!
supportLists]-->3.
--[endif]-->校验Form
--[if!
supportLists]-->4.
--[endif]-->Action
--[if!
supportLists]-->5.
--[endif]-->校验配置文件文件
下面以实现一个用户登陆的例子讲解Struts校验框架的组成部分以及相关的写法.
要求用户登陆的时候,输入用户名和密码,再进行登陆.用户名和密码都不能为空,而且密码的长度必须大于6,且小于9.
具体实现步骤如下:
--[if!
supportLists]-->1.
--[endif]-->编写校验提示信息资源文件
--[if!
supportLists]-->i)
--[endif]-->在工程的src/cn/com/book/demo/struts目录下,添加一个ApplicationResource_temp.properties文件
--[if!
supportLists]-->ii)
--[endif]-->在里面添加几条提示资源信息,内容如下:
login.username=用户名
login.password=密 码
login.login=登 陆
login.reset=重 置
error.required={0}必须输入
error.minlength={0}的长度必须大于{1}
error.maxlength={0}的长度必须小于{1}
其中有{}包含数字的表示参数,在具体应用中,将有别的值替代
--[if!
supportLists]-->iii)
--[endif]-->在以前的ecoding.bat批处理文件里面,添加一行转换命令
native2ascii-encodinggb2312
ApplicationResources_temp.propertiesApplicationResources_zh_CN.properties
--[if!
supportLists]-->iv)
--[endif]-->运行ecoding.bat命令,生成一个ApplicationResource_zh_CN.properties文件
--[if!
supportLists]-->v)
--[endif]-->刷新工程,重新编译
--[if!
supportLists]-->2.
--[endif]-->编写校验Form
--[if!
supportLists]-->i)
--[endif]-->打开struts-config.xml设计器
--[if!
supportLists]-->ii)
--[endif]-->在设计器上,点击鼠标右键->New->Form,出现界面
--[if!
vml]-->
--[endif]-->
并在输入框内输入各自的值,Superclass选择org.apache.struts.validator.validatorForm选项,点击Finish
--[if!
supportLists]-->iii)
--[endif]-->打开刚刚生成的LoginForm.java文件,添加userName和password两个属性,以及对应的set和get方法,删除自动生成的validate方法和reset方法.代码如下:
/*
*GeneratedbyMyEclipseStruts
*Templatepath:
templates/java/JavaClass.vtl
*/
package.book.demo.struts.form;
importorg.apache.struts.validator.ValidatorForm;
/**
*MyEclipseStruts
*Creationdate:
02-29-2011
*
*XDocletdefinition:
*@struts.formname="loginForm"
*/
publicclassLoginFormextendsValidatorForm{
privateStringuserName;
privateStringpassword;
/*
*GeneratedMethods
*/
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
}
--[if!
supportLists]-->iv)
--[endif]-->
--[if!
supportLists]-->3.
--[endif]-->编写login.jsp文件
<%@pagelanguage="java"pageEncoding="UTF-8"%>
<%@tagliburi="http:
//struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http:
//struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http:
//struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http:
//struts.apache.org/tags-tiles"prefix="tiles"%>
formaction="/login.do" onsubmit="validateLoginForm(this)"> messagekey="login.username"/>: textproperty="userName"> text> messagekey="login.password"/>: passwordproperty="password"> password> submit> messagekey="login.login"/> submit> reset> messagekey="login.reset"/> reset>
form>
javascriptformName="loginForm"/> 代码注意事项: --[if! supportLists]-->i) --[endif]-->粗体部分,是校验框架在jsp中必须的代码 --[if! supportLists]-->ii) --[endif]-->Form里面的 text>和 password>的property值,必须同LoginForm.java里面添加的两个属性一致 --[if! supportLists]-->iii) --[endif]--> form>里面的action值,必须同struts-config.xml里面配置的登陆action的path值一致 --[if! supportLists]-->4. --[endif]-->编写LoginAction.java代码如下: /* *GeneratedbyMyEclipseStruts *Templatepath: templates/java/JavaClass.vtl */ package.book.demo.struts.action; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.apache.struts.action.Action; importorg.apache.struts.action.ActionForm; importorg.apache.struts.action.ActionForward; importorg.apache.struts.action.ActionMapping; import.book.demo.struts.form.LoginForm; /** *MyEclipseStruts * *XDocletdefinition: *@struts.actionpath="/login"name="loginForm"input="/login.jsp"scope="request"validate="true" *@struts.action-forwardname="error"path="/login.jsp" *@struts.action-forwardname="success"path="/userList.do" */ publicclassLoginActionextendsAction{ /* *GeneratedMethods */ /** *Methodexecute *@parammapping *@paramform *@paramrequest *@paramresponse *@returnActionForward */ publicActionForwardexecute(ActionMappingmapping,ActionFormform, HttpServletRequestrequest,HttpServletResponseresponse){ System.out.println("***************"); Stringpath="error"; LoginFormloginForm=(LoginForm)form;//TODOAuto-generatedmethodstub StringuserName=loginForm.getUserName(); Stringpassword=loginForm.getPassword(); if(userName! =null&&password! =null&&password.length()>6&&password.length()<9){ path="success"; } returnmapping.findForward(path); } } --[if! supportLists]-->1. --[endif]-->配置struts-config.xml --[if! supportLists]-->i) --[endif]-->在struts-config.xml里面添加一个loginaction如下 attribute="loginForm" input="/login.jsp" name="loginForm" path="/login" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> --[if! supportLists]-->ii) --[endif]-->在struts-config.xml中,添加一个校验插件如下 --[if! supportLists]-->iii) --[endif]--> --[if! supportLists]-->2. --[endif]-->编写loginForm的校验规则 --[if! supportLists]-->i) --[endif]-->确定WEB-INF目录下是否有validator-rules.xml,如果没有,请将demo工程里面WEB-INF目录下的validator.xml文件,copy到新工程 --[if! supportLists]-->ii) --[endif]-->在WEB-INF目录下,新建一个validation.xml文件,如果以前存在,就在代码中添加loginForm的校验规则,代码如下: xmlversion="1.0"encoding="UTF-8"? > DOCTYPEform-validationPUBLIC"-//ApacheSoftwareFoundation//DTDCommonsValidatorRulesConfiguration1.1.3//EN""http: //jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> --msg标签指定消息 name指定对应required校验器 key指定提示信息的key arg? 指定提示消息里面的替代 --> minlength}"name="minlength"resource="false"/> maxlength}"name="maxlength"resource="false"/> 注意事项: --[if! supportLists]-->a) --[endif]--> --[if! supportLists]-->b) --[endif]--> --[if! supportLists]-->3. --[endif]-->在src目录下的applicationContext.xml文件里面,添加 --[if! supportLists]-->4. --[endif]-->重新编译工程,发布到web容器,访问login.jsp,就可以演示输入值的校验了
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Struts 校验 框架
![提示](https://static.bdocx.com/images/bang_tan.gif)