liguofeng29’s blog

個人勉強用ブログだっす。

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, Freemarkerftl, Velocity ⇒ vm)

サンプル

package lee.action;
public class LoginAction {
    public String execute() throws Exception {
        return "success";
    }
}

/WEB-INF/content/

の場合、

struts2-config-browser-pluginの表示。 f:id:liguofeng29:20160306002537p:plain

まとめ

  1. Action協定により、actionを検索する。⇒ LoginAction.java
  2. Actionクラス名からview名を決める ⇒ login
  3. Result協定により、viewを検索する。⇒ login-default.jsp,login-error.jsp,login-success.jsp
  4. loginアクションのResultは三つに決まる。

【Convention自動リロード】

<constant name="struts.convention.classes.reload" value="true" />

【Convention設定】

Convention Plugin

  • struts.convention.exclude.packages : Action検索除外パッケージ

  • struts.convention.result.path : デフォルト値は、「/WEB-INF/content:

  • 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に射影

など