some tips from mailReader(struts2 's sample project):
Message Resources
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的國(guó)際化定義都可以寫在ActionSupport.properites,以到達(dá)減少資源文件的數(shù)目的目的。同時(shí),可以為在ActionSupport實(shí)現(xiàn)一些action的公共行為,比如對(duì)于web框架而言,session的處理是必需的,因此MailreaderSupport在這里實(shí)現(xiàn)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.
指定鏈接時(shí),使用到action的鏈接,而非到j(luò)sp的鏈接
好處:指定到action的鏈接,可以充分使用action的xwork攔截器的特性,比如在安全性驗(yàn)證、修改反饋的reponse方面都比較容易,而不用在jsp中進(jìn)行這些操作
ByPass the validate
有些情況下,不需要再進(jìn)行用戶輸入數(shù)據(jù)的驗(yàn)證,而是直接執(zhí)行action中的相關(guān)方法,比如取消按鈕(跳過validate),將直接返回首頁,這時(shí)候用戶輸入的信息對(duì)這個(gè)方法來說是無意義的。比如下面的情況:
<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.
取消按鈕按下時(shí)不需要進(jìn)行數(shù)據(jù)的合法性驗(yàn)證,因此可以設(shè)置為跳過validate,直接調(diào)用action的cancel方法
Other Tips
使用MedelDriven ?
使用modelDriven的好處在于可以直接將view的數(shù)據(jù)賦值到model對(duì)象中,省去了在action中逐個(gè)注入model屬性的過程,可以減少工作量,提高開發(fā)速度。但是對(duì)于view的界面與model屬性不一一對(duì)應(yīng)的情況,就比較麻煩。
其實(shí),根據(jù)modelDriven的實(shí)現(xiàn)方式,是將model放入堆棧的時(shí)候放置到action之上,因此model可以先于action得到注入屬性的機(jī)會(huì),所以完全可以將這兩種方式結(jié)合起來使用,即將與model可以直接對(duì)應(yīng)的屬性使用modelDriven注入,而不能直接對(duì)應(yīng)的注入到action中,在action中進(jìn)行處理后,注入model
使用基類,直接將aciton映射到j(luò)sp文件
<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的鏈接都會(huì)被映射到/pages/disease/下同名的jsp,其中 BaseSupport為所有action的基類
_input的含義
表示是初始輸入頁面,用戶第一次訪問該頁面,所以盡管輸入項(xiàng)都是空的,或者不合法的,但是也不應(yīng)該有輸入項(xiàng)的合法性驗(yàn)證提示信息出現(xiàn)。
OGNL、ActionContext Map、value statck 的關(guān)系
(1)OGNL is the Object Graph Navigation Language,OGNL工作的基礎(chǔ)是ActionContext map
(2)value Stack是ActionContext map的根對(duì)象,所以對(duì)于value statck中的對(duì)象,比如action、model對(duì)象,不需要使用"#" ,但是對(duì)于ActionContext map其他對(duì)象的訪問,需要使用"#"
|--application
|--session
ActionContext map---|
|--value stack(root)
|--request
|--parameters
|--attr (searches page, request, session, then application scopes)