- Setting up a Struts2 environment:
- Add jar package : Copy all the jar packages under Struts\apps\struts2-blank\web-inf\lib to the current WEB app's lib directory.
- Configure struts2 in the Web. XML file: Copy the configuration of the filter in the Struts\apps\struts2-blank1\web-inf\web.xml File.
- The code is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1"><!--interception using the Struts2 interceptor -<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>
- Add struts2 configuration file under the Classpath of the current web App Struts.xml: Copy struts1\apps\struts2-blank\web-inf\ The Struts.xml file under classes is under the SRC directory of the current Web application.
<?xmlversion= "1.0" encoding= "UTF-8 "?><!doctypestrutspublic "-//apachesoftwarefoundation//dtdstrutsconfiguration2.3//en" "http://struts.apache.org/ Dtds/struts-2.3.dtd "><Struts><!--Package: Pack. STRUTS2 uses the packages to organize the modules. Name property: Must. Used for other packages. Extends: Which package inherits the current package, inherited, That is, you can inherit all of the configurations. Typically inherited Struts-defaultstruts-default This package is defined in the Struts-default.xml file. Namespace optional, if it is not given, then/ to the default value. If namespace has a non-default value, then to invoke the action in this package, you must add the namespace defined by this property to the URI string in question http://localhost:8080/contextPath/ Namespace/actionname.action -<PackageName= "HelloWorld"extends= "Struts-default"><!--Configure an action: A STRUTS2 request is a actionname: the name of the request that corresponds to a struts2 (or to a servletpath, but the Remove/and extension), the default value that does not contain the extension class is: The default value for Com.opensymphony.xwork2.ActionSupportmethod is: executeresult: result. -<Actionname= "Product-input"class= "Com.opensymphony.xwork2.ActionSupport"Method= "Execute"><!--Result : results. Represents a result that may be returned after the action method executes. Therefore, an action node may have more than one result child node. Multiple result subnodes use name to differentiate name: Identifies a result. Corresponds to the return value of the action method. The default value is Successtype: The type that represents the result. The default value is dispatcher (forwarded to the result.) -<resultname= "Success"type= "Dispatcher">/web-inf/pages/input.jsp</result></Action> <Actionname= "Product-save"class= "Com.cjm.struts2.helloworld.Product"Method= "Save"><resultname= "Details">/web-inf/pages/details.jsp</result></Action> <Actionname= "Test"class= "Com.cjm.struts2.helloworld.Product"Method= "Test"><result>/index.jsp</result></Action> </ Package></Struts>
Struts2 's Hello World Demo Program