Struts 2-Action-Tag-Beispiel

Struts 2 Action Tag Beispiel

Laden Sie es herunter -Struts2-Action-Tag-Example.zip

Das Struts 2-Tag "action" wird verwendet, um die Aktionsklasse direkt von einer JSP-Seite aufzurufen. Wenn das Attribut "executeResult" auf "true" gesetzt ist, wird der Inhalt der Ergebnisseite direkt auf der aktuellen Seite gerendert.

Dies lässt sich am besten anhand eines vollständigen Beispiels veranschaulichen:

1. Aktion

Eine Action-Klasse mit wenigen Methoden, um das Ergebnis an eine andere Ergebnisseite weiterzuleiten.

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. Beispiel für ein Action-Tag

JSP-Seiten, auf denen die Verwendung des Tags "action" angezeigt wird. WennexecuteResult=”true” im Aktions-Tag angegeben ist, wird die Methode ausgeführt und die Ergebnisseite wird direkt angezeigt. Andernfalls wird nur die Methode ausgeführt. Es wird keine Ergebnisseite angezeigt.

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

Es wurden nur wenige Ergebnisnamen deklariert, um den Effekt vonexecuteResultzu demonstrieren.





   
   

    
        pages/action.jsp
    

    

        sayHello.jsp
        sayStruts2.jsp
        saySysOut.jsp