I. Introduction of STRUTS2
Struts2 with WebWork excellent design thought as the core, absorbed some advantages of STRUTS1, and established an MVC framework based on WebWork and Struts1.
Second, build STRUTS2 development environment
2.1. Download the latest version via official website: http://struts.apache.org/download.cgi
It is recommended to download the Struts-xx.all.zip package, which contains not only struts2 jars but also examples, source code, and help documentation.
2.2. Import the STRUTS2 required jar package into the project
2.3. Modify the Web. xml file
Add the Strutsprepareandfilter core filter under the <web-app> node of the Web. xml file, mainly responsible for intercepting the user's request and handing it over to the STRUTS2 framework.
2.4. Add the Struts.xml configuration file.
Struts.xml is the core configuration file for Struts2, which is usually placed in the SRC directory, and after it has been compiled and deployed, he is in the web-inf\classes directory of the application.
Third, use struts2 output Hello World
3.1. Create a new Web project and import the Struts jar package
3.2. Add Action Class
There are three ways to implement an action:
1. Write the public String execute () method using the normal Java class
2. Implement the action interface to implement the Execute () method
3. Inherit the Actionsupport class, overriding the Execute () method.
Package Com.able.action;import Com.opensymphony.xwork2.action;import com.opensymphony.xwork2.actionsupport;/** * * @author Xia Chengwei * There are three ways to implement action: * 1. Write the public String execute () method using the normal Java class * 2. Implement the action interface to implement the Execute () method * 3. Inherit action The support class, overriding the Execute () method. * Useraction here Inherits Actionsupport */public class Useraction extends Actionsupport {private string name;private string message ;p ublic 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 written casually * @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 the Struts.xml in the SRC directory and add the appropriate 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 need to restart the Web service configuration to take effect, in order to avoid restarting the Web service multiple times set the following constants--<constant Name= "Struts.devmode" value= "true"/> < ;p ackage 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> <!--performs a proxy class--<action name= "useraction" class= "C Om.able.action.UserAction "method=" execute "> <result name=" abc ">index.jsp</result> <!--type= "ForWord"-</action> <action name= "Login" class= "Com.able.action.LoginFieldActi On "> <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, enter in the browser: Localhost:8080/struts2_demo (own project name)/useraction
You can see the page jump to the index.jsp page, and then in the text box enter Xiazhongwei submit, output a message below,
Iv. Summary4.1, in the browser request Useraction will pass through Struts core filter "Strutsprepareandexecutefilter",
4.2, the core filter will match the name of the same action in Struts.xml according to the request, then jump to the corresponding action class Useraction class,
4.3, will look for in the useraction with struts.xml configured <action method= "Exectue" > tags in the property method the same value of methods, the default execution line method name is the Execute () method, If you define method= "add" in the label, return to the method in the Useraction method that seeks Add.
4.4, after executing the Execute () method, based on the return value string, look for the same result as the <resutl> value in the <action></action> tag in the Struts.xml
4.5, according to the type of jump to the corresponding request, the example definition jumps to index.jsp, so you will see in the page Useraction class The message value returned in the Execute method "Hello World" +name,name is the value of the page submission form
STRUTS2 Basic article (1)