Home » Struts » Introduction to Modules in Struts Application

Introduction to Modules in Struts Application

Struts Modules:

When you develop a web based application using struts,many developers like to put all Struts related stuff (action, form) into a single Struts configuration file for the entire application .For small application its not a problem.But if you are developing big application day by day your configuration file will become complicated.

Generally a large application will be divided into various modules.These modules will be developed by various teams and finally when you are integrating each module, you may feel it as complex task.

Apache has analysed all these problems and introduced Struts Modules which allows you to develop modularized struts application.Struts modules concept allows you to write Struts Configuration file for each module.

1. Single Module Example :

By having multiple struts configuration files it will be easy to maintain and debug. Here we work with a single module, so you can split the configuration file according to your convenience. During the startup the ActionServlet will read all the configuration files and create a database of configuration objects,which it will later refer while processing the request.

Let's see how we can achieve this.

index.jsp


<html>
<body>
<h1>This is index page using struts module concept</h1>
</body>
</html>

info.jsp


<html>
<body>
<h1>This is info page using struts module concept</h1>
</body>
</html>

Now create Struts configuration files for the application.Here we are going to create two configuration files.We can create any number of configuration files.


struts-config1.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action
path="/Page1"
type="org.apache.struts.actions.ForwardAction"
parameter="/index.jsp"/>
</action-mappings>
</struts-config>

struts-config2.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action
path="/Page2"
type="org.apache.struts.actions.ForwardAction"
parameter="/info.jsp"/>
</action-mappings>
</struts-config>

Now we need to map both the configuration files in web.xml separated by comma.

web.xml


<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Maven Struts Examples</display-name>
<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-1.xml, /WEB-INF/struts-config-2.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

Test it:

  1. http://localhost:8080/Module1Example/index.jsp
    It will display index.jsp
  2. http://localhost:8080/Module1Example/info.jsp
    It will display info.jsp

Multi Module Example: :

If you are working with multiple modules in your project, then you have to create one configuration file for each modules. Let's say the project has two modules Employee and Reports.Here Employee and Department are two different modules. The above code shows how to create a separate configuration file for each module.


Previous Next Article

comments powered by Disqus