?Struts2框架的學習路線
l?第一天:Struts2的概述、Struts2的入門、Struts2常見的配置、Struts2的Action的編寫
l?第二天:Struts2的數據的封裝、結果頁面配置
l?第三天:Struts2的值棧和OGNL表達式
l?第四天:Struts2的標簽庫
?
Struts2的概述
?
?
?
?Struts2是一個基于MVC設計模式的WEB層框架。
?Struts2的內核相對于Struts1來講已經發生巨大變化。
?
?常見的web層框架
Struts2
Struts1
Webwork
SpringMVC
?
?
Web層框架基于前端控制器模型設計
?
?
?
?下載Struts2的開發環境? ?http://struts.apache.org/
?
?解壓Struts2開發包
?
?
?
apps :Struts2提供的應用,war文件:web項目打成war包。直接放入到tomcat可以允許。
?docs :Struts2的開發文檔和API
?lib :Strtus2框架的開發的jar包
?src :Struts2的源碼
?
創建web項目,引入jar包
?
?引入jar包
struts-blank項目下找jar包
?
?
?
?
創建一個JSP頁面
?
?
?
<body><h1>Struts2的入門</h1>
<!--點擊連接會觸發相應的反應--><h3><a href="hello.action">Struts2的入門</a></h3></body>
?
?
編寫Action的類
?
?對Action進行配置
在src下創建(提供)名稱叫做struts.xml的配置文件。注意名字不可以改變,src目錄下的是主要的
?
?
?
?
?配置前端控制器(核心過濾器)
?
?
?這是web.xml中的配置
?
<?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"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 配置Struts2的核心過濾器 --><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>
?
?
改寫Action中的方法的返回值
?
?
?
public class HelloAction {public String execute(){System.out.println("HelloAction執行了......");return "sucess";} }
?
?
改寫struts.xml
?
?
?
<?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> <!-- Struts2為了管理Action的配置,通過包進行管理 --> <!-- 配置Struts2的包============================== --> <!-- package中的名字隨意,但是在配置文件中不可以重復 --><package name="demo1" extends="struts-default" namespace="/"><!-- 配置Action==action中的名字必須是剛剛頁面中的xxx.action==class部分是對應的的類的路徑地址 --><action name="hello" class="com.zyz.struts.HelloAction"><!-- 配置跳轉頁面===當類中返回的是sucess那么就會設定跳轉到相應的界面==== --><result name="sucess">/demo1/sucess.jsp</result></action></package> </struts>
?
?
編寫sucess.jsp
?
?
?
分析Struts2的執行流程
?
?
?
當用戶訪問某一個Action的時候,先經過核心過濾器,在核心過濾器中執行一組攔截器(這組攔截器實現部分功能),執行目標Action,根據Action的返回值,進行頁面跳轉。
?
?
Struts2的常見配置
Struts2的配置文件的加載順序(了解)
?
?init_DefaultProperties() ----加載default.properties
init_TraditionalXmlConfigurations(); ----加載struts-default.xml、struts-plugin.xml、struts.xml
init_LegacyStrutsProperties(); ----加載struts.properties
init_CustomConfigurationProviders(); ?----加載配置提供類
init_FilterInitParameters() ; // [6] ----加載web.xml中過濾器初始化參數
init_AliasStandardObjects() ; // [7] ----加載Bean對象
?
加載順序
?
?default.properties
?
?struts-default.xml
?
?struts-plugin.xml
?
?struts.xml
?
?struts.properties
?
?web.xml
?
?注意:后配置的常量的值會覆蓋先配置的常量的值。
?
Action的配置
package相關配置
package標簽稱為包,這個包與Java中的包的概念不一致。包為了更好管理action的配置。
? ? package標簽的屬性
? ? ? ? ? ? ? ? ?name :包的名稱,只有在一個項目中不重名即可。
? ? ? ? ? ? ? ? extends :繼承哪個包,通常值為struts-default
? ? ? ? ? ? ? ? namespace :名稱空間,與<action>標簽中的name屬性共同決定訪問路徑。
? ? ? ? ? ? ? ?名稱空間有三種寫法:
? ? ? ? ? ? ? ? ? ? ? 帶名稱的名稱空間 :namespace=”/aaa”?
? ? ? ? ? ? ? ? ? ? ? 跟名稱空間 :namespance=”/”
? ? ? ? ? ? ? ? ? ? ?默認名稱空間 :namespace=””
? ? ? ? ? ? ? ? ? ? ?abstract :抽象的,用于其他包的繼承。
?
action相關配置:
action標簽配置Action類。
action標簽的屬性
- name :與namespace共同決定訪問路徑
- class :Action類的全路
- method :執行Action中的哪個方法的方法名,默認值execute
- ?converter :用于設置類型轉換器
?
?
?
常量的配置
? struts2的常量配置
??
在Struts2的框架中,提供了非常多的常量:(在default.properties)
?
- ?struts.i18n.encoding=UTF-8 ----Struts2中所有的post請求的中文亂碼不用處理。
- struts.action.extension=action,, ----Struts2請求的默認的擴展名。默認擴展名是.action或者什么都不寫。在Struts2中修改一些常量的值:
- 修改常量的值,可以有三個位置進行修正
?
?
struts.xml中進行修改
?
?
?
struts.properties中進行修改
?
?
?
?web.xml中進行修改
?
?
?
?
?
分模塊開發設置
?include的配置
?
?
經測試可以正常的運行
首先在包的目錄下建一個struts_demo1.xml代碼如下:
<?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> <!-- Struts2為了管理Action的配置,通過包進行管理 --> <!-- 配置Struts2的包============================== --> <!-- package中的名字隨意,但是在配置文件中不可以重復 --><package name="demo1" extends="struts-default" namespace="/"><!-- 配置Action==action中的名字必須是剛剛頁面中的xxx.action==class部分是對應的的類的路徑地址 --><action name="hello" class="com.zyz.struts.HelloAction"><!-- 配置跳轉頁面===當類中返回的是sucess那么就會設定跳轉到相應的界面==== --><result name="sucess">/demo1/sucess.jsp</result></action></package> </struts>
?
然后在src目錄下的struts.xml的文件中代碼如下:
<?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> <include file="com/zyz/struts/struts_demo1.xml"></include> </struts>
?
?
其他的不變。網頁依然可以正常的進行訪問
?
?
Action的訪問問題
Action類是POJO的類
?
?
?
Action類實現一個Action的接口
?
?
?
Action類繼承ActionSupport類
?
?
?
?
Action的三中訪問方式:
通過method設置
?
<body><h1>Struts2的入門</h1><h3><a href="hello.action">入門</a></h3><h3><a href="find.action">查看信息</a></h3><h3><a href="update.action">修改信息</a></h3><h3><a href="delete.action">刪除信息</a></h3><h3><a href="add.action">添加信息</a></h3></body>
?
?
配置文件?
<struts> <!-- Struts2為了管理Action的配置,通過包進行管理 --> <!-- 配置Struts2的包============================== --> <!-- package中的名字隨意,但是在配置文件中不可以重復 --><package name="demo2" extends="struts-default" namespace="/"><!-- 配置Action==action中的名字必須是剛剛頁面中的xxx.action==class部分是對應的的類的路徑地址 --><action name="find" class="com.learn.struts.demo1.Demo1" method="find"></action><action name="update" class="com.learn.struts.demo1.Demo1" method="update"></action><action name="delete" class="com.learn.struts.demo1.Demo1" method="delete"></action><action name="add" class="com.learn.struts.demo1.Demo1" method="add"></action></package> </struts>
?
?
通過通配符的方式進行配置(*****)
?
?
更加抽象的通配設置
?
?
?
動態方法訪問
?
?開啟動態方法訪問
?
?編寫訪問路徑
?
?
?
?唯一區別就是他用的是感嘆號!
?