Struts2 - Convention(協定)
Struts2.1からConventionプラグインを使い、strtus.xml,Annotionを使わず配置ができる。
【Actionの検索と射影の協定】
Action検索対象のパッケージ
パッケージ内容のActionクラス
- com.opensymphony,xwork2.Action実装
- XxxxAction.java
Action射影協定
- org.samlpe.action.LoginAction ⇒ /login
- org.samlpe.actions.Search(Action実装) ⇒ /search
- org.samlpe.struts.xxx.LoginAction ⇒ /xxx/loginに射影
- org.samlpe.strtus2.xxx.yyy.Login ⇒ /xxx/yyy/loginに射影
【Resultの射影の協定】
デフォルトVIEW位置
- WEB-INF/content
VIEW名
- actionName + resultCode + suffix
- ↑なければ、actionName + suffix
org.samlpe.action.LoginActionがsuccess時 ⇒ 1. /WEB-INF/content/login-success.jsp 2. /WEB-INF/content/login.jsp
※ resultのタイプにより、拡張子は変わる。
(Dispatcher ⇒ jps, Freemarker ⇒ ftl, Velocity ⇒ vm)
サンプル
package lee.action; public class LoginAction { public String execute() throws Exception { return "success"; } }
/WEB-INF/content/
の場合、
struts2-config-browser-pluginの表示。
まとめ
- Action協定により、actionを検索する。⇒ LoginAction.java
- Actionクラス名からview名を決める ⇒ login
- Result協定により、viewを検索する。⇒ login-default.jsp,login-error.jsp,login-success.jsp
- loginアクションのResultは三つに決まる。
【Convention自動リロード】
<constant name="struts.convention.classes.reload" value="true" />
【Convention設定】
struts.convention.exclude.packages : Action検索除外パッケージ
struts.convention.result.flatLayout : action対応のパッケージ内
struts.convention.action.packages : rootパッケージとしてaction検索
- struts.convention.action.suffix : デフォルト値は「Action」
struts.convention.action.separator : デフォルト値は「-」
struts.convention.exclude.locators : Actionのルートディレクトリ。actions.sample.LoginActionは協定上、/sample/loginだが、定数をsampleにすると/loginに射影
など