Struts2 - Action実装と配置
Actionクラスは、MVCモデルに置いてC(Controller)に該当する。
Actionクラスの実装
public class LoginAction { // field,setter,gettor public String execute() throws Exception { return "success"; } }
- Actionインタフェース : 開発規範ができる
import com.opensymphony.xwork2.Action; public class Login2Action implements Action { // field,setter,gettor @Override public String execute() throws Exception { return "success"; } }
ActionSupportクラス
- ActionSupportはデフォルトActionクラス。
- Action配置する際にclass属性を指定しないとActionSupportが処理する
import com.opensymphony.xwork2.ActionSupport; public class Login3Action extends ActionSupport {}
ActionからServletAPIアクセス
- ActionContextクラスを使う
// ActionContext対象 ActionContext ctx = ActionContext.getContext(); // ServletContext Map<String, Object> application = ctx.getApplication(); // HttpSession Map<String, Object> session = ctx.getSession(); // HttpServletRequst#getParameterMap()と似ている Map<String, Object> params = ctx.getParameters(); // HttpServletRequest#getAttribute(String name)と似ている Object obj2 = ctx.get("key2"); // 設定 ctx.setApplication(application); ctx.setSession(session);
ServletAPIのインスタンス取得
- ServletContextAware : ServletContext
- ServletRequesttAware : HttpServletRequest
- ServletResponseAware : HttpServletResponse
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.Action; public class Login2Action implements Action, ServletResponseAware { private HttpServletResponse response; @Override public void setServletResponse(HttpServletResponse arg0) { response = arg0; } @Override public String execute() throws Exception { return "success"; } }
ServletActionContext : 直接ServletAPIをアクセス
- static PageContext getPageContext()
- static HttpServletRequest getRequest()
- static HttpServletResponse getResponse()
- static ServletContext getServletContext()
Actionクラスの配置
<package.../>
- name : 必須、パッケージ名、他のパッケージから引用されるkey
- extend : 他のパッケージ継承
- namespace : 名前空間指定
- abstract : Action定義を含まない
<defalut-action-ref name="actionのname"/> : 対応するActionがみつからない際のAction
- <defalut-class-ref name="actionクラス"/> : class指定ない際のclass, sturts-defalut.xmlにActionSupportに設定
<action.../>
- name : 必須、action名、URLを処理する
- class: Actionクラス指定、省略の場合ActionSupportが処理する
- method : 実行メソッド
- ワイルドカード : name内で*を使い、class属性、method属性、<result.../>内で{1}で使用できる
<result.../>
- ローカル結果 : <action.../>の子要素、グルーバル結果より優先される
- グローバル結果 : <global-results.../>の子要素 、すべてのactionに有効
resultではOGNLの使用も可能。
name : ロジックVIEW名 (success,errorなど)
- type : 結果タイプ (デフォルトはdispatcher)
- chian
- dispatcher : JSP
- freemarker
- httpheader
- redirect
- redirectAction
- stream
- velocity
- xslt
- plainText
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 国際化ファイル --> <constant name="struts.custom.i18n.resources" value="message" /> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="false" /> <package name="sample" namespace="/" extends="struts-default"> <!-- すべてのactionのerrorに対応する --> <global-results> <result name="error">error.jsp</result> </global-results> <!-- すべてのaction対応 --> <action name="*"> <!-- action名.jspを返す --> <result>/WEB-INF/jsp/{1}.jsp</result> </action> <action name="login" class="lee.action.LoginAction" method="login"> <result name="error">/WEB-INF/jsp/error.jsp</result> <result name="success">/WEB-INF/jsp/welcome.jsp</result> </action> <action name="register" class="lee.action.LoginAction" method="register"> <result name="error">/WEB-INF/jsp/error.jsp</result> <result name="success">/WEB-INF/jsp/welcome.jsp</result> </action> <action name="ognl" class="lee.action.OGNLAction"> <!-- action内のkokyaku.IDをアクセスできる --> <result type="redirect">edit.action?id={kokyaku.ID}</result> </action> </package> </struts>
PreResultListner
action,interceptorに追加できるリスナー。
- actionが物理VIEWを返す前に実行。
- interceptor対応のすべてのactionに有効
ActionInvocation invocation = ActionContext.getContext().getActionInvocation(); invocation.addPreResultListener(new PreResultListener() { // 物理VIEWを表示する前に呼ばれる @Override public void beforeResult(ActionInvocation paramActionInvocation, String paramString) { paramActionInvocation.getInvocationContext().put("key1", "value1"); } });