Struts 2 params interceptor example


For an extensive application, it is smarter to utilize various arrangement document that one so it might be anything but difficult to deal with the application.

We can make numerous design documents however we have to characterize it in the struts.xml record. The includesub-component of struts is utilized to characterize the upheld arrangement file.

Example to make different arrangement files

Let's see the basic guide to characterize various arrangement documents.

1) Define passage of various arrangement documents in struts.xml

In this struts.xml record, we have characterized 2 arrangement documents struts first.xml document and struts second.xml file.

The struts first.xml record is situated inside the primary registry and struts second.xml document is situated inside the second directory.

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.   
  6. <package name="default1" namespace="/" extends="struts-default">  
  7. </package>  
  8.   
  9. <include file="first/struts first.xml"></include>  
  10. <include file="second/struts second.xml"></include>  
  11.   
  12. </struts>      

2) Create design files

Let's make two setup records that characterizes activity with results.

struts-first.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.   
  6. <package name="first" namespace="/first" extends="struts-default">  
  7.   
  8. <action name="hello"  class="com.Javacodegeeks.Welcome">  
  9. <result>welcome.jsp</result>  
  10. </action>  
  11. </package>  
  12.   
  13. </struts>  
struts-second.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="second" namespace="/second" extends="struts-default">  
  6. <action name="hello"  class="com.Javacodegeeks.Welcome">  
  7. <result>welcome.jsp</result>  
  8. </action>  
  9. </package>  
  10. </struts>  

Other Required Resources

To run this application, we need following resources:

  1. index.jsp
  2. Welcome.java
  3. 2 View Components

index.jsp

This jsp page makes two connections.

index.jsp
  1. <a href="first/hello">first namespace</a>|  
  2. <a href="second/hello">second namespace</a>  

Action class

It is the simple Action class containing only execute method.

Welcome.java
  1. package com.Javacodegeeks;  
  2.   
  3. public class Welcome {  
  4. public String execute(){  
  5.     return "success";  
  6. }  
  7. }  

2 view components

The name of 2 view components are same i.e. welcome.jsp but both are located differently.

welcome.jsp

It is located inside the first directory under root.

  1. <h1>Welcome to first namespace</h1>  
  2. </pre></div>  
  3.   
  4. <strong>welcome.jsp</strong>  
  5. <p>It is located inside the second directory under root.</p>  
  6. <div class="codeblock"><pre name="code" class="java" >  
  7. <h1>Welcome to second namespace</h1>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition