Struts2 action標簽

2018-12-26 17:16 更新
action標簽允許開發(fā)者從視圖頁面執(zhí)行action。他們可以通過指定action名稱來實現(xiàn)此目的,將“executeResult”參數(shù)設置為“true”,以便直接在視圖中呈現(xiàn)結果。或者,他們可以將此參數(shù)設置為“false”,但使用action方法展現(xiàn)的request屬性。

創(chuàng)建action類

package cn.w3cschool.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;
   }
}

創(chuàng)建視圖

讓我們用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>
接下來創(chuàng)建包含以下內容的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="cn.w3cschool.struts2.HelloWorldAction" 
      method="execute">
      <result name="success">/HelloWorld.jsp</result>
   </action>
   <action name="employee" 
      class="cn.w3cschool.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”以創(chuàng)建WAR文件。然后在Tomcat的webapps目錄中部署WAR文件。最后,啟動Tomcat服務器并嘗試訪問URL http://localhost:8080/HelloWorldStruts2/employee.action,將顯示以下界面:
系統(tǒng)圖片
正如你在這個例子中可以看到的,我們將executeResult的值指定為“true”。因此,hello.action的結果直接在頁面中呈現(xiàn),HelloWorld.jsp打印彩虹的顏色(這些呈現(xiàn)在employee.jsp中)。
現(xiàn)在,讓我們稍微修改HelloWorldAction.java:
package cn.w3cschool.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”以創(chuàng)建WAR文件。然后在Tomcat的webapps目錄中部署WAR文件。最后,啟動Tomcat服務器并嘗試訪問URL http://localhost:8080/HelloWorldStruts2/employee.action,將顯示以下界面:
系統(tǒng)信息

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號