Tuesday, March 3, 2009

Saving Form Parameters In Strtuts 2

This post intends to answer the following questions : -
  1. How to store details entered in a form in a Struts 2 application?
  2. Do we have an action form class in struts 2?
  3. How to use my action class to store form data?
The most important question that needs to be answered here is question number 2. We do not have any action form in struts 2 like it used to be in struts 1.

Struts 2 makes it possible to make use of the action class itself as the class that can store the details submitted by the user in a form or sent as parameters in the request. This task is performed by the ParametersInterceptor. It is the task of the parameters interceptor to inject the values passed in the request to the struts 2 instance fields. This interceptor is used to call the appropriate setter method of the action class to inject the parameter into the instance variable. The only requirement is that you the name of the parameter and the setter must be the same.

For example, suppose that you have a form in which you have a textfield called 'age' which is submitted with the form. Now, to automatically save this value in the action class, you need have a method called setAge(int age), in the action class. The Parameters interceptor takes care of parsing the arguments to the appropriate data type. You can now save the age in a private instance variable. If you create a getter method such as getAge(), then this can also be called directly form the jsp page by using the property tag, if the action class in the value stack. Just make sure that the getter and setter methods are public so that they can be accessibe anywhere.

Happy Programming ;)

Signing Off
Ryan

Specifying Action Method In Struts 2

This post intends to answer the following questions : -
  1. How call multiple methods of the same action class as separate actions?
  2. How to configure different methods as action methods?
  3. Can we configure methods other than execute() to be called when we have call an action?
Struts 2 makes it possible to call any ppublic method in a class instead of the execute() method. This is possible by simple making a small configuration change in the struts.xml file for the required action.

By using this feature you can actually create a single action class and have many methods that perform different tasks. You can have as many different actions as there are methods in your class. You need not make any changes anywhere in your action class to enable it to service more than one type of action request. This is simply done by using the struts.xml file. However your functions must have the following syntax

public String fucntionName()
{
}

Consider an action class with the following two functions

MyActions.java
------Source Code Starts Here------
.
.
import .......
.
.
public class MyActions extends ActionSupport
{
.
.
.
public String method1()
{
//some code
//return SUCCESS/ERROR etc etc
}


public String method2()
{
//some code
//return SUCCESS/ERROR etc etc
}
------Source Code Ends Here------

Now, in order to call these methods on different action requests, the following configuration in struts.xml will suffice :-

struts.xml
------Source Code Starts Here------
.
.
.
<action name="action1" class="MyActions" method="method1">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/error.jsp</result>
</action>
<action name="action2" class="user.My" method="method2">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/error.jsp</result>
</action>

.
.
.
------Source Code Ends Here------

Notice the 'method' attribute that is used to specify the method that needs to be called for an action. All the rest of the code is the same as in regular action classes.

Happy Programming ;)

Signing Off
Ryan

Monday, March 2, 2009

Using The if, else and elseif tags in Struts 2

This post intends to answer the following questions :-
  1. How to use the if tag in Struts 2?
  2. What are the uses of the if else tag in Struts 2?
  3. How many flavors of the if else tag are there in Struts 2?
The all the flavors of the if else tag are control tags in the Struts 2 framework. As in every programming language, such control structures are used to evaluate a condition and then do something if the condition is true and do something else if the condition is false. But then you already knew that, didn't you? There are three basic flavours of this tag. I shall demonstrate each with an example.


Flavour1-Chocolate!

<s:if test="condition">

This will be printed if the condition is true.
</s:if>


Flavour2-Vanilla!
<s:if test="condition">
This will be printed if the condition is true.
</s:if>
<s:else>
This will be printed if the condition is false.
</s:else>

Flavour3-Strawberry!!
<s:if test="condition">
This will be printed if the condition is true.
</s:if>
<s:elseif test="second_condition">
This will be printed if the second_condition is true.
</s:elseif>
<s:else>
This will be printed if the second condition is false.
</s:else>

So, there you go, three delicious flavors to add to your ice cream bowl. you can eat them separately or you can mix them all up! Its all in your hands, or I would rather say, all over your hands! Feast on it!

Happy Programming ;)

Signing Off
Ryan

Using property tag in Struts 2

This blog post intends to answer the following questions :-
  1. What is the usage of the property tag in Struts 2?
  2. How can I display a default value if a property is not present on a jsp page?
  3. What does the property tag do in Struts 2?
The property tag in Struts 2 is highly useful when you need to display contents on the screen, especially those associated with the session or context objects. The struts 2 framework makes use of the OGNL and value stack, which makes accessing these scoped attributes easier than before, enabling you to completely eliminate the need of any scriplet code or expression in your pages. The property tag can access any object that is visible in the value stack. All that you need to do is to provide the name of the object to the 'value' attribute of the tag.

As an example assume that there is an attribute in the session scope called 'username'. Now, to print the value of this attribute, I need to do the following.

<s:property value="#session.username"/>

Consider another scenario, when you submitted a form with a textfield called 'location' and it calls an action. This action has a setter and a getter method for the location field. The action does some processing and results in a jsp page being displayed. Now, in order for you to display this 'location' field on your resulting jsp page, all that you need to type is : -

<s:property value="session"/>

This is because since the action was the last thing that was called in the calling chain, the instance of the action class was on the top of the stack and hence we did not have to write any code to specify the instance whose getLocation() method we were attempting to call.

Sometimes, it is useful to display sensible defaults on your jsp page when the property tag retrieves value that is null. i.e. the value is not on the stack. For example, you might want to display a message saying that a user needs to login if the username is not in the session. This can be done as follows.

Welcome !! <s:property value="#session.username" default="Login To Use Your Superpowers"/>

Happy Programming ;)

Signing Off
Ryan

Sunday, March 1, 2009

Using url and param tags in Struts 2

This blog post intends to answer the following questions : -
  1. How to make use of the url tag in Struts 2?
  2. How to invoke an action from a url link in Struts 2?
  3. How to pass parameters to an action from a url link in Struts 2?
  4. How to use the url tag to invoke actions in Struts 2?
The url tag in Struts 2 can come in very handy if you need to use a url in many places and dont want to repeat the code again and again. The url tag is simple and extremely easy to use. You need to specify an id attribute for your url and you can also use actions instead of simple links in the url. Adding parameters to the url is simple and is done by using the param tag.

...
...
<s:url id="someId" value="http://myurl.com"/>
<s:a href="%{someId}">Click Me</s:a>
...
...
You can also specify a relative url.


Actions can also be called instead of url's. This is done as follows.
...
...
<s:url id="someId" action="actionName"/>
<s:a href="%{someId}">Click Me</s:a>
...
...

Here is an example where you can pass parameters to the action by using the param tag in the url.
...
...
<s:url id="someId" action="actionName">
<s:param name="parameterName">parameterValue</s:param>
</s:url>
<s:a href="%{someId}">Click Me</s:a>
...
...

That's all there is to it folks!

Happy Programming ;)

Signing Off
Ryan

Creating Action Classes In Struts 2

This blog post intends to answer the following questions : -
  1. How to create Action classes in Struts 2?
  2. Can we use POJO's as action classes in Struts 2?
  3. How to use a POJO as an action class in Struts 2?
  4. What are the interfaces or classes one needs to extend to create an action class in Struts 2?
The Struts 2 programming model makes it very easy to create action classes. The Dependency Injection pattern enables us to add Struts 2 functionality to a variety of classes.

Basically Struts 2 allows you to create Action classes in three different ways :-
  1. Implement the org.apache.struts.action.Action interface.
  2. Extend the com.opensymphony.xwork2.ActionSupport class.
  3. Or simply use any POJO as an Action class.
Implementing the Action interface is very useful in cases when your class belongs to some other class hierarchy. Extending the ActionSupport class enables us to to make use of a large number of features and constants that are available through the various interfaces implemented by the ActionSupport class. ActionSupport provides empty implementations of a large number of abstract methods for many interfaces.

Using a POJO is quite simple as well. All that is required of you is to create method in the POJO with the following signature : -

public String execute()
{
//method code
}

So finally, getting down to the actual business details is not gonna take millions of years! Have fun!

Happy Programming ;)

Signing Off
Ryan

Return Type String Constants In Action Interface

This blog post intends to answer the following questions :-
  1. What are the String constants defined in the Action interface?
  2. What are the different String constants in the Action interface used for?
  3. What is the use of the following- SUCCESS, INPUT, ERROR, LOGIN, NONE?
The Action interface defines fines String constants that can be used as return types from any function in a Struts 2 Class. Since we use the execute method in most Struts 2 applications as our method where all the so called action takes place, these string constants are mostly used as return values in this function.

All the five string constants have a different purpose to solve in the Struts 2 framework.
  1. SUCCESS :- This return type is used to indicate that the result of the operation has been successful and the application is eligible to flow as in normal operating conditions.
  2. INPUT :- This return type is used to indicate that the requested action cannot be completed due to inappropriate or insufficient data entered by the user.
  3. ERROR :- This return type is used to indicate that a certain error condition was produced while performing the requested operation.
  4. LOGIN :- This return type is used to indicate that the action need proper authentication before it can perform the operation.
  5. NONE :- This return type indicates that the completion of the action is not associated with the display of a view component to the user.
You can also return your own strings form your action classes instead of the above mentioned ones. But then be sure to map your custom return value to a view in the struts.xml file using the result element as follows.

<result name="my_own_string">/myview.jsp</result>

Happy Programming ;)

Signing Off
Ryan