1. Build the development environment of struts2
1. Find the required jar package: In the lib directory of the release package (the minimum jar package required by different versions is different. For details, see the documentation of different versions. 2.1.7)
Struts2-core.jar core jar package
Xwork-2.jar xwork core jar package
Ognl. Jar ognl expression
Freemarker. Jar freemarker Template
Commons-logging.jar log
Commons-fileupload.jar File Upload
Commons-io.jar files upload dependent packages
2. Create a configuration file named struts. xml under the WEB-INF/classes directory of the application, the content is as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> </struts>
3. configuring the core controller is a filter.
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4. If Tomcat is successfully started and no error is reported, the environment is successfully set up!
2. Develop the first struts2 case
1. Compile the Struts. xml configuration file.
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.1.7 // en" http://struts.apache.org/dtds/struts-2.1.7.dtd "> <struts> <! -- This is the root element of the struts2 configuration file --> <package name = "usst" namespace = "/test" extends = "struts-Default"> <! -- Pageckage: it is convenient to manage action element name: required. The package name, which must be unique in the configuration file. Namespace: the name of the package, generally starting with "/" extends: the name of the parent package for integration. The Struts-default package is a package named after the struts2 framework. (There is a struts2-core.jar in the struts-default.xml) Abstract: whether it is an abstract package. A package without any action elements is an abstract package (Java class) --> <action name = "helloworld" class = "CN. usst. action. helloworldaction "method =" sayhello "> <! -- Action: indicates a request action name, which must be unique in the same package. Action name class: The full name of the JavaBean class to be processed method: corresponding processing method in the JavaBean. (Action Method: feature: Public String method name () {}) --> <result name = "success">/1.jsp</result> <! -- Result: result type name: String body content returned by the Action method: the specific address of the view. --> </Action> </package> </struts>2. Create the required JavaBean and corresponding action methods based on the configuration file to complete your logical call in the Action method.
package cn.usst.action;public class HelloWorldAction implements Serializable {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String sayHello(){message = "helloworld by struts2";return "success";}}3. Write a view and display the result.
${message}4. How to access the helloworld action: http: // localhost: 8080/struts2day01/test/helloworld Application name/package namespace/action name
By default, the access action name helloworld can be helloworld or helloworld. Action.
Http: // localhost: 8080/struts2day01/test/A/B/C/helloworld
/Test/A/B/C: namespace
Helloworld: Action name
Search Order: namespace
/Test/A/B/C without helloworld
/Test/a/B without helloworld
/Test/A does not have helloworld
/Test is available. Call and execute
Iii. Detailed description of struts2 configuration file
1. Is there any prompt for compiling the Struts. xml configuration file?
Method 1: access the Internet
Method 2:
1. Copy http://struts.apache.org/dtds/struts-2.1.7.dtdaddress
2. Search for XML catelog in windows and preferences of eclipse
3. Click Add.
Location: path of the DTD file
Key type: URI
Key: http://struts.apache.org/dtds/struts-2.1.7.dtd
2. Various default values in the struts configuration file.
Action:
Class: The default value is com. opensymphony. xwork2.actionsupport.
Constant: Success success
None none
Error
Input
Login Login
Method: The default value is Public String execute (){}
In actual development: the compile class compiled by myself inherits from Com. opensymphony. xwork2.actionsupport.
Result:
Type: to the destination. The default value is forwarding and the name is dispatcher.
(Note: The value of type is well defined, not blindly written. Defined in the package in the struts-default.xml)
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/><result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/><result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/><result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/><result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/><result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/><result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/><result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/><result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/><result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
Dispatcher: Common forwarding to a page
Chain: A common forward to an action name.
Redirect: redirect to a page
Redirectaction: redirect to an action name
Plaintext: outputs JSP content in plain text format
Writing the result element:
Method 1:
<result type="chain" name="success">a2</result>
Method 2:
<Result type = "chain" name = "success"> <Param name = "actionname"> A2 </param> <! -- Setactionname () method in the processor of the chain corresponding to name --> </result>
Note: If you want to switch to another namespace action, you can only use method 2.
<package name="p1" namespace="/namespace1" extends="struts-default"><action name="a2"><result type="dispatcher" name="success">/3.jsp</result></action></package><package name="p2" namespace="/namespace2" extends="struts-default"><action name="a1"><result type="chain" name="success"><param name="namespace">/namespace1</param><param name="actionName">a2</param></result></action></package>
3. Make changes to the configuration file during development so that the framework is automatically reloaded during access:
Struts. devmode = false (default. properties) Use strutx. the constant element in XML overwrites default. default properties behavior <struts> <constant name = "struts. devmode "value =" true "> </constant> </struts>