Struts2 Study Notes (1)-Environment setup, struts2 Study Notes
1. Create a Web project and import the main jar package of Struts2
Create a Web project in MyEclipse and add the required jar package under the lib directory:
2. Create a jsp page
1) Create the test. jsp page:
1 <body> 2 <a href = "$ {pageContext. request. contextPath}/testAction. action"> test </a> 3 </body>
2) create a success. jsp page:
1 <body>2 success!3 </body>
3. Create an Action:
1 public class TestAction {2 public String test() {3 return "success";4 }5 }
4. Configure the struts. xml file
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE struts PUBLIC 3 "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <constant name = "struts. devMode "value =" true "/> 8 <! -- 9 package: package 10 * name: package name, unique, required 11 * namespace: namespace, unique, equivalent to the room number. Optional. If not specified, it is "/". The first half of the Request connection in the page 12 * extends: inherits other package13 * extends = "struts-default ": struts2 framework underlying provides struts2-core-2.3.3.jar files under the core package struts-default.xml 14 --> 15 <package name = "default" namespace = "/" extends = "struts-default"> 16 <! -- 17 action: 18 * name: corresponding to the last half of the Request connection on the page 19 * class: the complete path of the class to be executed 20 * method: name of the method to be executed 21 --> 22 <action name = "testAction" class = "com. sunny. action. testAction "method =" test "> 23 <! -- 24 result: result type 25 * name: corresponds to the text content in the second half of the return value of the method of the execution Class 26: page 27 --> 28 <result name = "success">/success. jsp </result> 29 </action> 30 </package> 31 </struts>
5. Configure the web. xml file
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5 6 <display-name>Struts Blank</display-name> 7 8 <filter> 9 <filter-name>struts2</filter-name>10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>11 </filter>12 13 <filter-mapping>14 <filter-name>struts2</filter-name>15 <url-pattern>/*</url-pattern>16 </filter-mapping>17 18 </web-app>
6. Access
Enter http: // localhost: 8080/Struts2_01/test. jsp in the browser. The page is as follows:
Click test. The execution result is as follows: