You are on page 1of 8


Egen Solutions Inc.

Egen Solutions Inc.

MVC (Model View Controller) Design Pattern



View Model

(JSPs) (Java Beans)

Model – Contains the business logic and interact with persistent logic to store, retrieve and manipulate the data
into the database.
View – Responsible for displaying the results back to the user. Typically view layer is developed using the UI
technologies such as JSPs, Faces and Velocity etc…
Controller – Acts as mediator between Model and View and handles all the requests from user and selects
appropriate view to return.
Struts Architecture
Framework, designed based on the MVC design pattern. It comes with couple of components listed below
o ActionServlet – Acts as controller in MVC pattern and follows a Front Controller design paatern.
o Java classes – Class library to develop Action classes, Form beans, and etc…
o Tag library – Tag library to develop the views (JSPs) to implement the UI components
o Validation framework – In build validation framework to perform the common and business validation.

Egen Solutions Inc.

Struts-config.xml Action Form

User Action Model

Browser Servlet Beans
Request Execute


Response (JSP)

Struts flow
o When user makes a request through browser, the request will comes to the ActionServlet configured in the
o ActionServlet was already instantiated and loaded the struts-config.xml file where you configured the action
mappings and action forwards.
o Populates the ActionForm with all the request values.
o struts-config.xml file decides which action class to be executed and validates the data entered by the user.
o Action class interacts with Java beans to manipulate the data into the database (Action class -> business layer ->
persistent layer -> database).
o struts-config.xml file will find out the view (jsp page) to where the request should be forwarded.
o Appropriate JSP page will be sent back to the browser as a response that contains the model information to be
displayed to the user.

Egen Solutions Inc.

web.xml file struts-config.xml
<web-app> <struts-config>
<servlet> <form-beans>
<servlet-name>struts</servlet-name> <form-bean name=”myFormName” type=”com.egen.form.MyForm” />
<servlet-class> </form-beans>
</servlet-class> <global-forwards>
<init-param> <forward name=”someName” path=”/” />
<param-name>config</param-name> </global-forwards>
</init-param> <action-mappings>
<load-on-startup>1</load-on-startup> <action path=”/samplePath” type=”com.egen.action.MyAction”
</servlet> name=”myFormName”>
<servlet-mapping> <forward name”success” path=”/success.jsp” />
<servlet-name></servlet-name> </action>
<url-pattern></url-pattern> </action-mappings>
</servlet-mapping> </struts-config>

Action class Action Form

public class MyAction extends Action { public class MyForm extends ActionForm {
public ActionForward execute (ActionMapping mapping, private String name;
ActionForm form, HttpServletRequest request, public String getName() {
HttpServletResponse response) throws Exception { return;
MyForm myForm = (MyForm)form; }
myForm.setName(“Praveen Samala”); public void setName(String name) { = name;
return mapping.findForward(“success”); }
} }

Egen Solutions Inc.

o Data Supplier – It supplies the data to html:form, using which you can display the data by JSP page to the user.
o Data Collector – It processes data from html:form i.e. it retrieves the form data submitted by user through html.
o Action Firewall – validates the data before the action sees it if you specify the validate attribute as true.
It will be used to configure the data in struts configuration file instead of using the ActionForm.
<form-bean name=”myDynaActionForm” type=”org.apache.struts.action.DynaActionForm”>
<form-property name=”firstName” type=”java.lang.String” />
<form-property name=”lastName” type=”java.lang.String” />

Note: The beauty about the DynaActionForm is you can create the DTO in one single statement like below.
YourDTO obj = new YourDTO();
BeanUtils.copyProperties(obj, dynaActionForm);

Validation Framework
Struts provide the plug-in to perform the validation on your application includes both business and data related
validation. Below 2 files are the validation framework configuration files.
o validator-rules.xml - Define the rules to be applied.
o Validation.xml – Apply the rules defined in the validator-rules.xml

Egen Solutions Inc.

Steps to configure the validation framework
1. Add the validator plug-in to the struts configuration file.
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" />
2. Copy the validator-rules.xml and validation.xml files into WEB-INF folder.
3. Change the ActionForm class to subclass the ValidatorForm
4. Add a form definition to the <formset> in the validation.xml file.
<form name="yourForm">
5. Add a field to the form declaration in the validation.xml file corresponding to the property and specify the list of
rules to associate with this property by comma-delimiter.
<form name="yourForm">
<field property="propertyName" depends="required, minlength, maxlength">
6. Add the error messages for the rules to the resource bundle.
errors.required={0} is required.
errors.maxlength={0} cannot be greater than {1} characters.
errors.minlength={0} cannot be less than {1} characters.

Egen Solutions Inc.

7. Specify the arguments to the error message.
<form name="user">
<field property="userName" depends="required,minlength,maxlength,match">
<arg0 key="userRegistration.userName" />

Creating your own rules

1. Write a java class
2. Add a static method with below signature to the above class.
public static boolean methodName(Object bean, ValidatorAction va, Field field, ActionErrors errors,
HttpServletRequest equest) { }
3. Define the rule in the validator-rules.xml
<validator name="myName" classname="com.egen.YourClass" method="methodName"
methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest"
Forward Action – To forward the request to a different resource. (Same as RequestDispatcher.forward())
<html:link action="home">Home</html:link>
<action path="/home" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp" /> OR
<action path="/home" forward="/index.jsp" />

Egen Solutions Inc.

Include Action – To include the other resource into your action (Same as RequestDispatcher.include())
<action path="/someAction" type="org.apache.struts.actions.IncludeAction" parameter="/some/resource"
input="/form/input.jsp" name="myForm" validate="true" scope="request" />

Dispatch Action – To group the actions into one single class.

public class MyDispatchAction extends DispatchAction {
method1() { }
method2() { }

<action path="/myDispatchAction" type="com.egen.MyDispatchAction" name="myForm"

attribute="user" parameter="action" input="/some.jsp">
LookupDispatchAction – Reverse lookup against the resource bundle with the label of a button. You need to implement
the following method to map the methods to resource keys.
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("formName.removeButton", "remove");
map.put("formName.saveButton", "save");
return map;
Switch Action – Supports switching from module to module.

Egen Solutions Inc.