Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Multi Namespace


We can define multiple namespaces in struts.xml file by the namespace attribute of package element. As we know, default namespace is / (root).

Let’s see the simple example to define multiple namespaces in struts.xml file.

Define multiple namespaces in struts.xml

This struts.xml file contains three packages with different names and namespaces.

struts.xml

  1. <?xml version=“1.0” encoding=“UTF-8” ?>
  2. <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts 
  3. Configuration 2.1//EN” “http://struts.apache.org/dtds/struts-2.1.dtd”>
  4. <struts>
  5. <package name=“default1” namespace=“/” extends=“struts-default”>
  6. <action name=“hello”  class=“com.Kreationnext.Welcome”>
  7. <result>welcome.jsp</result>
  8. </action>
  9. </package>
  10. <package name=“default2” namespace=“/first” extends=“struts-default”>
  11. <action name=“hello”  class=“com.Kreationnext.Welcome”>
  12. <result>welcome.jsp</result>
  13. </action>
  14. </package>
  15. <package name=“default3” namespace=“/second” extends=“struts-default”>
  16. <action name=“hello”  class=“com.Kreationnext.Welcome”>
  17. <result>welcome.jsp</result>
  18. </action>
  19. </package>
  20. </struts>

Other required resources

We need some other required files to understand the full example of multiple namespaces.

  • index.jsp
  • action class (Welcome.java)
  • 3 view components

1) Create index.jsp

This jsp page provides three links.

index.jsp

  1. <a href=“hello”>root namespace</a>|
  2. <a href=“first/hello”>first namespace</a>|
  3. <a href=“second/hello”>second namespace</a>

2) Create the action class

It is the simple action class containing execute method only.

Welcome.java

  1. package com.Kreationnext;
  2. public class Welcome {
  3. public String execute(){
  4.     return “success”;
  5. }
  6. }

3) 3 view components

Here name of all the 3 view components are same but there location are different.

welcome.jsp

It must be located in the root directory.

  1. <h1>Welcome to root namespace</h1>

welcome.jsp

It must be located in the first directory under the root directory.

  1. <h1>Welcome to first namespace</h1>

welcome.jsp

It must be located in the second directory under the root directory.

  1. <h1>Welcome to second namespace</h1>

Comments

comments