Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

An Introduction To Struts 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

An Introduction to Struts 1.3 Frameworks Advantages of Struts1.3 1. Centralized:- Struts1.

3 provides centralized controller and centralized xml file to configure all actions, plug-in ,form bean etc. and also provides XML or properties file in which we can write valuable information rather than hard-code into our java code file. 2. Maintaining and development:- As in struts1.3, There is clear separation of model, view and controllers so its maintaining cost is low and at development time GUI .Developer have focus only GUI ,business logic developer have focus on writing business logics and controller developer have focus on only networks securities ,traffics etc. 3. ActionForm Beans:- When we are using JSP to send form data we have use property="*" with jsp:setProperty .This will automatically populate data a JavaBean component based on incoming request parameters. Unfortunately there is no any standard API through which we can do this into servlets. Apache Struts extends this capability to Java code and adds in several useful utilities, all of which serve to greatly simplify the processing of request parameters. 4. Struts-Tags Library:- Apache Struts provides a set of Bean tags (bean:write, in particular) that let us easily output the properties of JavaBeans components. Apache Struts provides a set of HTML Tags to create HTML forms that are associated with JavaBeans components. This bean/form association serves two useful purposes: It lets you get initial form-field values from Java objects. It lets you redisplay forms with some or all previously entered values intact. 5. Form Field Validation:- Apache Struts provides validate frameworks to validate data like formats, minimum length, maximum lengths. Also maintains the previous stats of from data. This validation can be performed on the server (in Java), or both on the server and on the client (in JavaScript).

There are two types of MVC design pattern

MVC-1 Design Pattern:- MVC -1 have more than one controllers .Each request may have own controller in case of MVC-1.We can create controller using JSP or Servlet. MVC-2 Design Pattern:- MVC-2 has only one controller .The servelet is used to create controller for MVC-2.All request must be handled by single controller and only this controller can decide the response GUI on the basis of model calculation and manipulations.

Descriptions: Struts ActionServlet class:-The Struts Framework gives developers to make an application that has flexibility to use any approach or technology for building the Model layer code like Object Relational Mapping (ORM) Frameworks (e.g. Hibernate or TopLinkor or iBatis), Enterprise JavaBeans (EJB), Java Data Objects (JDO), or the Data Access Objects (DAO) pattern.Model is where Struts1.3 is ends and our application code (code for business logic) beginnings .Struts provides ActionServlet class which is part of controller layer. The org.apache.struts.action.ActionServlet is the backbone of all struts application.This is the main controller component the handle client request and processing each request .It serves as an Action Factory based on the user's request.

All the Action class in the Struts model extends the org.apache.struts.action.Action class. The subclasses of Action class interact with the Model via interfaces and use its Data Transfer Objects to pass and retrieve data. If we are using data code or business logic on subclasses of Action class then we are making a bypass the separation of the Model layer and the Controller layer. Doing so is violating the separations of model and controller. Struts Result Component- Struts provide a so many numbers of functionality and features for developing the View layer. We can use HTML/JSP, XML/XSLT, Velocity, and Swing, to make view in Struts based applications. This is the power of Struts and MVC. Because HTML/JSP is the typical View technology used for Java-based Web applications, Struts provides the most functionality and features for developing your application this way.

The Model:- System State & Business Logic JavaBeans Introduction: A model represents appication's data and state and contains the business logic for accessing and manipulating that data.where data that is part of the persistent state of the application. ActionForm bean represents the Model state and not at a persistent level.The controller can accessed model services effecting a change in the model state.The model notifies the view when a state change occurs in the model. Descriptions: Set of one or more JavaBeans represents the internal state of the system .Entity Enterprise JavaBeans (Entity EJBs) are also commonly used to represent internal state. The actions are part of Controller. In struts1.3 we are using model, the model represent data and business logic which are present in JavaBean and Action class in this tutorial we are see the format of JavaBean which are following and format of Action class also which are following The View: -JSP Pages & Presentation Components Descriptions: Struts HTML/JSP View layer support can be broken down into the following major components:- >>JSP page >>Form Beans >>JSP tag libraries includes Struts include an extensive custom tag library that facilitates creating user interfaces that are fully internationalized. These are :1. The HTML Tag Library. 2. The Bean Tag Library. 3. The Logic Tag Library. 4. The Nested Tag Library. Resource bundles In this tutorial provide the information of view in the struts1.3 framework. The view portion are written in JSP,Velocity,etc. The view provide the presentation logic of this MVC pattern.

The Controller: ActionServlet and ActionMapping Introduction: The controller's main job is to map requests to action i.e. the controller is the first component to act in the processing. So, controller function as a backbone for struts based application. Struts have Action class and ActionServlet class which is used as controller. Controller servlet is most core of a Struts based application. The Controller handles all requests from the user and selects the view to return. When the Controller receives a request from user, Controller forwards the request to the appropriate handler, which interprets what action to take based on that request. Struts provides a ActionServlet class which is responsible for initializing a Struts applications configuration file and receiving all incoming request and on the behalf of request ActionServlet populating the Form Bean with data, validating the From bean and then selecting the appropriate Action class to execute. The Action class is where the Struts Frameworks is ends and applications model begins.After doing calculations and manipulations Action class returns a key on the behalf of this key ActionServlet class will decides the next view. The ActionServlet Class is controller class that receives all incoming requests for the application. ActionServlet is responsible for initializing the Struts framework for your application. Like other servlet ActionServlet must be configured in web.xml (in Web application deployment descriptor). Descriptions: There are two ways that ActionServlet can be configured to receive requests in web.xml.

First, ActionServlet can be configured using path mapping, as shown here: Path mapping routes to ActionServlet all requests that match a specified path. The default path is /do/*. action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 1 action /do/* The second way to map requests to ActionServlet is to use extension mapping. Extension mapping maps to ActionServlet all requests with the specified

extension. The default extension to use is *.do action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 1 action *.do web.xml <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

struts-config.xml <struts-config> <form-beans> <form-bean name="helloForm" type="com.r4r.struts.HelloForm"/> </form-beans> <global-exceptions /> <global-forwards>

<forward name="helloAction" path="/helloAction.do"/> </global-forwards> <action-mappings> <action path="/helloAction" type="com.r4r.struts.HelloAction" name="helloForm"> <forward name="success" path="/success.jsp"/> </action> </action-mappings> <message-resources parameter="com.r4r.struts.ApplicationResources" /> </struts-config> When the request comes from client then ActionServet calls an Actions class which is subclass of org.apache.struts.action.Action We have to override the execute() method. Descriptions: The execute() method returns type is ActionForward. The execute methods forwards a key.On the behalf of this key ActionServelet decides the next view and give response to client.The execute method takes four arguments:

ActionMapping ActionForm HttpServletRequest HttpServletResponse

Syntax of execute method public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response){} Four object is used in this method :

mapping:- This is object of ActionMapping . ActionMapping contains all deployment information for a particular action. ActionMapping contains a group of ActionForward associated with the current action. ActionMapping class used to determine where the results of action class will be send after competitions of presses. form :-This is object of ActionForm.This contains the parameters passed by input form. request:-This is object of current HttpServletRequest. response:-This is object of current HttpServletResponse. The return type of execute methods is ActionForword returned by Action call through which ActionServlet decided next View. This key is compared with values of tags name attributes to forwards the request to next View to client as response. We have also map configurations of action subclass into struts-config.xml. Below is example of Action Mappings Configuration Here success.jsp is JSP page where request is forwarded when action will successfully completed.

LoginAction.java public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response){ LoginForm loginForm=(LoginForm)form; if(loginForm.getName().equals("Deepak") &&loginForm.getPassword().equals("Singh")){ return mapping.findForward("success");

}else{ return mapping.findForward("error"); } } } Write a Login Forward Introduction: Here in this section we are going to create a JSP View where Action Servlet forward the request coming from Action class .This request is forwarded to client as a response. The Action Servelet decides JSP View on the behalf of key returned by Action class. Descriptions: For this create a JSP page (e.g. success.jsp ). Configure this JSP page in strutsconfig.xml in tag .e.g Here success (name=success) is key returned by subclass of appropriate Action class. The action class ma return any key. As per as key we can configure JSP. struts-config.xml <struts-config> <form-beans> <form-bean name="loginForm" type="com.r4r.struts.LoginForm"/> </form-beans> <global-exceptions /> <global-forwards> <forward name="login" path="/login.do"/> </global-forwards> <action-mappings> <action path="/login" type="com.r4r.struts.LoginAction" name="loginForm"> <forward name="success" path="/success.jsp"/> <forward name="error" path="/error.jsp"/> </action> </action-mappings> <message-resources parameter="com.r4r.struts.ApplicationResources"/> </struts-config>

Create web.xml and struts-config.xml Introduction: To make struts based application. We need minimum two XMLs that are web.xml and struts-config.xml .The web.xml is a deployment descriptor file which is most impartent file. For Struts based application we need one more file struts-config.xml file to configure actions of struts.

Descriptions: The web.xml file provides configuration and deployment information for the Web components that comprise a Web application. Web components are:

Servlet parameters, Servlet and JavaServer Pages (JSP) definitions, and Uniform Resource Locators (URL) mappings.

In deployment descriptor we can describe following deployment descriptor elements.


ServletContext init parameters Localized context session configuration Servlet/JSP definitions Servlet/JSP mappings Tag Library MIME type Welcome file list Error pages Security information

A Sample Example of web.xml is for Servlet 2.4 application : The Servlet 2.4 application r4r WelcomeServlet welcome /r4r.welcome r4r.welcome 404 /error404.ht ml .The web.xml file must palaces into WEB-INF directory.

web.xml <web-app> <servlet> <servlet-name>welcome</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/welcomeServlet</url-pattern> </servlet-mapping> </web-app> In struts based application we have to configure

The Struts ActionServlet The struts tag libraries files The welcome files The error pages.

and action org.apache.struts.action.ActionServlet config /WEB-INF/strutsconfig.xml validating true 1 action *.do index.jsp /taglibs/struts-tiles /WEBINF/taglibs/struts-tiles.tld 404 /error404.html. The most important configuration in web.xml is configuring struts-config.xml. We must have configured struts-config.xml (using the init-param tag) in web.xml with in ActionServlet configuration. e.g. action org.apache.struts.action.ActionServlet config /WEB-INF/strutsconfig.xml .. Here we are mapping action servlet using two different url patterns..

This will receives all requests coming from URL Pattern .do extension .This request is directed to ActionServlet action *.do e.g. http://localhost:8080/r4r/home .do this is

directed to ActionServlet and ActionServlet Lookup home (URI )into strutscongif.xml called appropriate action.

This will receives all requests coming from URL Pattern /pages/*.This request is directed to ActionServlet action /pages/* Note: These two ways are just only you can see URL pattern in browser and using this URL pattern you can request to server.

When request is directed to ActionServlet then working will be same in both cases. struts-config.xml is most important file in struts based application .We can use more than one struts-config.xml file into struts based application. The struts-config.xml can be placed in WEB-INF folder.In struts-config.xml file we can configure ActionForm, Action ,DynaActionForm etc. All struts based components are configured here.

web.xml <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>

</web-app> struts-config.xml <struts-config> <form-beans> <form-bean name="loginForm" type="com.r4r.struts.LoginForm"/> </form-beans> <global-exceptions /> <global-forwards> <forward name="login" path="/login.do"/> </global-forwards> <action-mappings> <action path="/login" type="com.r4r.struts.LoginAction" name="loginForm"> <forward name="success" path="/success.jsp"/> <forward name="error" path="/error.jsp"/> </action> </action-mappings> <message-resources parameter="com.r4r.struts.ApplicationResources"/> </struts-config>

You might also like