Pages Navigation Menu

Coding is much easier than you think

Struts 2- Include multiple Struts configuration files

Struts 2- Include multiple Struts configuration files

 

Struts 2 comes with €œinclude file-€ feature to include multiple Struts configuration files into a single unit.
 

Single Struts configuration file

 
Bad way of Struts 2 configuration example (From Previous Example).
 
File:struts.xml
 

<struts>
	
	<package name="default" namespace="/" extends="struts-default">
		<action name="default">
			<result>jsp/home.jsp</result>
		</action>
	</package>

	<package name="staff" namespace="/staff" extends="struts-default">
		<action name="Staff">
			<result>/jsp/staff.jsp</result>
		</action>
	</package>

	<package name="student" namespace="/student" extends="struts-default">
		<action name="Student">
			<result>/jsp/student.jsp</result>
		</action>
	</package>

</struts>

 
** UPDATE: Struts 2 Complete tutorial now available here.
 

In the above Struts configuration file, it groups all the -€œstudent€ and €œstaff-€ settings in a single file, which is not recommended and MUST BE AVOID. You should break this struts.xml file into smaller module related pieces. Multiple Struts configuration files In Struts 2, you should always assign each module a struts configuration file. In this case, you can create three files :
 

  1. student.xml -€“ Put all student module settings here.
  2. staff.xml -€“ Put all staff modules settings here.
  3. struts.xml -€“ Put default settings and include the staff.xml and student.xml.

 

File:student.xml – Put all student module settings here.
 

<struts>
	<package name="student" extends="struts-default">
		<action name="Student">
			<result>/jsp/student.jsp</result>
		</action>
	</package>
</struts>

 
File: Staff.xml – Put all staff modules settings here.
 

<struts>
	<package name="staff" extends="struts-default">
		<action name="Staff">
			<result>/jsp/staff.jsp</result>
		</action>
	</package>
</struts>

 
File: Struts.xml – Put default settings and include the staff.xml and student.xml
 

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	
	<package name="default" namespace="/" extends="struts-default">
		<action name="default">
			<result>jsp/home.jsp</result>
		</action>
	</package>

	<include file="student.xml"/>
	<include file="staff.xml"/>

</struts>

 

Final project structure

 
Multi
 
download
 

About Mohaideen Jamil