Create struts2 application, first and foremost as previously shown in a good environment to be built. jar package to import and web.xml configuration is not here, write it down. Now look at the first application of struts.xml this configuration file:
<package name="hello" namespace="/test" extends="struts-default">
<action name="sayHello" method="execute">
<result name="success"> / WEB-INF/page/index.jsp </ result>
As indicated above, struts2 in <package> element is used to manage the Action, the packages in the role and java class package is very similar, which is mainly used to manage a group of business functions related to action. In practice, we should put a group of business functions related to a package under the Action on the same. Configuration package must specify the name attribute, the name attribute value can be any name, it must be unique, it does not correspond to java classes packages, if other packets should carry the package, must be carried out through the property reference. Package namespace attribute is used to define the package's name space, name space as access to the package under the Action part of the path, such as access to the above examples, Action, access path: / test / sayHello.action. namespace attribute can not configure, if you do not specify this attribute, the default namespace for the empty string. Configuration namespace there is any good? The name of the space for the name can be effective in preventing duplication, that is the same as the request path, so that it will lead to errors. The second is when there is name space can be configured to reduce the amount of writing, because the same path of the action request can be placed in the same room, under one name, so do not need to write the name of the space each time the value represented by the path. In addition to these two properties, usually every packet should continue to use struts-default package, because many of the core functions are Struts2 interceptors to achieve. Such as: the request parameters from the request package to the action, file upload and data validation, etc. are implemented through the interceptor. struts-default definition of these types of interceptors and Result. Therefore, when the package inherited the struts-default to use struts2 provide core functions. struts-default package is in the struts2-core-2.xxjar file is defined in struts-default.xml. struts-default.xml is also the Struts2 default configuration file. Struts2 automatically loaded each time the file struts-default.xml. Carefully points, we can find struts-default.xml a name for the struts-default package has a property abstract = "true", this means that the package is an abstract package, the package may not contain an abstract action, the abstract package is designed to be used by other packages to inheritance.
Then there is the configuration of <action> elements, which name attribute for the visit when part of the path, class to class fully qualified name, the last attribute is a method that should be invoked method. <result> Elements and struts1 inside the forward elements of the same, equivalent to the view configuration. The next step is Action and the JSP code snippet:
public class HelloWorld (
private String message;
public String getMessage () (
public String execute () (
message = "Hello World! Beijing:";
<body> Inside the JSP page content:
$ (message) <% = new java.util.Date ()%>
After finishing the deployment after the success of the browser, enter the appropriate URL (eg ... / test / sayHello.action) will show the message and time. One method requires to execute is to return a string, because struts2 According to the return string to determine which view is called. From the encoding process of view, Action class is complete POJO class, there is no reliance on any class or interface.
The struts2 in, Action of the name of the search to follow certain principles, the following is the struts2 in the request arrived, how to find the appropriate action to call, summarized as follows (the path to obtain the requested URI, such as the url is: http://server / struts2/path1/path2/path3/test.action):
1. First of all to find namespace for / path1/path2/path3 of the package, if you do not exist in this package is the implementation of Step 2; if there is in this package, then look for the name in this package as a test of the action, when, under the package for less than a will directly go to the default action when namaspace inside the package to find action (the default namespace for the empty string ""), if the default namaspace not find the package which is also the action, can not find the page you are prompted action.
2. To find namespace for the / path1/path2 of the package, if you do not exist in this package, then go to Step 3; if there is in this package, then look for the name in this package as a test of the action, when in the package to find no action When will directly go to the default namaspace inside the package to find the name for the test of action, in the default namaspace not find the package which is also the action, can not find the page you are prompted action.
3. To find namespace for the / path1 the package, if you do not exist in this package is the implementation of Step 4; if there is in this package, then look for the name in this package as a test of the action, when in the package when you find no action will be directly namaspace of the package went inside to find the default name for the test of action, in the default namaspace not find the package which is also the action, can not find the page you are prompted action.
4. To find namespace for / of the package, if it exists in this package, then look for the name in this package as a test of the action, while in the package to find no action or non-existent in this package, the package would go inside to find the default action of namaspace , if you still can not find the page not found prompt action.
In general, that is the equivalent of the principle of maximum matching, if has not been matched, then a one up to find, until the root directory. If you match the path there somewhere, then go to that name space to find the next action, this time If you do not, they jump straight in to find the default package. If you still can not find, it will only Baocuo of. So, this search process is the first big name of the matching space, there is matched only to find the corresponding action, if the match the name of the space there is no need for action, then skip to find the default package, and will not jump back again, matching name space.