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 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.
<?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>
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.
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>
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>
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:
/actionName.action
If you specify any namespace, you need this URI:
/namespacename/actionName.action
The action is the sub-element of package and represents an action.
Attributes of action element-
<action name="login">
<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.
It is the sub element of action that specifies where to forward the request for this action.
Attributes of result element-
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…