1.Struts2概述
1.1 什么是Struts2
- Struts2是一個基于MVC設計模式的Web應用框架,它本質(zhì)上相當于一個servlet靡羡,在MVC設計模式中,Struts2作為控制器(Controller)來建立模型與視圖的數(shù)據(jù)交互蹈胡。Struts 2是Struts的下一代產(chǎn)品通熄,是在 struts 1和WebWork的技術(shù)基礎上進行了合并的全新的Struts 2框架。其全新的Struts 2的體系結(jié)構(gòu)與Struts 1的體系結(jié)構(gòu)差別巨大脱羡。Struts 2以WebWork為核心萝究,采用攔截器的機制來處理用戶的請求免都,這樣的設計也使得業(yè)務邏輯控制器能夠與ServletAPI完全脫離開,所以Struts 2可以理解為WebWork的更新產(chǎn)品帆竹。雖然從Struts 1到Struts 2有著太大的變化绕娘,但是相對于WebWork,Struts 2的變化很小栽连。
- Struts2是一個基于MVC設計模式的web層框架险领,Struts2的內(nèi)核相對于Struts來講已經(jīng)發(fā)生巨大變化。
- 常見的web框架
- Struts2
- Struts1
- Webwork
- SpringMVC
1.2 Web層框架基于前端控制器模型設計
-
前端控制器模型
前端控制器模型
2. Struts2入門案例
2.1 Struts2的開發(fā)環(huán)境
2.2 解壓開發(fā)包
解壓struts-2.3.24-all
apps----------Struts2提供的應用秒紧,war文件:web項目打成war包绢陌。直接放入到tomcat可以允許。
docs ----------Struts2的開發(fā)文檔和API
lib--------------Strtus2框架的開發(fā)的jar包
src-------------Struts2的源碼
2.3 創(chuàng)建項目噩茄,引入jar包
- 將..\struts-2.3.24-all\struts-2.3.24\apps下的struts2-blank.war包拷貝到tomcat的webapps目錄下下面,啟動tomcat服務器,war包會自動解壓成一個工程绩聘。
- 打開解壓的該工程找到lib目錄下就可以看到建立一個最簡單的Struts2工程需要的jar包了沥割。
- 將這些jar包拷貝到新建的工程中
2.4 創(chuàng)建一個JSP頁面
-
WebContent/demo1/demo1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <h2>Struts2的入門</h2> <h3><a href="${pageContext.request.contextPath }/hello.action">Struts2的入門</a></h3> </body> </html>
2.5 編寫Action的類
package com.itzhouq.struts.demo1;
/**
* Struts2的入門的Action類
*
*/
public class HelloAction {
/**
* 提供一個方法:
* *方法簽名固定的,公有的凿菩,返回值是String類型机杜,方法名為execute,在這個方法中不能傳遞參數(shù)
*/
public String execute() {
System.out.println("HelloAction執(zhí)行了衅谷。椒拗。。获黔。");
return null;
}
}
2.6 對Action進行配置
- 在src下創(chuàng)建(提供)名稱叫做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 name="demo1" extends="struts-default" namespace="/">
<!-- 配置Action===== -->
<action name="hello" class="com.itzhouq.struts.demo1.HelloAction"/>
</package>
</struts>
2.7 配置前端控制器(核心過濾器)
- 在web.xml文件中添加以下內(nèi)容
<!-- 配置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>
2.8 測試
- 發(fā)布項目
- 訪問JSP的文件,在控制臺能打印相關(guān)信息玷氏。
2.9 改寫Action中的方法的返回值
package com.itzhouq.struts.demo1;
/**
* Struts2的入門的Action類
*
*/
public class HelloAction {
/**
* 提供一個方法:
* *方法簽名固定的堵未,公有的,返回值是String類型盏触,方法名為execute渗蟹,在這個方法中不能傳遞參數(shù)
*/
public String execute() {
System.out.println("HelloAction執(zhí)行了。赞辩。雌芽。。");
return "success";
}
}
2.10 改寫struts.xml
<struts>
<!-- Struts2為了管理Action的配置辨嗽,通過包進行管理 -->
<!-- 配置Struts2的包=========== -->
<package name="demo1" extends="struts-default" namespace="/">
<!-- 配置Action===== -->
<action name="hello" class="com.itzhouq.struts.demo1.HelloAction">
<!-- 配置頁面的跳轉(zhuǎn) -->
<result name="success">/demo1/success.jsp</result>
</action>
</package>
</struts>
2.11 編寫success.jsp
<body>
<h1>頁面跳轉(zhuǎn)成功J缆洹!糟需!</h1>
</body>
2.12 再次測試
- 訪問項目地址屉佳,控制臺打印了
HelloAction執(zhí)行了来破。。忘古。徘禁。
信息,頁面跳轉(zhuǎn)到了success.jsp
頁面髓堪。
3. Struts2的執(zhí)行流程
- 當用戶訪問到某一個Action的時候送朱,先經(jīng)過核心過濾器,在核心過濾器中執(zhí)行一組核心攔截器(這組攔截器實現(xiàn)部分功能)干旁,執(zhí)行目標Action驶沼,根據(jù)Action的返回值,進行頁面跳轉(zhuǎn)争群。
4. Struts2的常見配置
4.1 XML的提示問題
- 斷網(wǎng)條件下回怜,xml的提示需要手動配置本地dtd文件
- 首先復制xml文件中的連接http://struts.apache.org/dtds/struts-2.3.dtd,再進行以下配置
4.2 Struts2的配置文件的加載順序(了解)
4.2.1 源碼:Struts2的配置文件的加載順序
- init_DefaultProperties(); ------------------------------------------------------------加載default.properties
- init_TraditionalXmlConfigurations(); -----加載struts-default.xml换薄、struts-plugin.xml玉雾、struts.xml
- init_LegacyStrutsProperties();------------------------------------------------------加載struts.properties
- init_CustomConfigurationProviders();-------------------------------------------------加載配置提供類
- init_FilterInitParameters() ;------------------------------------------加載web.xml中過濾器初始化參數(shù)
- init_AliasStandardObjects() ;----------------------------------------------------------------加載Bean對象
4.2.2 加載順序
default.properties
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
-
web.xml
- 前三個是框架提供的配置文件,后三個都可以配置Struts2的常量
- 注意:后配置的常量的值會覆蓋先配置的常量的值轻要。
4.3 Action的配置
4.3.1 package相關(guān)配置
package標簽稱為包复旬,這個包與Java中的包的概念不一致。這個包為了更好管理action的配置冲泥。
-
package標簽的屬性
name:包的名稱驹碍,只有在一個項目中不重名即可。
extends:繼承哪個包凡恍,通常值為struts-default志秃。
-
namespace:名稱空間,與
<action>
標簽中的name屬性共同決定訪問路徑嚼酝。-
名稱空間有三種寫法:
帶名稱的名稱空間:namespace=”/aaa”
跟名稱空間:namespance=”/”
默認名稱空間:namespace=””
-
abstract :抽象的浮还,用于其他包的繼承.
4.3.2 action相關(guān)配置
- action標簽配置Action類.
- action標簽的屬性:
- name :與namespace共同決定訪問路徑
- class :Action類的全路徑
- method :執(zhí)行Action中的哪個方法的方法名,默認值execute
- converter :用于設置類型轉(zhuǎn)換器
4.4 Struts2中常量的配置
在Struts2的框架中革半,提供了非常多的常量碑定,主要在default.properties中
struts.i18n.encoding=UTF-8 ----Struts2中所有的post請求的中文亂碼不用處理流码。
struts.action.extension=action,, ----Struts2請求的默認的擴展名又官。默認擴展名是.action或者什么都不寫。
-
在Struts2中修改一些常量的值:可以與三個位置
-
struts.xml中進行修改
<!-- 配置Struts2的常量 --> <constant name="struts.action.extension" value="abc"/>
-
struts.properties中進行修改
struts.action.extension=action
-
web.xml中進行修改
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 修改常量 --> <init-param> <param-name>struts.action.extension</param-name> <param-value>xyz</param-value> </init-param> </filter>
一般在struts.xml中進行修改漫试,如果在三個文件中都修改六敬,則以web.xml中為準。
-
4.5 分模塊開發(fā)的配置
-
include的配置,引入多個struts.xml配置文件
<include file="com/itzhouq/struts/demo1/struts_demo1/xml"/>
5. Action的訪問
5.1 Action的三種寫法
5.1.1 方式一:Action類是POJO類
package com.itzhouq.struts.demo2;
/**
* Action的編寫方式:Action類是一個POJO類
* @author itzhouq
*
*/
public class ActionDemo1 {
public String execute() {
System.out.println("ActionDemo1執(zhí)行了.......");
return null;
}
}
5.1.2 方式二:Actions實現(xiàn)了一個Action的接口
package com.itzhouq.struts.demo2;
import com.opensymphony.xwork2.Action;
/**
* Action的編寫方式二:實現(xiàn)一個Action接口
* * 實現(xiàn)接口的這種方式:提供了五個常量(五個邏輯視圖的名稱)
* *SUCCESS:成功
* *ERROR:失敗
* *LOGIN:登錄出錯頁面跳轉(zhuǎn)
* *INPUT:表單校驗的時候出錯
* *NONE:不跳轉(zhuǎn)
*
* @author itzhouq
*
*/
public class ActionDemo2 implements Action {
@Override
public String execute() throws Exception {
System.out.println("ActionDemo2執(zhí)行了..........");
return null;
}
}
5.1.3 方式三:Action類繼承ActionSupport類【推薦】
package com.itzhouq.struts.demo2;
import com.opensymphony.xwork2.ActionSupport;
/**
* Action的編寫方式三:Action類繼承ActionSupport類
* ***推薦使用繼承ActionSupport的方式:
* ***ActionSupport提供了數(shù)據(jù)校驗驾荣、國際化等一系列操作的方法外构。
* @author itzhouq
*
*/
public class ActionDemo3 extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("ActionDemo3執(zhí)行了............");
return NONE;
}
}
三種寫法在配置文件的配置信息:
<package name="demo2" extends="struts-default" namespace="/">
<!-- 配置Action===== -->
<action name="ActionDemo1" class="com.itzhouq.struts.demo2.ActionDemo1"></action>
<action name="ActionDemo2" class="com.itzhouq.struts.demo2.ActionDemo2"></action>
<action name="ActionDemo3" class="com.itzhouq.struts.demo2.ActionDemo3"></action>
</package>
5.2 Action的訪問
5.2.1 通過method設置
首先寫一個JSP頁面../WebContent/demo3/demo3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Action的訪問</h1>
<h3>通過method方式</h3>
<a href="${ pageContext.request.contextPath }/userFind.action">查詢用戶</a>
<a href="${pageContext.request.contextPath }/userDelete.action">刪除用戶</a>
<a href="${pageContext.request.contextPath }/userUpdate.action">修改用戶</a>
<a href="${pageContext.request.contextPath }/userSave.action">保存用戶</a>
</body>
</html>
編寫Action類com.itzhouq.struts.demo3.UserAction
package com.itzhouq.struts.demo3;
import com.opensymphony.xwork2.ActionSupport;
/**
* Action的訪問方式:通過method方式
* @author itzhouq
*
*/
public class UserAction extends ActionSupport {
public String find() {
System.out.println("查詢用戶.......");
return NONE;
}
public String delete() {
System.out.println("刪除用戶.......");
return NONE;
}
public String update() {
System.out.println("更新用戶.......");
return NONE;
}
public String save() {
System.out.println("保存用戶.......");
return NONE;
}
}
在和Action同一個包寫編寫配置問價com/itzhouq/struts/demo3/struts_demo3.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 name="demo3" extends="struts-default" namespace="/">
<!-- 配置Action===== -->
<action name="userFind" class="com.itzhouq.struts.demo3.UserAction" method="find"></action>
<action name="userDelete" class="com.itzhouq.struts.demo3.UserAction" method="delete"></action>
<action name="userUpdate" class="com.itzhouq.struts.demo3.UserAction" method="update"></action>
<action name="userSave" class="com.itzhouq.struts.demo3.UserAction" method="save"></action>
</package>
</struts>
在主配置文件中引入struts_demo3.xml
<include file="com/itzhouq/struts/demo3/struts_demo3.xml"/>
5.2.2 通過通配符的方式【掌握】
編寫Action類ProductAction
package com.itzhouq.struts.demo3;
import com.opensymphony.xwork2.ActionSupport;
public class ProductAction extends ActionSupport {
public String find() {
System.out.println("查找商品....");
return NONE;
}
public String update() {
System.out.println("更新商品....");
return NONE;
}
public String delete() {
System.out.println("刪除商品....");
return NONE;
}
public String save() {
System.out.println("保存商品....");
return NONE;
}
}
在../struts_demo3.xml配置文件中添加
<!-- 通配符的方式 -->
<action name="product_*" class="com.itzhouq.struts.demo3.ProductAction" method="{1}"></action>
通配符的配置
5.2.3 動態(tài)方法訪問
-
開啟動態(tài)方法訪問,在struts_demo3.xml配置文件中添加
<!-- 開啟動態(tài)方法訪問 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
-
動態(tài)方法訪問的配置
<!-- 動態(tài)方法訪問的方式 --> <action name="customer" class="com.itzhouq.struts.demo3.CustomerAction"></action>
-
編寫訪問路徑jsp文件
<h3>通過動態(tài)方法訪問的方式</h3> <a href="${ pageContext.request.contextPath }/customer!find.action">查詢用戶</a> <a href="${ pageContext.request.contextPath }/customer!update.action">查詢用戶</a> <a href="${ pageContext.request.contextPath }/customer!delete.action">查詢用戶</a> <a href="${ pageContext.request.contextPath }/customer!save.action">查詢用戶</a>