Wednesday, July 2, 2008

Configuring Struts 2

Struts2 configuration can be done in 2 ways:-
  1. Configuring web.xml, struts.xml, struts. properties, struts-default.xml and strtus-plugin.xml.
  2. Zero configuration using Annotations. Only Web.xml is required to specify, FilterDispactcher and action packages.

In this blog I am posting a sample code to understand the 1st way of configuration, than after this will take you to other advance configuration.
Web.xml :- Struts 2 uses FilterDispactcher which is a servlet class that initializes struts framework and handles all the request.
<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>

Struts.xml:- This file is a default file for the struts 2 framework also known as the core configuration file for this framework. This file include configuration for <result-type>, <interceptors>, <interceptor-stack>, <global-results> and <action>. Explaining these in depth will require me to write one more blog only on struts.xml; here I am listing a sample struts.xml file which is usually used in configuring struts 2 application.

<?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=”struts-defualt.xml” />
<package name="default" extends = “struts-default”>
<action name="displayAction" class="com.struts2.ashu.DisplayAction">
<result name="success" >/success.jsp</result>
</action>
</package>
</struts>

Struts-default.xml:- This file may be included at the top of struts.xml file to include the standard configuration setting without having to copy them. The struts-default.xml file contains default configuration for the framework results, interceptors and interceptors stacks. The package defined in our sturts.xml file can extend <package> defined in struts-default.xml file. This makes all the default results, interceptors and interceptors stacks available to be used in our struts.xml file.
Struts. properties:- This file is used to override the default values set for different keys in default. properties file. This setting of keys help in customizing struts application.
For example :- To load application resources file for setting the values for internationalization/ property / label name on jsp is loaded in struts 2 using struts.properties.
// sample property file.
### Resources for parameter “com.struts2.ashu.ApplicationResources”
App.title=struts 2 Application
App.title=personal Information


##### stuts.properties will have an entry to load this file. The file will contain only a single line of code.
struts.custom.i18n.resources=com.struts2.ashu.ApplicationResources.

struts.properties files is used to customize/ override the default behavior of struts 2 application, this default behavior is present in default.properties file. This file contains number of other parameters to configure or override the default behavior in struts.properties.

No comments: