>> Delivering Value

Session - 10
June, 2009 Presentation By Training & Development

Struts 2.0

Good Evening everybody! Welcome to the Day 10 of the Struts 2 training course


 Day 1 – History of Web applications
It will be another exciting day of learning. Let us now do a quick recap on what we have learned so far:

 Day 2 – Concepts to be known before

learning Struts 2

 Day 3 – Struts 2 environment setup, Hello

World Project, PhD Project Stack, OGNL

 Day 4 – Dissection of Hello World, Value  Day 5 – Action Interface, Action Support

class, Validation, Resource Bundle and Internationalization Struts 2

 Day 6 – Validation Framework provided by  Day 7 – Writing your own interceptors  Day 8 – Action Context, accessing Session

and SessionAware interface Interfaces

 Day 9 – Preparable and ModelDriven


Ms. Natasha
Let us now let Thinker, Dumbo and Jumbo refresh your memory by providing reviews of important points over the past week


Dumbo Vs. Jumbo
if there is a validation error in the XML then will the validate method of action class be executed?

Yes, it will be executed because it is always better to display the errors at one go to the user


Mr. Thinker
What should be the name of the validation xml if I have just one business method inside the action class? <action class>validation.xml


Mr. Thinker
What should be the name of the validation xml if I have more than one business method inside the action class and I want various validations for various business methods?

<action class>-<action name>-validation.xml


Mr. Thinker
What is the validator type to be used for complex validations? Expression


Dumbo Vs. Jumbo
When is the error message displayed for a validator type of expression?

If the expression mentioned inside param returns false


Mr. Thinker
If your validation is not inside the field element, where is the error message displayed in the page?

The error message is displayed as action error under the page scope


Dumbo Vs. Jumbo
What is the parameter for intercept method?



Dumbo Vs. Jumbo
In the intercept method what should you do not break the chain of invocations?

You should call the method actionInvocation.invoke( )


Dumbo Vs. Jumbo
What is the name of the parameter which is used to specify the name of the methods for which the interceptors should not be invoked?



Mr. Thinker
What is the name of the parameter which is used to specify the name of the methods for which the interceptors should be invoked? includeMethods


Mr. Thinker
How do you pass parameters to an interceptor? Using the param element


Mr. Thinker
How can I pass different values for the same parameter in different actions? Use param name=”interceptor.para mname” instead of just parameter rname


Mr. Thinker
How many methods does the Preparable Interface provide? One


Dumbo Vs. Jumbo
What is the signature of the method?

void prepare( )


Dumbo Vs. Jumbo
Which interceptor in turn uses this method



Dumbo Vs. Jumbo
The defaultStack already PrepareInterceptor. True or false? includes the



Dumbo Vs. Jumbo
What kind of logic will you include in the prepare method?

We will include preparatory logic in this method (Eg. Preparing the model object by getting the fields values from the session/DB and setting in the model object etc)


Mr. Thinker
What is the name of the interface used to create a Model Object in the value stack? Model Driven


Mr. Thinker
What is the method exposed by this interface? Object getModel( )


Mr. Thinker
What is the name of the interceptor that uses this interface? ModelDriven Interceptor


Mr. Thinker
The defaultStack does not include ModelDrivenInterceptor. True or false? False. The defaultStack includes this interceptor


Dumbo Vs. Jumbo
What is the advantage of having the Model Object?

You can directly start referring from inside the model object so the ognl will be less complex. The other thing is that it is stored similar to the action object (separately) in the value stack


Dumbo Vs. Jumbo
How many model objects will be present in the Value Stack at any point of time?

Only one, since there can be only one action object at any time. One action object can contain only one model object


Ms. Natasha
Let us now learn today’s topic Today we will be learning about the various UI tags


Type of UI Tags
 Data Tags  Control Tags  Component Tags


Data Tags – s:property
 S:property provides an easy way of writing a property into the rendering


<s:property value="user.username"/>


Data Tags – s:set
 The set tag is used to assign a value to a property  In simple programming language terms it is similar to assigning a value to a


 In Struts terms the simplest reason to use this tag is for convenience  You can assign a complex OGNL to a variable with simple name, then keep

referring this simple name instead of the complex OGNL


Data Tags – s:set - usage
<s:set name="myName" value="user.username"/> Hello, <s:property value="#myName"/>. How are you?  In the above case, an object with name myName is created in the default scope

which is Action Context

 If you want to change the scope, here is an example <s:set name="myName" scope=”application” value="user.username"/> Hello, <s:property value="#application[‘myName’]"/>. How are you?  Please note that the above is also an example of retrieving data from within action

context’s various types of objects like application, session etc. the user’s name, you can retrieve it as

 For example, if you had an attribute called USER_NAME in session which stores
 <s:property value="#session[‘USER_NAME’]"/>

Data Tags – s:push
 While the set tag is used to create new references to existing values, push tag

allows you to push properties on to the value stack object

 This is useful when you want to do a lot of work revolving around a single  For example if you keep using user object, then instead of referring it every

time with a complex OGNL, you can push the user object so that it becomes a top level object. Then referring it would be much simpler


Data Tags – s:push – usage
<s:push value="user"> This is the "<s:property value="portfolioName"/>" portfolio, created by none other than <s:property value="username"/> </s:push>  In the above example, the push tag pushes the user object onto top of the value


 The end of the push tag removes the user object from the top of value stack.  Between these two tags you can refer the attributes inside user object directly


Control Tags – s:iterator
 Widely used  You use iterator tag to loop over collections of objects easily  It can be used to loop over Collection, Map, Enumeration, Iterator or array


Control Tags – s:iterator - usage
<s:iterator value="users" status="stat"> <li> <s:property value="#stat.index" /> <s:property value="userName"/> <s:property value="firstName"/> <s:property value="lastName"/> <s:property value="age"/> </li> </s:iterator>  In the above example, the action object exposes a collection of users. The iterator tag

iterates over this set of users

 During the body of the tag, the corresponding user is placed on top of the value

stack so that you can refer to the attributes of that particular user object provided by the framework to get various values

 Please note that there is another object with key stat.This is a convenience object


Control Tags – s:iterator – IteratorStatus Object
 The IteratorStatus object contains the following – getCount – count of the iterator – getIndex – the current index – isEven – whether the current row is even – isOdd – whether the current row is odd – isFirst – whether the current row is first – isLast – whether the current row is Last  You can use these methods just like any other getter methods


Control Tags – s:if, s:elseif, s:else
<s:if test="user.age > 35">This user is too old.</s:if> <s:elseif test="user.age < 35">This user is too young</s:elseif> <s:else>This user is just right</s:else>  The test attribute of this tag is the attribute that provides the actual condition  You can just have s:if or all the three or just s:if and s:else


Control Tags – s:text
 This is used for i18n  The usage is pretty simple <s:text name="hello"></s:text>  Correspondingly there should be an entry in the resource bundle with key as hello  The corresponding value will be rendered in the HTML


 Create 5 users in the action class. You can hard code them  In the JSP, display these 5 users following data in a tabular form – S.No – First Name – Last Name – Age – Occupation  Take these headings from a resource bundle instead of hard coding them  If there are no elements in the user collection, then display a

corresponding error message


UI Component Tags
 Text field <s:textfield required="true" name="person.myName" label="My Name is" size="30"/>  Password <s:password required="true" name="password" label="password" />  Checkbox <s:checkbox name="receiveJunkMail" fieldValue="true" label="Check to receive junk mail” />  Select Box <s:select name="user.name" list="{'Mike','Payal','Silas'}" />


UI Component Tags (Contd.)
 The above is translated into <select name="user.name" id="ViewPortfolio_user_name"> <option value="Mike">Mike</option> <option value="Payal">Payal</option> <option value="Silas">Silas</option> </select>  Select Box – Dynamic data in drop down <s:select name="username" list='users' listKey="username" listValue="username" label="Select an artist" />  Hidden <s:hidden name="username" value=”user.name” />


How things happen internally


• You need to create an application for User Account Creation of a

neighborhood pizza store. The following are the details • Basic Details • User name • Password • First Name • Last Name • Gender (Drop down with M and F being the codes) • Age • Address Details • Address1, Address 2, city, Zip • Miscellaneous • Agree to terms? – checkbox • Inform about offers? – checkbox • On click of submit, provide a success message


Session 10
We hope you thoroughly enjoyed today’s session. Have a good evening and we will meet you in the next session


Session 10
Thank You !!


Sign up to vote on this title
UsefulNot useful