Struts 2 Namespace is a new concept to handle the multiple modules by given a namespace to each module. In addition, it can used to avoid conflicts between same action names located at different modules.
See this picture to understand how a URL match to Struts 2 action namespace.
1. Namespace configuration-
Let go through a Struts 2 namescape configuration example to know how it match with URL and folder.
The package “login” will not affect the result, just give a meaningful name.
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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="myapp" /> <package name="default" extends="struts-default" namespace="/"> <action name="welcome" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package> <package name="login" extends="struts-default" namespace="/login"> <action name="welcome" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package> <package name="welocome" extends="struts-default" namespace="/welcome"> <action name="welcome" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package> </struts>
2. JSP View Pages
3 JSP view pages with same file name but locate at different modules.
1. View Root namespace
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>Welcome</title> </head> <body> <h2>Default namespace</h2> <h2>Hello Welcome , <s:property value="username" />...! Dineshonjava.com</h2> </body> </html>
URL : http://localhost:8080/doj/welcome.action
Will match the root namespace.
<package name="default" extends="struts-default" namespace="/"> <action name="welcome" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package>
2. View login namespace
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>Welcome</title> </head> <body> <h2>login namespace</h2> <h2>Hello Welcome , <s:property value="username" />...! Dineshonjava.com</h2> </body> </html>
URL : http://localhost:8080/doj/login/welcome.action
Will match the login namespace.
<package name="login" extends="struts-default" namespace="/login"> <action name="login" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package>
3. View welcome namespace-
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>Welcome</title> </head> <body> <h2>welcome namespace</h2> <h2>Hello Welcome , <s:property value="username" />...! Dineshonjava.com</h2> </body> </html>
URL : http://localhost:8080/doj/welcome/welcome.action
Will match the welcome namespace.
<package name="login" extends="struts-default" namespace="/welcome"> <action name="welcome" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package>
Reference