>> Delivering Value

Struts 2.0
Session - 7
June, 2009 Presentation By Training & Development

Introduction
Good Evening everybody! Welcome back to the Day 7 of the Struts 2 training course

2

Introduction
 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 Value Stack, OGNL

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

Support class, Validation, Resource Bundle and Internationalization by Struts 2

 Day 6 – Validation Framework provided

3

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

4

Mr. Thinker
Who authored Struts Craig Mc Clanahan

5

Dumbo Vs. Jumbo
Web Work is based on what Framework?

X Work

6

Mr. Thinker
X Work implements which design pattern? Command Pattern

7

Dumbo Vs. Jumbo
What is the minimum java requirement to use Struts 2?

You should be having at least java 1.5

8

Mr. Thinker
What are the advantages of Struts 2? (whatever we have realized till now?) No request. get parameters, no type conversions, no forwards/redirects, etc

9

Mr. Thinker
What is value stack? Value stack is a stack of objects

10

Dumbo Vs. Jumbo
I have an object called PurchaseOrder inside which I have Asset inside which I have Invoice. Inside Invoice I have invoiceNo. How can I access invoiceNo using OGNL?

purchaseOrder.asset.invoice.invoiceNo

11

Mr. Thinker
What is the name of the interceptor that sets the variables of action class from the screen fields? ParamsInterceptor

12

Mr. Thinker
What is the logical name of the interceptor responsible for displaying errors when there is a problem while converting string field to specific java types? ConversionError

13

Mr. Thinker
What is the class name of the interceptor which executes the validation framework? AnnotationValida tionInterceptor

14

Dumbo Vs. Jumbo
Validation interceptor gets executed after workflow interceptor. True or false?

False. Validation interceptor gets executed before workflow interceptor and after params interceptor

15

Dumbo Vs. Jumbo
Through what do we implement validation meta data?

We implement validation meta data using an XML File

16

Mr. Thinker
What is the purpose validation meta data? of The validation meta data wires fields with corresponding validator types

17

Mr. Thinker
Give examples of validator types? Required, Requiredstring, int, double, stringlength, email etc

18

Mr. Thinker
What is the parameter to specify the minimum size of a text box in the validation framework? minLength

19

Mr. Thinker
What is the class name of the interceptor which executes the validation framework? AnnotationValida tionInterceptor

20

Dumbo Vs. Jumbo
What is the significance of camel case in minLength

The corresponding validator class will just prefix “set” and then change the case of the next letter and check if there is a corresponding method in the validator class. If not then it will ignore the whole validation itself. Hence we need to be careful with the camel case

21

Dumbo Vs. Jumbo
How do we specify the minLength variable value

using <params name=”minLength”>3</param>

22

Dumbo Vs. Jumbo
What is the method to add an error above the field?

this.addFieldError

23

Mr. Thinker
What are parameters addFieldError? the for Field Name, Error message

24

Mr. Thinker
What is the method to retrieve an error message from the resource bundle? this.getText

25

Mr. Thinker
What is the key in struts.properties to specify the resource bundle properties file? struts.custom.i18n .resources

26

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

27

Dumbo Vs. Jumbo
How do you enclose OGNL expression inside validation xml?

${ }

28

Dumbo Vs. Jumbo
How do you enclose OGNL expression inside JSP?

%{ }

29

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

30

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

31

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

32

Dumbo Vs. Jumbo
In the validation for expression where do you actually provide the expression?

We provide the expression inside the element called param with name = expression

33

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

If the expression mentioned inside param returns false

34

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

35

Ms. Natasha
Thanks. Let us quickly move on to today’s session. We will cover a very interesting concept today. i.e. writing your own interceptors. Sounds interesting? Keep listening.

36

Writing Your Own Interceptors

37

Writing Your Own Interceptors
 Three ways of creating your own interface – Implement interface called Interceptor – Extend class called AbstractInterceptor – Extend class called MethodFilterInterceptor

38

Implementing Interceptor
 Implementing Interceptor – The Interceptor interface contains three methods:
public interface Interceptor extends Serializable { void destroy(); void init(); String intercept(ActionInvocation invocation) throws Exception; }

– Destroy and init methods are life cycle methods which are used to

initialize and clean up resources – The intercept method is the real business method which would be invoked by the ActionInvocation.invoke( ) method – As the implementor you need to override these three methods

39

Extending AbstractInterceptor
 AbstractInterceptor is again a convenience class which has already

implemented Interceptor for you

 Init and destroy methods are already implemented  The intercept method is declared as abstract  If you want to extend this class, you just need to define the intercept

method and provide the logic in this method

40

Dumbo Vs. Jumbo
I am able to understand these. But what is this MethodFilterInterceptor? The name sounds interesting

To understand this, you need to understand the concept called method filtering. I will let the trainer explain this concept to you before we jump into method filter interceptor

41

Method Filtering
 In the action result we used to have various values like input, success, cancel or

any other user defined value

 Method filtering is a concept which can be used if you do not want an

interceptor to be invoked for particular methods
– I have page 1 which takes Personal Details – I have page 2 which takes Address Details

 For example, let us take this scenario:

– When I click on Next from Page 1, Page 2 should be displayed – There are 4 mandatory fields in Page 2 – There is a submit button in page 2 on click of which, the data will be

submitted to the database

42

Method Filtering (Contd.)
 But as soon as Page 2 is displayed, the mandatory error messages should not

be displayed because this is the first time the system is displaying Page 2 and hence it is not user’s mistake

 Only on click of Submit in Page 2, the error messages should be displayed

43

How do we achieve the above?
 Create an action element in struts.xml specifically for displaying Page 2 for

the first time like this:

<action name=”page2Display” action=”MyAction” method=”input”> <result>Page2.jsp</result> </action>

 Now on click of next in Page 1, use the action that you just declared  Now focus your energy on struts-default.xml under defaultStack  You will find the following:
<interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref>

44

How do we achieve the above? (Contd.)
 As you have guessed, excludeMethods is a variable inside the

AnnotationValidationInterceptor not be called

 For input, back, cancel and browse methods this particular interceptor will  In other words this interceptor will be skipped for all methods mentioned

under excludeMethods parameter

 For interceptors which require such a filtering concept you extend the

MethodFilterInterceptor while writing the interceptor

 Now let us see what are the methods in MethodFilterInterceptor

45

MethodFilterInterceptor
«interface» Interceptor

AbstractInterceptor

MethodFilterInterceptor

46

MethodFilterInterceptor
 MethodFilterInterceptor is an abstract class  MethodFilterInterceptor extends AbstractInterceptor and hence overrides

the intercept method

 But it provides a new method called doIntercept which is abstract  If you want your interceptor to extend this class, then you need to override

doIntercept method

 You should not touch intercept method  Let us now look into the source code of MethodFilterInterceptor to

understand things better

47

Summary
 If your interceptor implements Interceptor interface, you need to override, init,

destroy and intercept methods

 If your interceptor extends AbstractInterceptor abstract class, then you need to

override just intercept method (but method filtering will not be available)

 If your interceptor extends MethodFilterInterceptor abstract class, then you need

to override just intercept method (method filtering will be available for you)

48

Steps in creating the Interceptor
 Use any of the above three methods and write your own interceptor – In this please note that you should implement the corresponding method

(either intercept or doIntercept) – At the end of the method, do not forget to invoke ActionInvocation.invoke( ) – If you do not do this you will break the Interceptor chain and your action itself will not be invoked

49

Steps in creating the Interceptor (Contd.)
 Declare your interceptor in struts.xml as follows
<interceptor name="myInterceptor" class="MyInterceptorFullClassWithPackage"/>

 Use this interceptor in an interceptor stack (Example as follows)
<interceptor-stack name="myStack"> <interceptor-ref name="myInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack>

 You are all set!!!  Please note that in this case myInterceptor will be invoked first before

defaultStack is even started

50

Exercise
 Whenever I invoke any action class I need the

following details to be displayed in the log files – Name of the action class – Package of the action class – All the fields of the action class – All the methods of the action class of the project” in the log file if it is provided

 I should also have a provision to write the “name

51

Case Study
It was a short and sweet session today I am sure you now have a sense of achievement since you developed your own interceptor and plugged this into the struts 2 framework

Don’t you feel that you have just extended the framework itself. Great work guys!! We will all see you tomorrow with an equally interesting session. Till then its bye bye from all of us

52

Session 7
Thank You !

53

Sign up to vote on this title
UsefulNot useful