[Struts 2]request,sessionおよびアプリケーションオブジェクトへのアクセス


サーブレットAPIとデカップリングされたアクセス方式
Structs 2は、HttpServertRequest,HttpSession,およびServertContextをカプセル化し、これら3つのオブジェクトの代わりに3つのMapオブジェクトを構築し、Actionでは、直接、HttpServertRequest,Httpsession,ServertContextに対応するMapオブジェクトを使用してデータを保存および読み出します.
この3つのMapオブジェクトを取得するにはcomを使用します.opensymphony.xwork2.ActionContextクラス.
ActionContextはaction実行のコンテキストであり、parameters、request、session、アプリケーション、localeなど、action実行に必要なオブジェクトのセットがActionContextに保存されている.
ActionContextクラスでは、getRequest()のような方法でHttpServertRequestがカプセル化されたMapオブジェクトを取得することはできません.Mapオブジェクトを要求するには、get()メソッドでパラメータ「request」を渡す必要があります.
public Object get(Object key)

public Map getSession()

public Map getApplication()

Action:
ActionContext context = ActionContext.getContext();
Map request=(Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();

request.put("greeting" "hello request");
session.put("user",user);

Integer count = (Integer)application.get("counter");


application.put("counter",count);

JSP:
<h3>${sessionScope.user.username},${requestScope.greeting}。</h3><br />
${applicationScope.counter}

要求オブジェクトを使用してデータを転送する方法は、ActionContextクラスのput()メソッドを直接利用してActionContextにデータを保存する方法もあります.
Actioncontext.getContext().put("greeting","hello ");
その後、結果ページでgreetingプロパティをリクエストオブジェクトから取り出します.
$(requestScope.greeting)  <%=request.getAttribute("greeting")%>

ActionContextに保存するデータは要求対象から得ることができ、その奥義はStruct 2のorgにある.apache.struts2.dispatcher.StrutsRequestWrapperクラス.このクラスはHttpServiceletRequestのパッケージクラスで、getAttribute()メソッドを書き換えています.このメソッドでは、オブジェクトにプロパティの検索を要求し、そうでなければActionContextから検索します.これがActionContextに保存されているデータがリクエスト対象から入手できる理由です.
ActionContextがrequest,session,applicationオブジェクトを取得する方法に加えて、Actionクラスは、Structs 2が実行時にActionインスタンスにrequest,session,applicationオブジェクトを注入する特定のインタフェースを実装することもできる.これに対応する3つのインタフェースと彼らの方法:
org.apache.struts2.interceptor.RequestAware
org.apache.struts2.interceptor.SessionAware
org.apache.struts2.interceptor.ApplicationAware
public class LoginAction implements Action,RequestAware,SessionAware,ApplicationAware{
private Map request;
private Map session;
private Map application;

// ......

@override
public void setRequest(Map request){
this.request=request;
}
@override
public void setSession(Map session){
this.session=session;
}
@override
public void setApplication(Map application){
this.application=application;
}

}

サーブレットAPIに結合されたアクセス方式
HttpServletRequestとServletContextオブジェクトを直接取得するにはorgを使用します.apache.struts2.サーブレットActionContextクラス.このクラスでは、2つの静的メソッドが定義されています.
public static HttpserlvetRequest getRequest()

public static HttpservletResponse getResponse()

pubolic static ServletContext getServletContext()

HttpSessionオブジェクトは、HttpServiceRequestオブジェクトから取得できます.
ActionContextオブジェクトのget()メソッドを呼び出し、サーブレットActionContextを渡すこともできます.HTTP_REQUESTとサーブレットActionContext.SERVLET_CONTEXTキー値を使用して、HttpServeretRequestオブジェクトとServeretContextオブジェクトを次のように取得します.
ActionContext.getcontext().get(ServletActionContext.HTTP_REQUEST);
ActionContext.getcontext().get(ServletActionContext.HTTP_RESPONSE);
ActionContext.getcontext().get(ServletActionContext.SERVLET_CONTEXT);
HttpServletResquest request  = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ServletContext context = ServletActionContext.getServletContext();
//application     
Integer count = (Integer)context.getAttribute("counter");

Actionクラスは、サーブレットActionContextを使用してHttpServertRequestオブジェクトおよびサーブレットContextオブジェクトを取得する方法に加えて、Struts 2によってActionインスタンスにHttpServertRequestおよびサーブレットContextオブジェクトを注入するサーブレットRequestAwareおよびサーブレットContextインタフェースを実装することもできる
ServiceletRequestAwareインタフェースとServiceletContextAwareインタフェースは同じパッケージではありません.前者はorgです.apache.structs2.interceptorパッケージでは、後者はorg.apache.struts2.utilパッケージにあります.
public class LoginAction implements Action,ServletRequestAware,ServletContextAware{
private HttpServletRequest request;
private ServletContext context;

//......

@override
public void setServletRequest(...){
//...
}

@override
public void setServletContext(...){
//...
}
}