Spring自帶的JPetStore分析(二)

2021-03-31 14:00:57 字數 1423 閱讀 7488

spring帶的jpetstore的struts mvc版

使用了多級的繼承體系,有baseaction,securebaseaction,baseactionform。這些父類將常用的功能進行了一些封裝,對寫實際的程式還是有參考價值的。

===baseaction===

繼承.apache.struts.action.action,其實.apache.struts.action.dispatchaction可能功能更強一些。所有的action都繼承了這個類。

private petstore

在所有的action中儲存了這個business object,封裝了所有的資料訪問的方法

過載了這個方法:

public void setservlet(actionservlet actionservlet)

struts會在建立action例項的時候自動執行這個方法。這個方法通過spring獲得petstorefacade介面的實現

增加了這個方法

protected petstorefacade getpetstore()

返回petstore物件,只有之類能訪問到這個方法

===securebaseaction===

這個類用於保護需要登入才能執行的操作。

在execute方法中:

檢查在session中的accountform物件,if (acctform == null || acctform.getaccount() == null),不存在或沒有登入則,儲存當前位址,跳轉到登入頁]

定義了乙個抽象方法,代替被本類使用了的execute方法。

protected abstract actionforward doexecute

===baseactionform===

這個類提供了乙個常用的form驗證的框架,在子類中只要實現驗證部分就可以了。

request.setattribute("errors", errorlist);將驗證後的結果儲存到request中。

這裡還提供了struts的錯誤處理,actionerrors.add(actionerrors.global_error, new actionerror("global.error"));

這個在新的struts版本中將不能使用,要用actionmessages。頁面可以直接用struts的taglib來顯示錯誤,不過這裡好像沒有將錯誤存入到actionerrors中去,只是使用了傳統的方式來處理錯誤,這裡並沒有使用struts的錯誤處理機制。

在includetop.jsp中顯示錯誤。

這個類還提供了乙個通用的方法:

protected void adderrorifstringempty(list errors, string message, string value);

檢查value是否為空,若空者將message加入到errors這個列表中。

Spring自帶的JPetStore分析(一)

spring mvc spring ibatis 或struts mvc spring ibatis 使用的是spring自帶的例子,與ibatis給出的例子還有有區別 安裝 我使用的環境是mysql,tomcat4 按照readme的說明編譯好war檔案後,根據兩個檔案建立資料庫表和插入資料,配置...

spring自帶快取機制

cacheable支援如下引數 負責將方法的返回值加入到快取中 value 快取位置名稱,不能為空.key 快取的key,預設為空,支援springel表示式.condition 觸發條件,只有滿足條件的情況下才會加入快取,預設為空,表示全部加入快取,支援springel表示式.cachevict支...

Spring自帶定時任務

spring自帶實現定時任務有兩種方式,一種是通過註解的方式實現,一種是通過在配置檔案中配置後實現。1.task命名空間 xmlns task xsi schemalocation spring task 3.1.xsd 2.xml檔案中配置 或直接使用註解 enablescheduling pub...