Struts2零配置 Zero Config+CodeBehind
Zero Config能根據web.xml中配置的actionPackages自動掃描所有Action類,并猜測其NameSpace.
再利用CodeBehind猜測Result指向的jsp,實現了struts.xml的零配置(其實也不是完全沒有struts.xml,而是指struts.xml的內容不會隨action的增加而膨脹)
如果有特殊的結果指向(如redirect類型的結果),在Action處用@Result配置。
如有package級的配置(如使用非默認的Interceptor棧),仍在struts.xml中定義package,用@ParentPackage指定。
不過,目前ZeroConfig的Annotation較少,只有@Result、@ParentPackage,@NameSpace(java的package名不符合約定規則時使用),還有exception-Mapping之類的配置沒有包含。
1. ZeroConfig
在Web.xml 中設置ActionPackages ,則 Struts2會自動掃描這些Package下的Class,Class名含Action或擴展子ActionSupport的類都將被載入。
其中actionPackages的設置很有學問,比如 org.springside.miniweb.web, 則org.springside.miniweb.web.user.RoleAction,訪問路徑就會被自動的猜測為 /user/role.action
如果package名不符合這個規則,就需要自行設定NameSpace了,可以用Namespace annotation。又或者用ParentPackage annotation指定package,再在struts.xml中定義package的namespace.
| <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>org.springside.examples.miniweb.web</param-value> </init-param> </filter> |
在Action類中,用Annotation 對特殊的RELOAD返回值進行了注釋,其余的results就交給code-behind去猜了。
| @Results({ @Result(name=CRUDActionSupport.RELOAD, value="/role", type=ServletActionRedirectResult.class) }) public class RoleAction ... |
2. CodeBehind
指定JSP的默認目錄在/WEB-INF/jsp 下,原因就是希望保護jsp不能被直接打開,安全模塊只要保護Action的地址即可
| <constant name="struts.codebehind.pathPrefix" value="/WEB-INF/jsp/" /> |
1). 可以用 /user/login.action的URL 打開 /WEB-INF/jsp/user/login.jsp ,而LoginAction無需實際編寫。
2). 對于UserAction返回return SUCCESS,默認訪問/WEB-INF/jsp/user/user.jsp 或 user-success.jsp. 返回 “input”, 默認訪問/WEB-INF/jsp/user/user-input.jsp
3. 參數綁定– ModelDriven, Prepareable
無論是將Action中的變量渲染頁面中,或者從request中將內容回傳到Action中變量的過程,統稱參數綁定。
1). 最原始的Struts2會直接賦值Action中的變量。 如hello.action?id=1,會將action中的id屬性賦值。
2). 如果參數較多,而且都屬于同一個對象的,可以將所有屬性都放入一個對象中,比如hello.action?user.id 會為action中的User對象的id屬性賦值。
3). ModelDriven接口,如果不想寫太多”user.”前綴,如${user.id},可以實現ModelDriven接口的getModel函數, 返回user對象。則Struts2碰到{id}時,就會嘗試調用getModel() 獲得user對象再獲取其id屬性。
4). Prepareable接口,還有一種情況Hibernate常用的情況,一個對象可能有很多屬性(比如有10個屬性),但頁面上可能只顯示5個屬性的輸 入框。如果按上面的方法,先new一個User類,然后從頁面上賦值。保存此對象時就會將不在頁面上修改的5個屬性清空了。這時就需要兩次的 binding,一開始user變量為空,只綁定了action的id屬性,然后在prepare()函數中查出有完整10個屬性的對象,然后二次綁定時 再將頁面的那5個屬性復制到user對象中。
prepare()函數有兩種作用,一種專門為了二次binding,一種是作為公共的數據準備函數。但是,一個action內有多個method,不是 每一個method都需要執行prepare,比如list()方法,如果這種method較多,或者會造成沖突時,還有另外一種方式來定義二次 binding函數。比如prepareSave() 函數,就會默認的在執行save()前執行,此時,專門實現prePareMethodName() 方法再調度一個內部的prepare函數,而將prepare()函數留空。
4. Theme設定
雖然沒有用多少Struts2的taglib,但Struts2的Theme設計還是不錯的,在中,我們就需要更改輸出的格式,不以列表形式顯示。
1).在classpath的/template目錄中新增自己的theme目錄,如/template/mytheme,從struts2的默認simple theme中復制出actionmessage.ftl進行修改。
2).在jsp中使用新的theme
| <s:actionmessage theme="mytheme"/> |
如果需要默認都使用新的theme:
1).在classpath:/template/mytheme 中放置theme.properties,里面放一句parent = xhtml,即所有未重新實現的ftl,都使用xhtml默認的模板。
2).修改struts.xml,增加
| <constant name = "struts.ui.theme" value = "mytheme" /> |
5. 信息與異常顯示
1).配置使用store interceptor,可以在redirect頁面時,將信息存儲在session中.
| <interceptor-stack name="springSideStack"> <interceptor-ref name="store"> <param name="operationMode">AUTOMATIC</param> </interceptor-ref> <interceptor-ref name="paramsPrepareParamsStack" /> </interceptor-stack> |
2). 盡量使用addActionMessage來添加信息, 如果用addActionError會自動跳到input頁。
6. 取得Request 和 Response
HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionContext.getResponse();
?
總結
以上是生活随笔為你收集整理的Struts2零配置 Zero Config+CodeBehind的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery1.43源码分析之工具方法
- 下一篇: jdbc hibernate ibati