1. 程式人生 > >rest外掛實現restful風格第1集(Struts2)

rest外掛實現restful風格第1集(Struts2)

  1. struts2外掛算得上是她騰飛的翅膀,這裡先直接感覺下rest外掛簡單事例。
  2. 看一下rest外掛,規則
==========================================URL對映表,傳送一個請求呼叫哪個Action的方法=====================================================
				The following URL's will invoke its methods: 
				
				說明:movies代表action名,Thrillers代表引數名(可以是int,也可以是String根據業務選定)
				
				//檢視產品資訊頁面
				GET: /movies => method="index" 									常用於展示所有產品資訊頁面
				GET: /movies/Thrillers => method="show", id="Thrillers" 		常用於檢視某一個產品詳情
				
				//進入一個操作頁面(下面兩種方式等價)
					===>根據底層原始碼可以得到這樣一個公式:/movies/引數xxx/指定呼叫的action的方法名(GET請求 )
				GET: /movies/Thrillers;edit => method="edit", id="Thrillers" 	常用於編輯某個產品資訊(進入一個編輯的操作頁面)
				GET: /movies/Thrillers/edit => method="edit", id="Thrillers" 
				
				GET: /movies/new => method="editNew" 							常用於建立一個新產品(進入一個建立的操作頁面)
				POST: /movies => method="create" 								常用於建立一個新的產品
				
				//在操作頁面進行修改或刪除操作
				PUT: /movies/Thrillers => method="update", id="Thrillers" 		常用於編輯一個產品(進行了編輯操作)
				DELETE: /movies/Thrillers => method="destroy", id="Thrillers" 	常用於刪除一個產品(進行刪除操作)
				
				To simulate the HTTP methods PUT and DELETE, since they aren't supported by HTML, the HTTP parameter "_method" will be used. 

				===========================================根據resultCode(通常是Action方法返回的結果)返回那個頁面=======================================
				1.建立ResultTypeConfig有兩種方式:
						(1).從xml相關資源中createFromResources
						(2).從註解中createFromAnnotations
				//比如第(1)種通過配置:
						<constant name="struts.convention.result.path" value="/WEB-INF/content/"/>
				從當前專案的WEB-INF/content/目錄下尋找資源來構建result:
						(a).resultPath=/WEB-INF/content,
						(b).resultPrefix = /WEB-INF/content/movies
						(c).ResultConfigs(Map資料結構):
							{del
[email protected]
918059d, [email protected]1d5, [email protected]c9, [email protected]f2d46} 根據返回的resultCode來找到返回的檢視ResultConfig,比如返回的是"index", 可找到[email protected], 此物件中含有/WEB-INF/content/movies-index.jsp,可作為返回的頁面。 ====>通過原始碼可以得出這樣一個公式:"/WEB-INF/content/movies-index.jsp通過substring()方法擷取到index作為ResultConfigs的key(resultCode) 逆向反過來:通過resultCode可以構建出返回的頁面名稱,比如resultCode="abc",則movies-abc.jsp (movies為actionName)

3.專案結構圖 在這裡插入圖片描述

  1. 主程式碼檔案
有需要的可私信作者!

																  @author:拈花為何不一笑
  1. 專案演示
現在開始 >>>>>>

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

配置檔案說明 >>>>>>


	<!-- Restful相關常量Bean配置 -->
	
	<!--  通過掃描指定包(比如com.bs.action)後來確定包中那些類可以作為Action,這個配置指定以Controller結尾的類可作為Action。
		這與struts2中指定的請求url的字尾名完全是兩碼事,比如:/hello.action。
		rest風格的action構建與struts2風格差別比較大,rest風格構建acton有兩種方式:
		(1).一種是掃描指定包結合下面這個配置等資訊來構建ActionConfig
		(2).另一種是掃描指定包結合@Action註解等資訊來構建ActionConfig,這個需要配置常量bean:
		<constant name="struts.convention.action.mapAllMatches" value="true" />
		這個配置相當於一個開關,由它來確定是否使用@Action註解來構建ActionConfig。
	-->
	<constant name="struts.convention.action.suffix" value="Controller" />
	
	<!-- PackageBasedActionConfigBuilder類中指定defaultParentPackage:
		根據name得到value,把value作為引數賦值給defaultParentPackage。
		用於指定構建的action繼承的父包為:rest-default -->
	<constant name="struts.convention.default.parent.package" value="rest-default" /> 

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述