1.新建一個web項目,目錄結構如下,添加jar包到lib文件夾里,並把jar包add 到 buildpath里面
2.web.xml配置 struts2的過濾器類:StrutsPrepareAndExecuteFilter ,把全部請求定位到該Struts2過濾器中
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <welcome-file-list> 7 <welcome-file>index.jsp</welcome-file> 8 </welcome-file-list> 9 10 <filter> 11 <filter-name>StrutsPrepareAndExecuteFilter</filter-name> 12 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 13 </filter> 14 15 <filter-mapping> 16 <filter-name>StrutsPrepareAndExecuteFilter</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 20 </web-app>
3. 通過實現Action接口的方式來創建一個處理請求的action類:
(1)Action接口中定義了public String execute()方法,來執行用戶請求的處理,此外,該沒看還定義了五個字符類型的靜態常量,是Action默認支持的邏輯視圖名
常量 | 值 | 邏輯含義 |
SUCCESS | “success” | 表示程序處理正常,並返回給用戶成功后的結果 |
NONE | "none" | 表示處理正常結束,但不返回給用戶任何提示 |
ERROR | "error" | 表示處理結果失敗 |
INPUT | "input" | 表示需要更多用戶輸入才能順利執行 |
LOGIN | "login" | 表示需要用戶正確登陸后才能順利執行 |
(2)Action類中的屬性可以直接來接收用戶的輸入(也就是獲取請求參數),單表單提交愛哦時,Struts2自動對請求參數進行轉換並對具有相同名字的Action屬性進行賦值(通過setter方法,因此Action類中接收請求參數的屬性,必須要設置getter setter方法)
(3)Action類的屬性除了用來接收請求的數據,還可以用來輸出處理結果,仍然是借助get set方法對結果屬性進行處理,如下例子中所示。
(4)Struts2中,系統不會識別哪些舒心用於接收請求參數,哪些屬性用於輸出處理結果,只要對屬性設置了get set方法,該屬性就可以被自動處理。
(5)Action類中還可以使用復雜的屬性,如用戶自定義類,數組,集合對象等
1 package cn.test.action; 2 3 import com.opensymphony.xwork2.Action; 4 5 public class HelloAction implements Action { 6 7 private String name=""; //Action類中定義的屬性設置的set get方法,就可以接收前端傳遞過來的與該屬性名同名的參數值 8 private Integer age=0; 9 private String msg; 10 11 public String getName() { 12 return name; 13 } 14 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 public Integer getAge() { 20 return age; 21 } 22 23 public void setAge(Integer age) { 24 this.age = age; 25 } 26 27 28 public String getMsg() { 29 return msg; 30 } 31 32 public void setMsg(String msg) { 33 this.msg = msg; 34 } 35 36 @Override 37 public String execute() throws Exception { 38 System.out.println("hello action"); 39 System.out.println("name value is:"+this.name); 40 System.out.println("age value is:"+this.getAge()); 41 if(this.getName().equals("admin")){ 42 this.setMsg("this is message from HelloAction"); //設置返回給頁面的消息 43 return SUCCESS; 44 } else { 45 return ERROR; 46 } 47 } 48 }
4.編寫Struts2的配置文件
(1)package元素定義Struts2處理請求的邏輯單元,其name屬性用來指定包的名稱(必須指定,並且唯一,因為要用於被其他包引用),extends用於指定要擴展的包
(2)action元素用於配置Action類,用於把一個請求的URI對應到一個Action類,name屬性表示action的名稱,必須指定,class屬性可選,用於設定action類的全限定名
(3)result元素用於指定邏輯視圖名,name屬性表示result的邏輯視圖名稱(就是上邊HelloAction類的execute方法的返回值),必須與Action類返回的字符串匹配;result元素的值表示與邏輯視圖名對應的物理資源之間的映射,用來指定這個結果對應的實際資源的位置 注意,<result name="success">/success.jsp</result> 中,有/表示使用的絕對路徑,是當前web應用程序的上下文路徑,<result name="success">success.jsp</result> 無/ 相當於當前執行的Action路徑
(4)result元素如果沒寫name屬性,默認是返回success對應的邏輯視圖
(5)package中的namespace的值+action元素中name屬性的值,確定了一個唯一的請求action的路徑
(6)以下面這個action為例,如果action里面沒有指定method方法,那么調用默認的execute方法,企業項目中,還是要在result元素里面加上method屬性,指定處理請求的方法。
<action name="helloStruts" class="cn.test.action.HelloAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action>
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 <struts> 6 <!-- 開啟動態調用,並且設置啟用開發者模式 --> 7 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> 8 <constant name="struts.devMode" value="true"></constant> 9 10 <package name="default" namespace="/user" extends="struts-default"> 11 12 <!-- 配置默認的行為 ,請求里什么行為都不輸入 http://localhost:8080/strutsstu1/user/ --> 13 <default-action-ref name="defaultAction"></default-action-ref> 14 <!-- 全局結果所有的錯誤都跳轉到error.jsp --> 15 <global-results> 16 <result name="myexcept">/error.jsp</result> 17 </global-results> 18 <action name="defaultAction"> 19 <result>/error.jsp</result> 20 </action> 21 <!-- http://localhost:8080/strutsstu1/user/helloStruts?name=admin&age=20 22 package中定義的namespace+action中的name,可以確定一個唯一的訪問action的路徑 --> 23 <action name="helloStruts" class="cn.test.action.HelloAction"> 24 <result name="success">/success.jsp</result> 25 <result name="error">/error.jsp</result> 26 </action> 27 </package> 28 29 <package name="user" namespace="/user" extends="struts-default"> 30 <action name="login" class="cn.test.action.UserAction" 31 method="login"> 32 <result>/login.jsp</result> 33 <!-- result不寫name屬性,默認就是success --> 34 </action> 35 <action name="register" class="cn.test.action.UserAction" 36 method="register"> 37 <result>/register.jsp</result> 38 </action> 39 40 <!-- 動態調用 --> 41 <action name="dyTest" class="cn.test.action.User2Action"> 42 <!-- http://localhost:8080/strutsstu1/user/dyTest!login.action --> 43 <result name="gologin">/login.jsp</result> 44 <!--請求地址: http://localhost:8080/strutsstu1/user/dyTest!register.action--> 45 <result name="goregister">/register.jsp</result> 46 </action> 47 48 <!-- 動態調用2 --> 49 <action name="*User" class="cn.test.action.User3Action" 50 method="{1}"> 51 <!--http://localhost:8080/strutsstu1/user/deleteUser.action?value=admin--> 52 <result name="deleteadmin">/{1}admin.jsp</result><!-- deleteadmin --> 53 <result name="deletecommon" type="redirect">/{1}common.jsp</result> <!-- 請求中的*的值組裝成deletecommon.jsp--> 54 <!--http://localhost:8080/strutsstu1/user/deleteUser.action?value=adminxxx--> 55 <!-- <result name="deleteerror">/{1}error.jsp</result> --> 56 <!-- --> 57 <result name="deleteerror" type="redirectAction">login</result> <!-- 相當於請求重定向到/user/login --> 58 59 </action> 60 </package> 61 62 63 <package name="service" namespace="/student" extends="struts-default"> 64 <!-- http://localhost:8080/strutsstu1/student/stulogin.action --> 65 <action name="stulogin" class="cn.test.action.StudenAction" 66 method="studentLogin"> 67 <result name="studentLoginSucess">/stuLogin.jsp</result> 68 </action> 69 <action name="sturegister" class="cn.test.action.StudenAction" 70 method="studentRegister"> 71 <result name="studentRegisterSuccess">/stuRegister.jsp</result> 72 </action> 73 74 <action name="stu" class="cn.test.action.StudenAction"> 75 <!--http://localhost:8080/strutsstu1/student/stu!studentLogin.action--> 76 <result name="studentLoginSucess">/stuLogin.jsp</result> 77 <result name="studentRegisterSucess">/stuRegister.jsp</result> 78 </action> 79 </package> 80 81 <package name="ognl" namespace="/ognl" extends="struts-default"> 82 <action name="show" class="cn.test.action.OgnlAction"> 83 <result>/show.jsp</result> 84 </action> 85 <action name="showArray" class="cn.test.action.ArrayAction"> 86 <result name="success">/show.jsp</result> 87 </action> 88 </package> 89 90 <!-- 攔截器 --> 91 <package name="intercepter" namespace="/" extends="struts-default"> 92 <interceptors> 93 <interceptor name="myTime" class="cn.test.interceptor.MyTimeIntercepter"></interceptor> 94 <interceptor-stack name="myTimeStack"> 95 <interceptor-ref name="myTime"></interceptor-ref> <!-- 把自定義的攔截器設置為棧的頂部 --> 96 <interceptor-ref name="defaultStack"></interceptor-ref> 97 </interceptor-stack> 98 </interceptors> 99 <action name="index"> 100 <result>/index.jsp</result> 101 <interceptor-ref name="myTimeStack"></interceptor-ref> <!-- 讓myTimeStack這個攔截器棧去進行攔截 --> 102 </action> 103 </package> 104 105 106 </struts>
5.編寫物理視圖文件success.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() + "://" 6 + request.getServerName() + ":" + request.getServerPort() 7 + path + "/"; 8 %> 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html> 12 <head> 13 <base href="<%=basePath%>"> 14 <title>My JSP 'success.jsp' starting page</title> 15 </head> 16 17 <body> 18 This is my success JSP page. 19 <br> 20 <!--<s:property value="name"/> value里面寫上Action類中定義的屬性,就能在jsp頁面顯示請求中所帶的參數了 --> 21 顯示struts action中的屬性內容:<br> 22 采用OGNL與Struts2標簽輸出Action類的屬性值(Action類獲取到的請求參數值):<br> 23 name=<s:property value="name" /> age=<s:property value="age" /> <br> 24 采用EL表達式 輸出Action類的屬性值(Action類返回給頁面的值):<br> 25 msg=${msg }; 26 </body> 27 </html>
在瀏覽器中輸入 http://localhost:8080/strutsstu1/user/helloStruts?name=admin&age=20 並訪問,結果如下,
6.Struts2應用的執行流程簡單總結:
(1)瀏覽器發出user/helloStruts這個請求到服務器(web.xml中配置的url-pattern是/*)
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(2)服務器接收后,根據web.xml的配置,將請求分發給指定的Struts2過濾器
(3)過濾器根據Struts.xml的配置內容,將請求發送給cn.test.action.HelloAction類的對象,並調用默認的execute方法、
(4) 根據execute方法的返回結果,在struts.xml文件中匹配 邏輯視圖名為success的處理結果(
<result name="success">/success.jsp</result>),當execute方法的返回值為success字符串時,跳轉到success.jsp頁面
(5)頁面根據上下文中的內容,借助Struts2的OGNL表達式和Struts2標簽將內容顯示在頁面上,需要先導入Struts2的標簽庫:
<%@ taglib uri="/struts-tags" prefix="s"%>
示例代碼下載地址 https://github.com/liuch0228/Struts2SSH.git