3
3

More than 5 years have passed since last update.

ValueStackへ格納する/ValueStackから表示する

Posted at

あまり使わないかもしれませんが、Struts2で値やオブジェクトを格納する領域にValueStackがあります。
Struts2内部で利用している格納領域なのですが、新設したインターセプタやActionSupportの代替となるクラスにて共通的に保管する情報を格納する際にも使えます。

ValueStackに格納したオブジェクトのフィールドをJSPで表示する際にはActionのフィールドを表示するときと同様に、

 <s:property value="フィールド名" />

で表示できます。

オブジェクトそのものを表示するわけではありません

例:Actionクラス

public class SampleAction extends ActionSupport {
@Action("")
    public String display() throws Exception {
        // ActionContextを取得し、ValueStackインスタンスを取得
        ActionContext context = ActionContext.getContext();
        ValueStack stack = context.getValueStack();

        // ValueStackへ格納するVOの用意
        CommonStackVO vo = new CommonStackVO();

        // コンテキストパスをValueStackへ格納
        HttpServletRequest request = ServletActionContext.getRequest();
        vo.setContextPath(request.getContextPath());
        stack.push(vo);

        return SUCCESS;
    }
}

ValueStackへ格納するVOの例( lombok を利用しています )

@Data
public class CommonStackVO {
    private String contextPath;
}

JSPはValueStackに格納したオブジェクトがもつフィールド名を指定します。

<s:property value="contextPath" />

コンテキストパスなのでリクエストごとにコンテキストパスを格納するのはよくありませんけどね…。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3