Struts 2 Configuration File

This chapter will take you through basic configuration required for a Struts 2 application. Here we will see what will be configured in few important configuration files : web.xml, struts.xml, struts-config.xml and struts.properties

The struts application contains two main configuration files struts.xml file and struts.properties file.
The struts.properties file is used to override the default values of default.xml file provided by struts framework. So it is not mandatory. Mostly, you will not use struts.properties file.

The web.xml file:
The web.xml configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. It is not strictly a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.

As discussed earlier, this file provides an entry point for any web application. The entry point of Struts2 application will be a filter defined in deployment descriptor (web.xml). Hence we will define an entry of FilterDispatcher class in web.xml. The web.xml file needs to be created under the folder WebRoot/WEB-INF.

This is the first configuration file you will need to configure if you are starting without the aid of a template or tool that generates it (such as Eclipse or Maven2). Following is the content of web.xml file which we used in our last example.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2MyFirstApp</display-name>
  <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>
    <welcome-file-list>
        <welcome-file>Login.jsp</welcome-file>
    </welcome-file-list>
</web-app>
The struts.xml file:
Here, we are going to learn all about struts.xml file. First of all let us see the simple example of struts.xml file. Let us have a look at the struts.xml file we created in the Hello World example explained in previous chapter.

The struts.xml file contains the configuration information that you will be modifying as actions are developed. This file can be used to override default settings for an application, for example struts.devMode = false and other settings which are defined in property file. This file can be created under the folder WEB-INF/classes.


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="login" class="com.dineshonjava.struts2.login.LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>
        </action>
    </package>
</struts>
The first thing to note is the DOCTYPE. All struts configuration file need to have the correct doctype as shown in our little example. <struts> is the root tag element, under which we declare different packages using <package> tags. 
Multi Configuration File-
Here <package> allows separation and modularization of the configuration. This is very useful when you have a large project and project is divided into different modules.

<?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="my-struts1.xml"/>
     <include file="my-struts2.xml"/>
</struts>
Struts 2 Multiple Namespace Example-
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.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts 
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="default1" namespace="/" extends="struts-default">
<action name="hello"  class="com.dineshonjava.struts2.login.LoginAction">
<result>welcome.jsp</result>
</action>
</package>

<package name="default2" namespace="/first" extends="struts-default">
<action name="hello"  class="com.dineshonjava.struts2.login.LoginAction">
<result>welcome.jsp</result>
</action>
</package>

<package name="default3" namespace="/second" extends="struts-default">
<action name="hello"  class="com.dineshonjava.struts2.login.LoginAction">
<result>welcome.jsp</result>
</action>
</package>

</struts>    
1) package element-
We can easily divide our struts application into sub modules. The package element specifies a module. You can have or or more packages in the struts.xml file. Say, if your project has three domains - business_applicaiton, customer_application and staff_application, you could create three packages and store associated actions in the appropriate package. 


Attributes of package elements:
  • name name is must for defining any package.
  • namespace It is an optional attribute of package. If namespace is not present, / is assumed as the default namespace. In such case, to invoke the action class, you need this URI:
    /actionName.action
    
    If you specify any namespace, you need this URI:
    /namespacename/actionName.action
    
  • extends The package element mostly extends the struts-default package where interceptors and result types are defined. If you extend struts-default, all the actions of this package can use the interceptors and result-types defined in the struts-default.xml file.
2) action element:
The action is the sub-element of package and represents an action.
Attributes of action element-
  • name name is must for defining any action.
  • class class is the optional attribute of action. If you omit the class attribute, ActionSupport will be considered as the default action. A simple action may be as:
    <action name="login">
    
  • method It is an optional attribute. If you don't specify method attribute, execute method will be considered as the method of action class. So this code:
    <action name="login" class="com.dineshonjava.struts2.login.LoginAction">
    
    will be same as:
    <action name="login" class="com.dineshonjava.struts2.login.LoginAction" method="execute">
    
    If you want to invoke a particular method of the action, you need to use method attribute.
3) result element:
It is the sub element of action that specifies where to forward the request for this action.
Attributes of result element-
  • name is the optional attribute. If you omit the name attribute, success is assumed as the default result name.
  • type is the optional attribute. If you omit the type attribute, dispatcher is assumed as the default result type.



<<Previous <<   || Index ||   >>Next >>





No comments:

Post a Comment