Struts2 BASICS (1) and struts2 Basics
1. Introduction to Struts2
Taking excellent WebWork design as the core, Struts2 absorbs some advantages of Struts1 and establishes a MVC Framework Based on WebWork and Struts1.
2. Build the Struts2 Development Environment
2.1, download the latest version through the official website: http://struts.apache.org/download.cgi
We recommend that you download the struts-xx.all.zip package. The package not only contains the struts2 jar package, but also contains examples, source code, and help documentation.
2.2 import the jar package required by Struts2 in the project
2.3 modify the web. xml file
Add the StrutsPrepareAndFilter core filter under the <web-app> node of the web. xml file to intercept user requests and send them to the Struts2 framework for processing.
2.4 Add the struts. xml configuration file.
Struts. xml is the core configuration file of Struts2, which is usually placed in the src directory. After compilation and deployment, it will go to the WEB-INF \ classes directory of the application.
3. Output Hello World using struts2
3.1 create a web project and import the struts jar package
3.2 Add Action class
There are three methods to implement Action:
1. Use a common Java class to compile the public String execute () method
2. Implement the Action interface and execute () method
3. inherit the ActionSupport class and override the execute () method.
Package com. able. action; import com. opensymphony. xwork2.Action; import com. opensymphony. xwork2.ActionSupport;/***** @ author Xia Zhongwei * There are three methods to implement Action: * 1. use a common Java class to compile the public String execute () method * 2. implement the Action interface and execute () method * 3. inherits the ActionSupport class and overwrites the execute () method. * UserAction here inherits ActionSupport */public class UserAction extends ActionSupport {private String name; private String message; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getMessage () {return message;} public void setMessage (String message) {this. message = message;}/*** this method name can be casually written * @ return */public String execute () {System. out. println (name + "-----------------"); message = "Hello World" + name; return "abc ";}}
3.3 modify web. xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2_Demo</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
3.4 add struts. xml under the src directory and add the corresponding configuration
<? 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> <constant name = "struts. enable. dynamicMethodInvocation "value =" false "/> <! -- Modified Struts. xml requires you to restart the web service configuration to take effect. To avoid multiple restart of the web service, set the following constant --> <constant name = "struts. devMode "value =" true "/> <package name =" default "namespace ="/"extends =" struts-default "> <default-action-ref name =" index" /> <global-results> <result name = "error">/err. jsp </result> </global-results> <global-exception-mappings> <exception-mapping exception = "java. lang. exception "result =" error "/> </global-exception-mappings> <! -- The agent class is executed --> <action name = "userAction" class = "com. able. action. userAction "method =" execute "> <result name =" abc "> index. jsp </result> <! -- Type = "forword" --> </action> <action name = "login" class = "com. able. action. loginFieldAction "> <result name =" success "> index. jsp </result> </action> </package> <! -- <Include file = "exemple. xml"> </include> --> <! -- Add packages here --> </struts>
3.5 modify the index. jps file.
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <% @ taglib uri = "/struts-tags" prefix = "s" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
3.6 Add the project to tomcat to start the project. In the browser, enter: localhost: 8080/Struts2_Demo (your project name)/userAction
You can see the page Jump to the index. jsp page, and then input xiazhongwei in the text box to submit, Output message below,
Iv. Summary
4.1 when requesting userAction in a browser, the request will go through Struts's core filter "StrutsPrepareAndExecuteFilter ",
4.2 The core filter will match the action with the same name in struts. xml according to the request, and then jump to the corresponding action processing class userAction class,
4.3. Search for and struts in userAction. the <action method = "exectue"> label configured in xml has the same value as the property method. The default execution method is the execute () method, if method = "add" is defined in the tag, the userAction method is used to seek the add method.
4.4 After executing the execute () method, find the result matching the same value in the <action> </action> tag of struts. xml based on the returned value String.
4.5. Jump to the corresponding request based on the type. The previous example defines the jump to index. jsp, so the message value "Hello world" + name returned by the execute method in the userAction class is displayed on the page. name is the value in the form submitted on the page.