Struts2 Multiple Namespace

We can handle multiple modules by given a namespace to each module in struts.xml file by the namespace attribute of package element. Default namespace is / (root).
Here is an example where we will define multiple namespaces in struts.xml file.

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.
This can be access via this link: http://localhost:8080/projectName/hello

<%@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>

Create first/nsexample.jsp file in web first folder.
This can be access via this link: http://localhost:8080/projectName/first/hello

<%@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 first namespace</h1>
    </body>
</html>

Create second/nsexample.jsp file in web second folder.
This can be access via this link: http://localhost:8080/projectName/second/hello

<%@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 second 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. Namespace configuration

struts.xml

<?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>

4. Note

Required jar files
Add the following jars to the project’s class path.

  1. commons-fileupload-1.3.1.jar
  2. commons-io-2.2.jar
  3. commons-lang3-3.2.jar
  4. commons-logging-1.1.3.jar
  5. freemarker-2.3.22.jar
  6. javassist-3.11.0.GA.jar
  7. ognl-3.0.6.jar
  8. struts2-core-2.3.24.1.jar
  9. xwork-core-2.3.24.1.jar

If you add “/” before nsexample.jsp in default2 and default3 packages like the following then all three namespace will display the nsexample.jsp page from root.

 <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>
 

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,425` Total Views

Leave a Reply

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


three − = 1