Struts 2アクションタグの例

Struts 2アクションタグの例

ダウンロード–Struts2-Action-Tag-Example.zip

Struts 2の「action」タグは、JSPページから直接アクションクラスを呼び出すために使用されます。 「executeResult」属性がtrueに設定されている場合、結果ページのコンテンツは現在のページに直接レンダリングされます。

これは完全な例を使用して最もよく説明されています:

1. アクション

結果を別の結果ページに転送するメソッドがほとんどないActionクラス。

ParamTagAction.java

package com.example.common.action;

import com.opensymphony.xwork2.ActionSupport;

public class ActionTagAction extends ActionSupport{

    public String execute() {
        return SUCCESS;
    }

    public String sayHello(){
        return "sayHello";
    }

    public String sayStruts2(){
        return "sayStruts2";
    }

    public String saySysOut(){
        System.out.println("SysOut SysOut SysOut");
        return "saySysOut";
    }

}

2. アクションタグの例

action」タグの使用法を示すJSPページ。 アクションタグにexecuteResult=”true”が指定されている場合、メソッドが実行され、結果ページが直接表示されます。それ以外の場合は、メソッドを実行するだけで、結果ページは表示されません。

action.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>





Struts 2 action tag example

  1. Execute the action's result, render the page here.
  2. Doing the same as above, but call action's sayStruts2() method.
  3. Call the action's saySysOut() method only, no result will be rendered, By defautlt, executeResult="false".

sayHello.jsp






Hello Hello Hello ~ from sayHello.jsp

sayStruts2.jsp






Struts 2 Struts 2 Struts 2 ~ from sayStruts2.jsp

saySysOut.jsp






SysOut SysOut SysOut ~ from saySysOut.jsp

3. struts.xml

executeResult効果を示すために、いくつかの結果名を宣言しました。




   
   

    
        pages/action.jsp
    

    

        sayHello.jsp
        sayStruts2.jsp
        saySysOut.jsp