some tips from mailReader(struts2 's sample project):
In Struts 2, message resources are associated with the Action class being processed. If we check the source, we find a language resource bundle named MailreaderSupport. MailreaderSupport is our base class for all the MailReader Actions. Since all of our Actions extend MailreaderSupport, all of our Actions can use the same resource bundle.
public class MailreaderSupport extends ActionSupport
在本例中提供了繼承了ActionSupport的MailreaderSupport類 ,以供其他action來繼承,這樣,所有的action的國際化定義都可以寫在ActionSupport.properites,以到達減少資源文件的數目的目的。同時,可以為在ActionSupport實現一些action的公共行為,比如對于web框架而言,session的處理是必需的,因此MailreaderSupport在這里實現SessionAware接口。
Link actions not pages:
Actions specify code that we want to be run before a page or other resource renders the response. An accepted practice is to never link directly to server pages, but only to logical action mappings. By linking to actions, developers can often "rewire" an application without editing the server pages.
指定鏈接時,使用到action的鏈接,而非到jsp的鏈接
好處:指定到action的鏈接,可以充分使用action的xwork攔截器的特性,比如在安全性驗證、修改反饋的reponse方面都比較容易,而不用在jsp中進行這些操作
ByPass the validate
有些情況下,不需要再進行用戶輸入數據的驗證,而是直接執行action中的相關方法,比如取消按鈕(跳過validate),將直接返回首頁,這時候用戶輸入的信息對這個方法來說是無意義的。比如下面的情況:
<s:submit action="Login_cancel" onclick="form.onsubmit=null"
key="button.cancel"/>
Here we are creating the Cancel button for the form. The button's attribute action="Login_cancel" tells the
framework to submit to the Login's "cancel" method instead of the usual "execute" method. The onclick="form.onsubmit=null" script defeats client-side validation. On the server side, "cancel" is on a special list of methods that bypass validation, so the request will go directly to the Action's cancel method. Another entry on the special-case list is the "input" method.
取消按鈕按下時不需要進行數據的合法性驗證,因此可以設置為跳過validate,直接調用action的cancel方法
Other Tips
使用MedelDriven ?
使用modelDriven的好處在于可以直接將view的數據賦值到model對象中,省去了在action中逐個注入model屬性的過程,可以減少工作量,提高開發速度。但是對于view的界面與model屬性不一一對應的情況,就比較麻煩。
其實,根據modelDriven的實現方式,是將model放入堆棧的時候放置到action之上,因此model可以先于action得到注入屬性的機會,所以完全可以將這兩種方式結合起來使用,即將與model可以直接對應的屬性使用modelDriven注入,而不能直接對應的注入到action中,在action中進行處理后,注入model
使用基類,直接將aciton映射到jsp文件
<struts>
<package name="disease" namespace="/disease" extends="struts-default">
.....
<action name="*" class="com.work.action.BaseSupport">
<result>/pages/disease/{1}.jsp</result>
</action>
<!-- Add actions here -->
</package>
</struts>
這樣,凡是指向disease/***.action的鏈接都會被映射到/pages/disease/下同名的jsp,其中 BaseSupport為所
有action的基類
_input的含義
表示是初始輸入頁面,用戶第一次訪問該頁面,所以盡管輸入項都是空的,或者不合法的,但是也不應該有輸入項的合法性驗證提示信息出現。
OGNL、ActionContext Map、value statck 的關系
(1)OGNL is the Object Graph Navigation Language,OGNL工作的基礎是ActionContext map
(2)value Stack是ActionContext map的根對象,所以對于value statck中的對象,比如action、model對象,不需要使用"#" ,但是對于ActionContext map其他對象的訪問,需要使用"#"
|--application
|--session
ActionContext map---|
|--value stack(root)
|--request
|--parameters
|--attr (searches page, request, session, then application scopes)