First, build STRUTS2 Development environment:
1), download the struts2 required jar package:
to download struts-2.x.x-all.zip, After download, unzip the file, develop struts2 applications need to rely on jar file in the Extract directory of lib folder. Different applications require the jar struts 2 minimum required Span style= "font-family:arial;" >jar
struts2-core-2.1.8.1.jar:struts 2 Core Class Library of the framework
xwork-core-2.1.6.jar:xwork class Library, Struts 2 Build on it
Ognl-2.7.3.jar: Object Graph Navigation language (Object Graph Navigation Language), struts2 framework through its read-write Object
Property
freemarker-2.3.15.jar:struts 2 of the UI the template for the label uses Freemarker
Write
commons-logging-1.1.x.jar:asf the log package produced, Struts 2 Frame Use this log package to support log4j and the JDK
1.4+ log records.
commons-fileupload-1.2.1.jar File Upload component, need to Add this file after 2.1.6 version
Commons-io-1.3.2.jar, upload files dependent on the Jar Package
2), create a Web project, add struts2 required jar package;
3), configure the STRUTS2 filter in the Project Web. XML, (Struts2 is filter-based)
Code:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <filter> <filter-name>strutsprepareandexecutefilt Er</filter-name> <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> < Filter-mapping> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <url-pattern>/* </url-pattern> </filter-mapping></web-app>
In this way, each of our requests will go STRUTS2 filter;
4) Create a JSP page and add an action path to it:
<a href= "${pagecontext.request.contextpath}/primer/helloworldaction.action" >helloWorld</a><br ><!--Clicking on this hyperlink will go struts2-->
5) An action interface is provided in Struts2, and the Execute () method of the action interface is executed by default after STRUTS2 receives the request, so we need to implement the action interface so that STRUTS2 executes our own execute () In Struts2, because Struts2 's action interface has several default implementation classes, we need to inherit the default implementation class Actionsupport.
Code:
Import com.opensymphony.xwork2.actionsupport;/** * All actions in STRUTS2 must implement the action interface */@SuppressWarnings ("Serial") public class Helloworldaction extends Actionsupport {//public class Helloworldaction implements Action {public String E Xecute () throws Exception {System.out.println ("helloworldaction"); return "Success";}}
6) Create Struts.xml file, configure helloworldaction and request connection in the inside;
Struts2.xml House Project SRC directory;
Code:
<?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> <!-- Requested path /primer/helloWorldAction.action--> <!-- The &NBSP;&NBSP;PACKAGE:STRUTS2 framework uses packages to manage the action, where the package is equivalent to a package in Java * name: The name of the package, unique, the primary function for inheriting * namespace: namespace, equivalent to room number , all packages in the unique * extends:struts2 inherit the default package Struts-default * This default package exists in the Struts-default.xml file <package name= "Struts-default" aBstract= "true" > * the reason for the inheritance, to execute the interceptor, Complete STRUTS2-related features action tags: Configure the STRUTS2 framework to access the Action * name: Struts2 Framework to access the action, the only one in the same package * Class: The full classpath of the action you want to access result: Return result of the Execute method that handles the action * name: The only flag for the result, which must correspond to the return value of the Execute method in the action * result the text value of the label; the path to forward and redirect public String Execute () throws exception { system.out.println ("Helloworldaction"); return " Success "; } &Nbsp; --> <package name= "Primer" Namespace= "/primer" extends= "Struts-default" > <!--Configure the default action --> <default-action-ref when the action is not found name= "Helloworldaction"/> <action Name= "Helloworldaction" class= "Cn.yujian.primer.HelloworldAction" > <result name= "Success" >/primer/success.jsp</result> </action> <!-- * if no class is specified for action, The default class is com.opensymphony.xwork2.actionsupport * If no method is specified, the default mode is &NBSP;EXECTUE&NBSP;&NBSP;&NBSP;&NBSP;&NBSPThe Name property is not specified in the * result tag, the default Name property is success --> <action name= "ActionNoClass" > <result>/primer/success.jsp</result> </action> </package></struts>
So far, you can run the struts2, and when you click on the connection in the JSP, you will see the Execute () method executed in Helloworldaction.
STRUTS2 Study (i)