Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to use namespace in struts to separate web application modules

This article will help our readers to understand what is namespace in struts, why do we use namespace and how do we define namespace in struts

In Struts framework, namespace is a concept to separate our web application modules by giving them a unique name in the url. While a user hits a request to struts web application, a url is submitted by the web browser. This url is splitted into hostname, port name, servlet context name, namespace (if any) and the struts action (that is to be performed on request).

Ex. : localhost:8080/strutsapp/accountModule/getsalary.action

Namespace in Detail:

The namespace is the way to separate our web application modules in struts and to access these modules using their names in the url in struts framework. It is defined in the <package> tag of the struts configuration file. It is declared using the <package> tag attribute “namespace” and its value assigned. The default value to the namespace is blank.

namespace = “” or namespace = “/”
Syntax: <package namespace = “namespace-name”>

Namespace is very usefule in large web application that need to seperated with their sub-divisions. Here we go with a publication house web application example that has four sub-divisions :

  • Dashboard
  • Journals
  • Admin

As described earlier, namespace is defined inside the tag using namespace attribute. To define these four sub-divisions, four tag will be defined with their namespace specification as follows:

<package namespace = “/”>..</package>
<package namespace = “Journals”>..</package>
<package namespace = “admin”>..</package>

The above packages have three namespace with specific values and one is defined as default value. The default namespace is decided to be assigned to dashboard module and the other namespaces will represent the respective modules.

listing servlet context, namespace and method name

Figure 1: listing servlet context, namespace and method name

Above figure show ServletContext, namespace and method name passing into url. The method name may or may not be passed with .action extention.

Listing 1: 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>
	<include file="journals.xml"	/>
	<include file="admin.xml" 		/>
	<package name="default" extends="struts-default">
		<default-action-ref name="index" />
		<action name="index" class = "com.ajo.actions.Dashboard" method = "getDashboard">
			<result name = "success">
                /jsp/dashboard/index.jsp
            </result>
        </action>
	</package>
</struts>

The above table includes three other xml file files for modules journals, order and admin. There is one default package was defined that is loaded by default, while this web application is visited. This loads dashboard module. The first tab <?xml> defines the xml document version and encoding type. The next tag <!DOCTYPE...> defines the document type that DTD information about the struts configuration. The maint root tag of this document is <struts> tag that include 3 other xml configuration files and defines a default package. The <include> tag is used to include other xml configuration file and <package> tag is used to define default package that is to be loaded while web application loads. It loads the dashboard module. It ask to Dashboard class to invoke method getDashboard(). If this method returns “success” then index.jsp file of dashboard module will loaded.

To invoke getDashboard() method of class com.ajo.actions.Dashboard class, <action> tag is defined inside <package> tag. This tag contains a sub-tag <result> that has its name as success. getDashboard() method returns a string that should match <result> name defined inside the <action> tag.

Listing 2: admin.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="admins" namespace = "admins" extends="struts-default">
		<action name="admin" class = "com.ajo.actions.Admin" method = "admin">
            <result name = "success">
                /jsp/admin/index.jsp
            </result>
        </action>
	</package>
</struts>

The above table defines the package configuration for admin module of the web application. The <package> sub-tag of <struts> tag defines package name as “admins” , the namespace as “admins” and extends “struts-default”. The Namespace separates the url with the “admins” name invoke admins module.

Ex: localhost:8080/ajoapp/admins/admin.action

Listing 3: Journals.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="journals" namespace = "/journals" extends="struts-default">
		<action name="journals" class = "com.ajo.actions.Journals" method = "journals">
            <result name = "journals">
                /jsp/journals/index.jsp
            </result>
        </action>
	</package>
</struts>

The above table defines the package configuration for journal module of the web application. The sub-tag defines the namespace as “journals”.It separates the url with the “journals” to load module journals module.

Listing 4: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

The above table defines the deployment descriptor for the web application. This filter all the requests and move them struts.xml file that identifies the package specific request and loads the resulting jsp file from the invoking module. The first tag <?xml> define the xml file version and encoding type. The next tag <web-app> is the root tag of this file, whose sub-tag <filter> defines a filter for struts configuration file for all the coming request url.

Listing 5: Dashboard.java

package com.ajo.actions;

public class Dashboard {
	public String getDashboard(){
		return "success";
	}
}

The above table defines a Dashboard action class that handles all the coming request for this module. According to submitted url, a specific data should be returned by invoking method, which is getDashboard(). It will load index.jsp page of dashboard module, while return success.

Listing 6: Admin.java

package com.ajo.actions;

public class Admin{
	public String admin(){
		return "success";
	}
}

The above table defines an Admin action class to handle all the coming requests for the admin module. The invoking method admin() decides, which jsp view is to be loaded as response for the requested url that will loads index.jsp page of admin module, while return success.

Listing 7: Journals.java

package com.ajo.actions;

public class Journals {
	public String journals(){
		return "journals";
	}
}

The above table defines a Journal action class to handle all coming requests for Journals module. The invodking method journals() decides, which jsp view is to be loaded as response for the requested url that will load index.jsp page of journals module, while return success.

Listing 8: jsp/journals/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Journals</title>
</head>
<body>
	Journals Index.
</body>
</html>

The above table creates default index view for Journal module of the application and message as “Journals Index”.

Listing 9: jsp/dashboard/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dashboard Panel</title>
</head>
<body>
	This is dashboard panel index page.
</body>
</html>

The above table creates a index view for dashboard module and displays a message as “This is dashboard panel index page”.

Listing 10: jsp/admin/index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Admin Panel</title>
</head>
<body>
	This is admin panel
</body>
</html>

The above table creates a index view for admin module of the application and displays a message as “This is admin panel”.

journal namespace loads index page of journal module.

Figure 2: journal namespace loads index page of journal module.

The above figure loads index page of journal module, while namespace “journals” is passed into the url after servlet context before invoking method journal() of Journals action class.

Admins namespace loads index page of admin module.

Figure 3: Admins namespace loads index page of admin module.

The above figure loads index page of admin module, while namespace “admins” is passed into the url after servlet and admin() method is invoked of Admin action class.

Dashboard module is loaded bi-default

Figure 4: Dashboard module is loaded bi-default

A default module (dashboard module) of the web application is loaded, while no namespace is specified.

Project Directory Structure

Figure 5: Project Directory Structure

The above figure shows the directory structure for struts web application using namespace.

Required Jar Files

Figure 6: Required Jar Files

The above figure shows a set of required jar files to run the above example.

Conclusion:

In this article, we learned about:

  • What is namespace in struts?
  • Why do we use namespace?
  • How do we define namespace in struts?


I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login