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