Model-1 Architecture
In this Architecture, all kinds of requirements get fulfilled in one layer. The user provides the request to the JSP and JSP provides a response to the user. This architecture uses a set of JSP to implement presentation logic, business logic, controlling logic. It is suitable for small scale requirement due to its simplicity. It does not provide reusability.
Model-2 Architecture
This layer is divided into three parts they are as below
-
Model
-
Controller
-
View
- Model
This layer implements business logic and contains data. State of users can be stored in this layer. This layer can be created by using a set of java beans.
- Controller
This layer implements controlling logic. It takes decision to make visible appropriate view to the appropriate users. This layer can be created by using a servlet and a set of helper classes. The users give request to the servlet present in the layer.
- View
This layer implements the presentation logic of the application. It provides a response to the user. A set of JSP can be used to create this layer.
Introduction to Struts
This is a framework to implement MVC Architecture in a java based web application. It uses three different layers to implements different types of logic. It provides a set of classes and tag libraries for being used in different layers of the application.
MVC Architecture of struts
1. Model: - This layer implements business logic and contains data. This can be created by using a set of java beans called a form bean. Struts provide a predefined class as the super class of all form beans.
2. Controller: - This layer implements the controlling logic of the application. It takes the decision to make visible appropriate view to appropriate users. This can be created by using a controlling servlet and a set of helper class called form- action. Struts provide a predefined servlet as the controlling servlet. The controlling servlet receives a request from the users. Struts provide a predefined class as the super class of all form action classes.
3. View: - This layer implements presentation logic. It provides a response to the users. This layer can be created by using a set of JSP. Struts provide tag libraries for this layer.
Installation of struts
-
-
Search for all .tld files in the extracted folder and makes those files available in WEB-INF folder of the context.
-
Search for all .jar files in the extracted folder and make those files available in lib folder of the context.
Configuration of struts
Create a configuration file named as struts-config.xml inside WEB-INF folder. This file contains information about form-bean, form-action, and forwards.
Tips: -
- Search for struts-config.xml inside the extracted folder of struts. Copy that file to make available in WEB-INF folder of the context. Open this file and remove all the tag present between <struts-config></struts-config>
-
Deploy the controlling servlet by using the required in WEB-INF. Name of the controlling servlet is org.apache.struts.action.Actionservlet. This servlet must accept the name of the configuration file by using an initial parameter named as config. This servlet must be a permanent servlet. This servlet must have an url-pattern to receive all types of request.
- <servlet>
- <servlet-name>x</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>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>x</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
Creating a form-based application in struts
Requirements
-
Create the JSP to contain the form.
-
Create a form-bean to contain values of form fields.
-
Create a form-action to validate the input of the user and to provide appropriate forwards names to controlling servlets.
-
Provide information in struts-config.xml.
-
Create a page for each forwards.
Creation of form bean
-
Create a class in a package by inheriting org.apache.struts.action.ActionForm.
-
Provide variables in the form-bean class as the properties. Name of these variables must match with the value of property attributes present in form field tags.
-
Provide setter and getter methods for each of the property.
-
Optionally override reset () method to provide default values to the properties.
Creation of form action
-
Create a class in the same package where form-bean exists and this class must inherit org.apache.struts.action.Action class.
-
Override execute () method to implement validation logic and to return a forward. The return type of this method must be org.apache.struts.ActionForward.This method accepts four parameters as follows below.
a. org.apache.struts.action.Actionmappings
This represents all forward name and present in struts-config.xml and helps to create object of actionforward.
b. org.apache.struts.action.Actionform
This represent form bean.
c. javax.servlet.http.HttpServletRequest
This represent user request.
d. javax.servlet.http.HttpServletResponse
This represent service response.
Compilation of bean and action
Compile formbean and action file simultaneously by using servlet-api.jar and struts-core-1.3.8.jar in classpath. The jar file of struts(struts-core-1.3.8.jar) can be found in lib folder of the context, after installation of structs.
--- \classes\logs\javac - classpath servlet-api.jar ;struts-core-1.3.8.jar *.java
Here logs is the package folder where we stored both the form bean and form action files.
Providing information in struts-config.xml file
-
provide information about formbean by using the following tags in struts-config.xml as
- <form-beans>
- <form-bean name="name to map formbean with form action" type="class name with package name of the form bean" />
- </form-beans>
-
Supply information about form-action and forwards by using the following tags in struts-config.xml file
- <action-mappings>
- <action name="mapping name specified in formbean" path="value of action attribute present in from" type="class name of form action" >
- <forward name="name of forward used by form action" path="name of the page to be visible" />
- <forward name=" name of forward used by form action " path=" name of the page to be visible " />
- </action>
- </action-mappings>
In the next section, we will do a login page using struts framework... Stay tuned...