This article is for those who are not new to the web application world and but new to Struts 2. Specially if you know what struts 2 is and what it does but you don't know how to implement it or you are not finding a simple example to explain it after browsing all over the internet then this is what you are looking for. I would not waste your time saying what it is and what it does and would directly go to the implementation part of it.
- JSP Page : In your jsp page you have to mention struts taglib tags. That is something like this.
<%@ taglib prefix="s" uri="/struts-tags"%>
Now in your forms and input fields also you have maintain that same prefix. They should be something like this.
<s:form id="loginForm" action="actionLogin".........
<s:textfield id="loginuser" name="loginUser.userName".................. - Struts.xml : Your Struts.xml should contain the mapping of all the action that you define in your JSP Page. For example actionLogin which we have mentioned in the action of our form, we need to define mapping for that. Code snippet is given below.
<action name="actionLogin"
class="com.iyouth.common.login.action.LoginCustomizationAction"
method="actionLogin">
<result name="success" type="redirect"> loadMainPage.action</result>
<result name="failure" type="redirect">login.action</result>
</action>
What it means is the following. For action name acctionLogin, it will go to the class LoginCustomizationAction and ill hit the method actionLogin. This method should ideally return a string. If that string is success then the page control should go to the loadMainPage action which in turn should call a landing page. - Action Class : We have defined upto now that for a specific action, a specific method of the action class will be called. But we have to implement this class as well as the method. You can ideally create one base action class and make all your other action class to extend from that. BaseAction class should be something like this......BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware{
................
}
Now your login action class should look something like this......
public class LoginCustomizationAction extends BaseAction implements ModelDriven<LoginDetails>Here, LoginDetails is the VO for the JSP Page. We will talk about that later but just for the continuation I want to add that a VO is which stores all the user provided UI data. Now lets come back to the login action class. It will have one overridden method getModel() which should return the object of the VO. Now we should implement the actionLogin method which we mentioned in the struts.xml. In this method we just call a service class which will be responsible for all the business logic and our action method will pass the VO to the service method and will just return a string based on the result of the operation. Like in this case, it should return success or failure as mentioned in the Struts.xml. - VO : VO is basically a pojo having some attributes same as the input fields in the UI page and setters and getters for them. Like for the textfield userName, it should have an attribute with the same name along with its setters and getters.
- Service Class : Service class is typically a layer where all the user data from the action class will be processed so that they can interact with the database and then will send them to the DAO class for database interaction. The result can again be processed and will be sent to the action class for UI rendering.
- DAO Class : It is responsible for interacting with the database. Mainly it fetches data, insert data or update database based on the requirement and send back the result to the service class.
All the basic data forwarding is done by the VO which acts as a data container for different layer. That's it! This is all you have to do to use struts for your web application. But don't forget to put the required jars in your lib!!!
cheers,
Subhankar