struts.xml global-results 要素とは何か

struts2-core v2.3.24.1

struts.xml global-results 要素とは何か

action 間で共有したい result がある時、これを定義する為の要素

  • package ごとに global-results を定義できる
  • Struts2 は先ず ローカルの result(package.action.result)を走査する
    • ローカルで result が見つからない場合、global-results を走査する

(要素の設置順序が決まっているようなので設定ミスしないように)(X)

具体的に

或る package の result#name="error" を共通化する

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
  <constant name="struts.devMode" value="true" />
  <constant name="struts.ui.theme" value="simple" />
  <package name="top" extends="struts-default">
    <global-results>
      <result name="error">/error.jsp</result>
    </global-results>
    <action name="index" class="jp.ymatsukawa.top.Index">
      <result name="success">/index.jsp</result>
    </action>
  </package>
</struts>

action 要素の前に global-results を設定する

global-results の下に共通化したい result を設定する

global-results.result を action.result で上書きできるか

できる

上の例より result#name="error" の時

/error.jsp ではなく, /myError.jsp を render させたい場合は

下のように設定する

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
  <constant name="struts.devMode" value="true" />
  <constant name="struts.ui.theme" value="simple" />
  <package name="top" extends="struts-default">
    <global-results>
      <result name="error">/error.jsp</result>
    </global-results>
    <action name="index" class="jp.ymatsukawa.top.Index">
      <result name="error">/myError.jsp</result>
      <result name="success">/index.jsp</result>
    </action>
  </package>
</struts>

(X)要素の設定順序に注意

action の前に global-results を設定する

要素は上から順に以下の通りである

result-types?,
interceptors?,
default-interceptor-ref?,
default-action-ref?,
default-class-ref?,
global-results?,
global-allowed-methods?,
global-exception-mappings?,
action*

[参考元]

https://struts.apache.org/docs/result-configuration.html#ResultConfiguration-GlobalResults

http://stackoverflow.com/questions/3742379/struts2-global-results-configuration-error