I will show you here how to create multiple namespaces in struts2 web application. The benefit of namespace is the same file and action can be mapped to the multiple modules.

First create the web application using any JEE based IDE like Eclipse.
Make sure that the following jars exist in the WB-INF/lib directory. Please download the following jar files from the respective site.

file upload example in struts
We can define multiple namespaces in struts.xml file by the namespace attribute of package element. The default namespace is /(root).

Look at the below picture how namespace in the URL is mapped.

namespace example in struts

1. Creating Action class

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author admin
 *
 */
public class HomeAction extends ActionSupport {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

 

2. Namespace Configurations

Namespace is configured in the struts.xml file. The below file shows there are three namespaces – “/” or root, common and user.

<?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">
        <action name="home" class="in.sblog.actions.HomeAction">
            <result>jsp/index.jsp</result>
        </action>
    </package>

    <package name="common" namespace="/common" extends="struts-default">
        <action name="home" class="in.sblog.actions.HomeAction">
            <result>jsp/index.jsp</result>
        </action>
    </package>

    <package name="user" namespace="/user" extends="struts-default">
        <action name="home" class="in.sblog.actions.HomeAction">
            <result>jsp/index.jsp</result>
        </action>
    </package>
</struts>

 

namespace example in struts
3. JSP Pages

index.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Multiple Namespaces</title>
</head>
<body>
    <a href="/jcms/home">root or "/" namespace</a>|
    <a href="/jcms/common/home">common namespace</a>|
    <a href="/jcms/user/home">user namespace</a>
</body>
</html>

 

Below JSP pages with same file name but locate at different modules.

jsp/index.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2 multiple Namespace</title>
</head>
<body>
    <h1>Namespace - "/"</h1>
</body>
</html>

 

common/jsp/index.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2 multiple Namespace</title>
</head>
<body>
    <h1>Namespace - "common"</h1>
</body>
</html>

 

user/jsp/index.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2 multiple Namespace</title>
</head>
<body>
    <h1>Namespace - "user"</h1>
</body>
</html>

 

4. Mapping

http://localhost:8080/struts2multiplenamespace/home

<package name="default" namespace="/" extends="struts-default">
    <action name="home" class="in.sblog.actions.HomeAction">
        <result>jsp/index.jsp</result>
    </action>
</package>

 

http://localhost:8080/struts2multiplenamespace/common/home

<package name="common" namespace="/common" extends="struts-default">
    <action name="home" class="in.sblog.actions.HomeAction">
        <result>jsp/index.jsp</result>
    </action>
</package>

 

http://localhost:8080/struts2multiplenamespace/user/home

<package name="user" namespace="/user" extends="struts-default">
    <action name="home" class="in.sblog.actions.HomeAction">
        <result>jsp/index.jsp</result>
    </action>
</package>

 

Once you run the application you will see the following page. Now you can navigae to any of the namespaces.
namespace example in struts

Thanks for your patience. Please do not forget to leave a comment.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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