45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:在Struts使用Validate的步骤

在Struts使用Validate的步骤

2016-08-30 12:58:31 来源:www.45fan.com 【

在Struts使用Validate的步骤

Steps to using validate() in ActionForm:

1.rewrite the method validate() in ActionForm file;

2.in resource file ApplicationResources.properties create error marks;

3.in struts-config.xml, set validate property of <action> to TRUE, and set <message-resources> element to indicate where the resource file locates;

4.in related jsp files, add <html:errors>.

Example:

1.

----------------------------------------------------

LoginActionForm.java

------------------------

...

publicActionErrorsvalidate(ActionMappingmapping,HttpServletRequestrequest){

ActionErrorserrors
=newActionErrors();

//Validateanattributenamed"xxx"

if(getUsername()==null||getUsername().length()<3){

errors.add(
"name",newActionMessage("error.username.required",username));

}

if(getPassword()==null||getPassword().length()<3){

errors.add(
"pwd",newActionMessage("error.password.required",password));

}

returnerrors;

}

...

----------------------------------------------------

2.

----------------------------------------------------

ApplicationResources.properties

---------------------------------

#Usernameerror

error.username.required
=<li>Pleaseinputyourusernameagain!</li>

#Passworderror

error.password.required
=<li>Pleaseinputyourpasswordagain!</li>

----------------------------------------------------

3.

----------------------------------------------------

struts-config.xml

-----------------

<?xmlversion="1.0"encoding="ISO-8859-1"?>

<!--

ThisisablankStrutsconfigurationfilewithanexample

welcomeaction/pageandothercommentedsampleelements.

TilesandtheStrutsValidatorareconfiguredusingthefactorydefaults

andareready-to-use.

NOTE:IfyouhaveageneratortooltocreatethecorrespondingJavaclasses

foryou,youcouldincludethedetailsinthe"form-bean"declarations.

Otherwise,youwouldonlydefinethe"form-bean"elementitself,withthe

corresponding"name"and"type"attributes,asshownhere.

-->

<struts-config>

<form-beans>

<form-beanname="loginActionForm"type="login.loginActionForm"/>

</form-beans>

<global-exceptions/>

<global-forwards>

<forwardname="welcome"path="/Welcome.do"/>

</global-forwards>

<action-mappings>

<actionforward="/pages/Welcome.jsp"path="/Welcome"/>

<actioninput="/login/login.jsp"name="loginActionForm"path="/loginAction"scope="request"type="login.loginAction"validate="true">

<forwardname="Success"path="/login/main.jsp"/>

<forwardname="Fail"path="/login/register.jsp"/>

</action>

</action-mappings>

<controllerprocessorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<plug-inclassName="org.apache.struts.tiles.TilesPlugin">

<set-propertyproperty="definitions-config"value="/WEB-INF/tiles-defs.xml"/>

<set-propertyproperty="moduleAware"value="true"/>

</plug-in>

<plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">

<set-propertyproperty="pathnames"value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

</plug-in>

<message-resourcesparameter="ApplicationResources"></message-resources>

</struts-config>

----------------------------------------------------

4.

----------------------------------------------------

login.jsp

----------

在Struts使用Validate的步骤...

在Struts使用Validate的步骤html:formaction="loginAction"method="post">

在Struts使用Validate的步骤Username:
<html:textproperty="username"size="15"/>

在Struts使用Validate的步骤<fontcolor="red"><html:errorsproperty="name"/></font>

在Struts使用Validate的步骤<br/>

在Struts使用Validate的步骤<br/>

在Struts使用Validate的步骤Password:<html:passwordproperty="password"size="15"/>

在Struts使用Validate的步骤<fontcolor="red"><html:errorsproperty="pwd"/></font>

在Struts使用Validate的步骤<br/>

在Struts使用Validate的步骤<br/>

在Struts使用Validate的步骤<html:submitproperty="submit"value="Login"/>

在Struts使用Validate的步骤</html:form>

在Struts使用Validate的步骤...

----------------------------------------------------

 

本文地址:http://www.45fan.com/a/question/69727.html
Tags: 方法 Struts Validate
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部