action標記允許程式員從檢視頁面執行動作。他們可以做到這一點通過指定的動作名稱。他們可以設定的“executeResult”引數設定為“true”,在檢視中直接呈現結果。或者他們可以將此引數設定為“false”,但利用請求屬性暴露的操作方法。
package com.yiibai.struts2; public class HelloWorldAction{ private String name; public String execute() throws Exception { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
讓我們具有 helloWorld.jsp 展示使用generator 標記:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World</title> </head> <body> <h2>Example of Generator Tag</h2> <h3>The colours of rainbow:</h3> <s:generator val="%{'Violet,Indigo,Blue, Green,Yellow,Orange,Red '}" count="7" separator=","> <s:iterator> <s:property /><br/> </s:iterator> </s:generator> </body> </html>
接下來讓我們有employees.jsp包含以下內容:
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Employees</title> </head> <body> <s:action name="hello" executeResult="true"> Output from Hello: <br /> </s:action> </body> </html>
struts.xml 應該像這樣:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.yiibai.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> <action name="employee" class="com.yiibai.struts2.Employee" method="execute"> <result name="success">/employee.jsp</result> </action> </package> </struts>
web.xml 應該像這樣:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
右鍵點選專案名稱,並單擊 Export > WAR File 建立一個WAR檔案。然後部署此WAR在Tomcat的webapps目錄下。最後,啟動Tomcat伺服器和嘗試存取URL http://localhost:8080/HelloWorldStruts2/employee.action。這會給出以下畫面:
正如可以看到在這個例子中,我們指定為“true”的值executeResult。因此,hello.action結果是直接在網頁上呈現。helloWorld.jsp列印彩虹的顏色 - 這是現在employee.jsp呈現
現在,讓我們稍微修改 HelloWorldAction.java:
package com.yiibai.struts2; import java.util.ArrayList; import java.util.List; import org.apache.struts2.ServletActionContext; public class HelloWorldAction{ private String name; public String execute() { List names = new ArrayList(); names.add("Robert"); names.add("Page"); names.add("Kate"); ServletActionContext.getRequest().setAttribute("names", names); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
最後,修改employee.jsp,如下所示:
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Employees</title> </head> <body> <s:action name="hello" executeResult="false"> Output from Hello: <br /> </s:action> <s:iterator value="#attr.names"> <s:property /><br /> </s:iterator> </body> </html>
再次右鍵點選專案名稱,並單擊Export > WAR File建立一個WAR檔案。然後部署此WAR在Tomcat 的 webapps目錄下。
最後,啟動Tomcat伺服器和嘗試存取URL http://localhost:8080/HelloWorldStruts2/employee.action。這會給出以下畫面: