正如前面提到的,Struts提供了兩種形式的配置。傳統的方式是使用對所有配置struts.xml檔。到目前為止,我們已經看到了這樣的例子很多。 Struts配置的另一種方法是使用Java5注釋功能。使用Struts 注解,我們可以實現零配置。
要開始在你的專案中使用注釋,確保WebContent/WEB-INF/lib檔夾中的jar檔包括以下:
-
struts2-convention-plugin-x.y.z.jar
-
asm-x.y.jar
-
antlr-x.y.z.jar
-
commons-fileupload-x.y.z.jar
-
commons-io-x.y.z.jar
-
commons-lang-x.y.jar
-
commons-logging-x.y.z.jar
-
commons-logging-api-x.y.jar
-
freemarker-x.y.z.jar
-
javassist-.xy.z.GA
-
ognl-x.y.z.jar
-
struts2-core-x.y.z.jar
-
xwork-core.x.y.z.jar
現在,讓我們看看你如何能做到配置在struts.xml檔,取而代之的是注解。
Struts2注釋的概念的解釋,我們需要重新考慮我們的驗證為例說明在 Struts2的驗證 一章中。
在這裏,我們將採取一個例子,雇員Employee 將被捕獲的姓名和年齡使用一個簡單的頁面,我們將會把兩個驗證,以確保使用總是進入一個名字和年齡應該是在28和65之間。所以,讓我們先從主JSP頁面的例子。
創建主頁:
讓我們寫主JSP頁面檔index.jsp,這將被用來收集上述員工的相關資訊。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Employee Form</title> </head> <body> <s:form action="empinfo" method="post"> <s:textfield name="name" label="Name" size="20" /> <s:textfield name="age" label="Age" size="20" /> <s:submit name="submit" label="Submit" align="center" /> </s:form> </body> </html>
在index.jsp使用Struts的標籤,我們還沒有覆蓋,但我們將研究這些標籤相關的章節。但現在,假設s:textfield 標籤列印一個輸入字段 s:submit 列印一個提交按鈕。我們已經使用label屬性標籤,每個標籤每個標籤創建。
創建視圖:
我們將使用JSP檔的success.jsp將調用的情況下定義的動作返回SUCCESS。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Success</title> </head> <body> Employee Information is captured successfully. </body> </html>
創建動作:
這是將用於注釋的地方。讓我們重新定義行動Employee類的注釋,然後添加一個方法稱為validate() ,如下所示在Employee.java檔。請確保操作類擴展ActionSupport類,否則validate方法將不會被執行。
package com.zaixian.struts2; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.validator.annotations.*; @Results({ @Result(name="success", location="/success.jsp"), @Result(name="input", location="/index.jsp") }) public class Employee extends ActionSupport{ private String name; private int age; @Action(value="/empinfo") public String execute() { return SUCCESS; } @RequiredFieldValidator( message = "The name is required" ) public String getName() { return name; } public void setName(String name) { this.name = name; } @IntRangeFieldValidator(message = "Age must be in between 28 and 65", min = "29", max = "65") public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
在這個例子中,我們已經使用了一些注解。讓我逐個說明:
-
首先,我們已經Result注解。結果注解的結果是一個集合。結果注解下,我們有兩個結果注釋。結果注釋的名稱對應的執行方法的結果。它們還含有一個視圖應擔任相應的execute() 返回值的位置。
-
下一個注解是行動注解。這是用來修飾 execute()方法。操作方法也需要一個值,該URL上調用操作。
-
最後,使用兩個驗證的注解。已經配置了所需的字段驗證的年齡字段"name“字段和整數範圍驗證。也指定了自定義驗證消息。
配置檔:
我們不需要struts.xml 配置檔,讓我們刪除該檔,並讓我們檢查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> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-value> </init-param> </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/index.jsp。這會給出以下畫面:

現在不輸入任何所需資訊,只需點擊“Submit ”按鈕。將看到以下結果:

輸入所需的資訊,但輸入了錯誤的“From ”字段,讓我們說“test”和年齡為30名,最後點擊“Submit ”按鈕。將看到以下結果:

Struts 2的注釋類型:
Struts 2 應用程式可以使用Java5注釋作為替代XML和Java屬性配置。可以檢查最重要的注解涉及不同類別的列表: