Architecture of Struts 1 MVC Framework

18
Architecture of Struts 1 MVC Framework In this post we will see the architecture of Struts 1 or work flow of popular mvc framework that is Struts 1. As we know struts 1 is a mvc (Model-View- Controller) based framework which reduces the work of developers to develop a web application. So, one question will come in your mind i.e. What are the elements in Struts 1 that serves as Model-View-Controller rolls ? Here is the answer : Model : Models in Struts 1 framework are the java bean classes that extends ActionForm class provided in Struts jars. Model classes holds the state of the internal and external system. View : Views are web components that are shown to the web browser users. In Struts 1, it can be jsp page or any view render technique such as Tiles. Controller : Controllers are the components that handles are the requests that comes from users and decides which view is to send back to the use as response. In Struts 1 the controller part is handled by ActionServlet class and a subclass of Action class that is developer defined. The controller may interact with the application specific business logic or some code that persists data in database or with an EJB. The bellow figure shows the work flow of Struts 1 : Architecture of Struts 1

description

MVC

Transcript of Architecture of Struts 1 MVC Framework

Architecture of Struts 1 MVC FrameworkIn this post we will see the architecture of Struts 1 or work flow of popular mvc framework that is Struts 1. As we know struts 1 is a mvc (Model-View-Controller) based framework which reduces the work of developers to develop a web application. So, one question will come in your mind i.e.What are the elements in Struts 1 that serves as Model-View-Controller rolls ?Here is the answer :Model: Models in Struts 1 framework are the java bean classes that extends ActionForm class provided in Struts jars. Model classes holds the state of the internal and external system.View: Views are web components that are shown to the web browser users. In Struts 1, it can be jsp page or any view render technique such as Tiles.Controller: Controllers are the components that handles are the requests that comes from users and decides which view is to send back to the use as response. In Struts 1 the controller part is handled by ActionServlet class and a subclass of Action class that is developer defined. The controller may interact with the application specific business logic or some code that persists data in database or with an EJB.The bellow figure shows the work flow of Struts 1 :

Architecture of Struts 1The following steps shows the work flow of Strust 1 framework :1. When first time user request comes from the browser, ActionServlet invoked which reads struts-config.xml configuration file and creates configuration objects. struts-config.xml contains information about the FormBeans, that are subclasses of ActionForm, Actions and view that can be send as response to the browser.2. In the second step ActionServlet instantiate the specified form bean and puts the value of the form attributes, submitted by the browser, to form bean.3. Then ActionServlet calls the Action classs execute method, which returns ActionForward instance. ActionForward instance contains the detail of the view to be return as response.4. ActionServlet renders the view and return to the browser as response.Hello World with Struts 1 in Eclipse IDEIn theprevious postwe saw the architecture of Struts 1. Now this post will take you through the first illustrative program made up with the help of Struts 1 framework to displayHello Worldin browser in Eclipse IDE. I have used Eclipse 3.7 and Tomcat 6 for developing the example set.First of all click onNew Projectand theDynamic Web Project.

Select Dynamic Web ProjectThen Eclipse will ask you for name of the project. Enter the name of the project and click on finish.

Provide Project NameThis will create a web project in Eclipse which can be exported as war file and run on Tomcat 6 server.The next step will be to add struts jar files. The following jar files should be added to the project for successful deployment of struts project : antlr-2.7.2.jar commons-beanutils-1.8.0.jar commons-chain-1.2.jar commons-digester-1.8.jar commons-logging-1.0.4.jar commons-validator-1.3.1.jar oro-2.0.8.jar struts-core-1.3.10.jar struts-taglib-1.3.10.jarThese jar files can be downloaded from thelinkwith the Struts 1.3 distribution.You can put these jar files to the directory underWebContent->WEB-INF->lib.Now we have to configure ActionServlet of struts with web.xml. The following xml shows how to configure struts in web.xml.1

2

7HelloWordWithStruts1

8

9

10action

11org.apache.struts.action.ActionServlet

12

13config

14

15/WEB-INF/struts-config.xml

16

17

182

19

20

21

22action

23*.do

24

25

26

27index.jsp

28

29

We have registered ActionServlet class in web.xml for the url *.do , that means every url that ends with .do will be taken care by Struts. We have to also specify location ofstruts-config.xmlas init-param with name config. Struts reads the filestruts-config.xmlabout the configuration that we provide to the struts such as FormBeans, Actions, Plugins , Global Forwards etc.After that we have to create a form bean that will be sub class of ActionForm class which will bw act as model in out application and will contain application state. Following is our form bean named HelloWorldForm.1package com.raistudies.forms;

2

3import org.apache.struts.action.ActionForm;

4

5public class HelloWorldForm extends ActionForm {

6

7private static final long serialVersionUID = 7352021000623040587L;

8

9private String hello = null;

10

11public HelloWorldForm() {

12super();

13hello = "Hello World";

14}

15

16public String getHello() {

17return hello;

18}

19

20public void setHello(String hello) {

21this.hello = hello;

22}

23}

This form bean contains single property named as hello which stores Hello World as default value. Our next step will be to create a controller helper class that will help to handle a particulate url pattern. The url pattern will be specified and associated with our controller helper class in the file struts-config.xml. Our controller helper class must be a subclass of Action class provided in struts jar. We the url will be requested to the server, struts will run execute method of the associated with the url and returns the view in response. Following is our controller helper class named as HelloWorldAction :1package com.raistudies.actions;

2

3import javax.servlet.http.HttpServletRequest;

4import javax.servlet.http.HttpServletResponse;

5

6import org.apache.struts.action.Action;

7import org.apache.struts.action.ActionForm;

8import org.apache.struts.action.ActionForward;

9import org.apache.struts.action.ActionMapping;

10

11public class HelloWorldAction extends Action {

12

13@Override

14public ActionForward execute(ActionMapping mapping, ActionForm form,

15HttpServletRequest request, HttpServletResponse response)

16throws Exception {

17return mapping.findForward("hello");

18}

19

20}

The ActionForm object will contain instance of our form bean i.e. HelloWorldForm. The execute method returns string hello as the ActionForward. All the configuration regarding the action will be made as in the file struts-config.xml mentioned bellow.1

2

3

6

7

8

9

10

11

12

13

14

15

16

17

18

As you can see, we have registered our form bean com.raistudies.forms.HelloWorldForm with name HelloForm and put our controller helper class com.raistudies.actions.HelloWorldAction with the following attributes :1. name = HelloForm: It specifies the name of the form bean to be use as Model and in this case it is com.raistudies.forms.HelloWorldForm.2. path=/HelloWorld: It indicate the url associated to the controller helper class. In this case whene we will hit the url /HelloWorld.do , action will be invoked.3. scope=request: It indicates that the model or the form bean HelloForm will be stored in HttpServletRequest instance. the other option can be session. indicated a view as HelloWorld.jsp that is named as hello. That means if the execute method returns ActionForward with value hello, server will send HelloWorld.jsp as response.So the directory structure of the project will be like bellow figure :

Directory StructureWhen we will run the project in Tomcat 6 bellow output window will be the output :

Login Form Example With StrutsThis post will show you how a form process in struts 1 framework. We will create a login form and then using struts 1 we will verify the authentication of the user. This example will take following steps :1. First of all we will create a form bean (LoginForm.java) that will hold the form values provided by the user.2. Create a jsp page (Login.jsp) which will contain the form to be displayed to the user.3. Create a success page (Success.jsp) and failure page (Failure.jsp) for providing feedback to the user on their form submission.4. Create a controller helper class (LoginAction.java) that will check for the user input and decide which view to be respond to the users (Success.jsp or Failure.jsp).5. And finally we will configure our form bean and action classes in struts-config.xml.LoginForm.javaFollowing is the code in LoginForm.java file :1package com.raistudies.forms;

2

3import javax.servlet.http.HttpServletRequest;

4

5import org.apache.struts.action.ActionForm;

6import org.apache.struts.action.ActionMapping;

7

8public class LoginForm extends ActionForm {

9

10private static final long serialVersionUID = -3491637470205228033L;

11

12private String username = null;

13private String password = null;

14

15public String getUsername() {

16return username;

17}

18

19public void setUsername(String username) {

20this.username = username;

21}

22

23public String getPassword() {

24return password;

25}

26

27public void setPassword(String password) {

28this.password = password;

29}

30

31@Override

32public void reset(ActionMapping mapping, HttpServletRequest request) {

33this.password = null;

34}

35}

There are two fields in this form bean username and password, that will hold the value of two fields in the login form. One new goods here is the method reset which is overwritten by our form bean. reset method is called at the end of the every request processed by the struts. In reset method we have set the value of password as null which means every time the user will open the login jsp in browser it will show the last username but will not show the value of password.Login.jspBellow are the content of Login.jsp :1

3

4

5

6

7

8Login Form

9

10

11

12Username :

13Password :

14

15

16

17

This jsp will render a form with two fields, username and password. Struts HTML taglib has been used to create the form. will render as a html form with submit url as /Login.do, so our action must use this path in configuration to be run. will be render as html input tag and its value will be put in the username field of the form bean LoginForm. Same with the property password.LoginAction.javaLets look inside the code in LoginAction.java :1public class LoginAction extends Action {

2@Override

3public ActionForward execute(ActionMapping mapping, ActionForm form,

4HttpServletRequest request, HttpServletResponse response)

5throws Exception {

6LoginForm loginForm = (LoginForm)form;

7if(loginForm.getUsername() == null || loginForm.getPassword() == null ||

8!loginForm.getUsername().equalsIgnoreCase("rahul") || !loginForm.getPassword().equals("abc")){

9return mapping.findForward("failure");

10}

11else

12return mapping.findForward("success");

13}

14}

As you can see first of all the ActionForm instance is typecast to LoginForm in the execute method of LoginAction and then logic to verify the username and password will decide which view to be send back to the user. In this case, username must be rahul ans password must be abc to go to the view associated with the success, otherwise view associated with failure will be returned. We can see the jsp files associated with success and failure.struts-config.xmlEntries of form beans and actions aer done in struts-config.xml as follows :1

2

5

6

7

8

9

10

11

12

13

14

15

16

17

Our LoginForm class has been added as a form bean named loginForm and associated with the action class LoginAction. Attributes associated with the action class are specified as with the following reason :1. name=loginForm: Struts will instantiate the LoginForm class will set the value of form properties.2. path=/Login: The action class will be associated with the request path /Login.do. Hence the Login.jsp contains /Login as action attribute value.3. input=/Login.jsp: Form inputs will be taken from Login.jsp.There are two action forwards also registered with our action which tells struts that if failure is return by action then the response view will be /Failure.jsp and if success is returned by action then the response view will be /Success.jsp.Deploy the war file in Tomcat 6 and hit the url in your browser you will get the following login form :Now, put Rahul as username and abc as password you will be carried out to the Success.jsp page :

Login Successful FormNow, try with the username as scote and tiger. It will show you Failure.jsp page :

Login FailureIf you import the code provided in bellow link in Eclipse. The project directory will be like this :

Project Hierarchi in Eclipse

http://www.raistudies.com/struts-tutorials/