Struts2 Multiple Configuration File

In Struts 2 we can create multiple configuration xml file. The idea is to create multiple configuration file is for better management of the application.

We must have one struts.xml file. Then we can add several files in struts.xml in include sub-element of struts element.

1. JSP View Pages

index.jsp

  <a href="hello">root namespace</a>|
  <a href="first/hello">first namespace</a>|
  <a href="second/hello">second namespace</a>


Create nsexample.jsp file in web root folder.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Welcome to root namespace</h1>
    </body>
</html>

2. Deployment descriptor

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

3. Multiple configuration

Place struts.xml file in default package.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>        
    <package name="default" namespace="/" extends="struts-default"> 
    <result>/index.jsp</result>
</package>  
  
<include file="struts_another.xml"></include>
</struts>

Place struts_another.xml file in default package also.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>        
    <package name="default1" namespace="/" extends="struts-default">
        <action name="hello">
            <result>/nsexample.jsp</result>
        </action>
    </package>
    <package name="default2" namespace="/first" extends="struts-default">
        <action name="hello">
            <result>/nsexample.jsp</result>
        </action>
    </package>
    <package name="default3" namespace="/second" extends="struts-default">
        <action name="hello">
            <result>/nsexample.jsp</result>
        </action>
    </package>
</struts>

Now add required jar files and run your application.

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

2,398` Total Views

Leave a Reply

Your email address will not be published. Required fields are marked *


× nine = 36