...
Once we've run the application we can see that it works by visiting localhost:8080/tutorial/example/HelloWorldWelcome.action
as a sanity check.
...
The application consists of a few test actions demonstrating simple validation and package-level property (resource) files. The most interesting URLs are as follows (assuming we used groupId=tutorial
):
URL | Description |
---|---|
| Handled by wildcard mapping |
| Handled by explicit mapping, demonstrates package-level properties |
| Handled by explicit mapping with method wilcard (see the |
Application Configuration
The default Struts 2 configuration file is contained in src/main/resources/struts.xml
. It includes an additional configuration file, src/main/resources/example.xml
. The application's mappings are contained in the example.xml
file. The struts.xml
file sets some constants.
See the struts.xml page for more information about the struts.xml
file. For more information regarding what's contained in typical struts.xml
files start at the Configuration Elements page, which contains links to a bunch of information. See the Configuration Files page for more information regarding additional Struts 2 configuration files, including links to the files that set all the Struts 2 default configurations.
Welcome.action
The mapping for this action is handled by a "catch-all" mapping in example.xml
:
Code Block |
---|
<action name="*" class="tutorial2.example.ExampleSupport">
<result>/example/{1}.jsp</result>
</action>
|
This mapping is the last mapping contained in example.xml
--anything not handled by mappings appearing before it in example.xml
will be caught by this mapping. This mapping will look for JSP files in src/main/webapp/example/*.jsp
. Since there's a Welcome.jsp
in that directory, we're all set. See the Wildcard Mapping page for more information on how Struts 2 can use wildcard mappings.
HelloWorld.action
The mapping executes the HelloWorld action, contained in src/main/java/tutorial/example/HelloWorld.action
. This action retrieves a message from a package-level properties file, contained in src/main/resources/tutorial/package.properties
. See the Localization page for more information about how Struts 2 handles message resources (it's pretty cool).
Login.action
This is another wildcard mapping:
Code Block |
---|
<action name="Login_*" method="{1}" class="tutorial2.example.Login">
<result name="input">/example/Login.jsp</result>
<result type="redirectAction">Menu</result>
</action>
|
In the application the Login action is reached via a link, but not directly to /tutorial/example/Login.action
. The link is actually /tutorial/example/Login_input.action
. Reaching the Login action this way bypasses validation: if we hit the Login action directly we'll get a validation error.
The validation configuration is contained in /src/main/resources/tutorial/example/Login-validation.xml
. It checks for the presence of the username
and password
properties (form values). It uses messages stored in the package.properties
file (in the same directory). See the Validation page for more information regarding Struts 2 validation.