Thursday, January 21, 2010

Validation using validator framework

Validation on a form using validation form

1)Create a new web application
2)Add struts capabilities
3)create new action,form and jsp
4)Add the following lines in Struts-config.xml

//This is for validator plug in
plug-in className="org.apache.struts.validator.ValidatorPlugIn">
set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>


and in action tag ,add (validate="true") also.

6)Edit validation.xml
?xml version="1.0" encoding="UTF-8"?>
!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

form-validation>
formset>
form name="loginform" >
field property="name" depends="required,minlength" >
arg0 key="err.name" />
arg1 key="${var:minlength}" name="minlength" resource="false" />
var>
var-name>minlength
var-value>3
/field>
field property="pass" depends="required" >
arg0 key="err.pass" />
/field>
/form>
/formset>
/form-validation>

7)Edit Form JSP

html:javascript formName="loginform"/>
html:form action="login" onsubmit="return validateLoginform(this)">

onsubmit return validateFormName(this).

8)Test your application

Thanks
deepak

Thursday, January 14, 2010

Use Tiles in your Struts Application

Application Using Tiles:

For Eclise:
1) Create New Web-Project
2)Add Struts Capabilities.
3)Create "layout.jsp"
Use this Syntax:
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

tiles:insert attribute="header"/>
How you want to show your page.Like header,footer,main,left,body etc.Specify it here.

4)Make following Entry in "tiles-defs.xml"

tiles-definitions>
-- Base Tiles Definition
This is the main definition.You extend it in other definition.
It means if you can use same jsp or override it.
-->
definition name="base.definition" path="/Layout.jsp">
put name="header" value="/WEB-INF/JSP/header.jsp" />
put name="left" value="/WEB-INF/JSP/LeftLink.jsp"/>
put name="left" value="/WEB-INF/JSP/welcome.jsp"/>
put name="footer" value="/WEB-INF/JSP/footer.jsp" />


-- Tiles Definition of welcome page -->
definition name="page.welcome" extends="base.definition">
put name="title" value="Welcome page" />
put name="body" value="/WEB-INF/JSP/welcome.jsp" />




5)Edit Struts-Config.XML

Required part:
plug-in className="org.apache.struts.tiles.TilesPlugin">
set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
set-property property="moduleAware" value="true" />
set-property property="definitions-parser-validate" value="true" />


And define your actions as you want.
path="/welcome.do"
redirect="true" />

action-mappings>
action path="/welcome"
type="home.WelcomeAction">
forward name="showWelcome" path="page.welcome" />


action path="/leftlink"
type="home.LinkAction">
forward name="showLink" path="page.left" />



6)Create jsp according to your view and URL which you are giving in tiles-defs package

7)Create your actions.

8)Run your application.

I think it is useful.If any required step is still remaining,plz correct it.

Thanks
Deepak