May 15, 2021 Struts2
This section will take you through the basic configuration required for the Struts2 application. Here you can see which ones will be configured into some important profiles: web .xml, struts.xml, struts-config.xml, and struts.properties.
In fact, you can continue to rely on using web .xml and struts.xml profiles, and as you learned in the previous sections, our example works with these two files, but to let you know more, let's talk about other files.
As discussed earlier, this file provides access points for each web application. I
n the deployment descriptor (web .xml), the access point for the Struts2 application is defined as a filter. S
o we'.xml define an access point for the FilterDispatcher class in the web .xml file needs to be created under
the WebContent/WEB-INF
folder.
If you don't start with a template or tool (such as Eclipse or Maven2) to build, this is the first file you need to configure. Here's what we learned about the web .xml 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>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> </web-app>
Note that we map the Struts2 filter /* of /.action, which means that all urls are parsed by the Struts filter. This is discussed when we learn about annotations.
Note: Since version 2.1.3, ActionContextCleanUp and FilterDispatcher have been replaced by StrutsPrepare AndExecuteFilter.
<?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.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="cn.w3cschool.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> <-- more actions can be listed here --> </package> <-- more packages can be listed here --> </struts>
The first thing to note is DOCTYPE (document type). /b12> As our example shows, all Struts profiles need to have the correct doctype. /b14>The root tag element is the root tag element under which we declare different packages using the label. T he label here allows for the separation and modularization of the configuration. This is useful when you are working on a large project and the project is divided into several different modules.
If your project has three domains: business_applicaiton, customer_application, and staff_application, you can create three packages and store the associated Actions in the appropriate package. The label has the following properties:
Property | Describe |
---|---|
name (required) | A unique identity for package |
extends | Specifies that the package inherits all configurations for another package. Typically, we use struts-default as the basis for package. |
abstract | Define the package as abstract. If marked true, the package cannot be used by end users. |
namespace | Action's unique namespace |
The labels and name and value properties will be used to override any of the properties defined in default.properties, just as we set the struts.devMode property. Setting the struts.devMode property allows us to view more debug messages in the log file.
We define the label to correspond to each URL we want to access, and use the execute() method to define a class to be accessed when accessing the appropriate URL.
Results determines what is returned to the browser after the action is performed, and the string returned from the operation should be the name of the result. R esults are configured as above, or as a "global" result, for each operation in the package. Results has
nameAnd
typeThe property is optional, and the default name value is "success".
Struts.xml files can grow over time, so breaking them through packages is one way to modularize it, but struts provides another way to modular struts.xml files that you can split into multiple xml files and import them in the following ways.
<?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>
Another profile that we don't cover is the struts-default .xml. T his file contains the standard configuration settings for Struts, which you no longer have to repeat for 99.99% of projects. T herefore, we will not delve into the details of this document. If you are interested, you can view the default.properties file available .jar struts2-core-2.2.3.
The struts-config .xml is a link between the View and Model components in Web Client, but you don't have to use these settings in 99.99% of your projects. The struts-config .xml profile contains the following key elements:
Serial number | Interceptors and instructions |
---|---|
1 |
struts-config
This is the root node of the profile. |
2 |
form-beans
This is where you map the ActionForm sub-class to the name, which you can use as an alias for actionForm in the rest of the struts-config.xml file and even on the JSP page. |
3 |
global forwards
This section maps your page on the webapp to name, which you can use to refer to the actual page. This avoids hard coding the URL on your web page. |
4 |
action-mappings
This is where you declare the form handler, also known as action mapping. |
5 |
controller
This part is the interior of the configuration Struts and is rarely used in practice. |
6 |
plug-in
This section tells Struts where to find the property file, which contains prompts and error messages. |
Here's an example of .xml the struts-config file:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <!-- ========== Form Bean Definitions ============ --> <form-beans> <form-bean name="login" type="test.struts.LoginForm" /> </form-beans> <!-- ========== Global Forward Definitions ========= --> <global-forwards> </global-forwards> <!-- ========== Action Mapping Definitions ======== --> <action-mappings> <action path="/login" type="test.struts.LoginAction" > <forward name="valid" path="/jsp/MainMenu.jsp" /> <forward name="invalid" path="/jsp/LoginView.jsp" /> </action> </action-mappings> <!-- ========== Controller Definitions ======== --> <controller contentType="text/html;charset=UTF-8" debug="3" maxFileSize="1.618M" locale="true" nocache="true"/> </struts-config>
For more details on .xml documentation, see Struts Documentation.
This profile provides a mechanism to change the default behavior of the framework. I n fact, all the properties contained in the struts.properties profile can also be configured in the web.xml using init-param and using the constant .xml in the struts profile. But if you want to keep events independent and keep more struts details, you can create this file under the WEB-INF/classes folder.
struts.propertiesThe values configured in the file are overwritten
default.propertiesthe default values configured in , which are included in the struts2-core-x.y.z.jar distribution. There are some properties that you can consider using instead
struts.propertiesFile:
### When set to true, Struts will act much more friendly for developers struts.devMode = true ### Enables reloading of internationalization files struts.i18n.reload = true ### Enables reloading of XML configuration files struts.configuration.xml.reload = true ### Sets the port that the server is run on struts.url.http.port = 8080
Any line here that starts with s (hash) will be assumed to be a comment, and it will be ignored by Struts 2 by default.