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
首先,先開始寫用來收集上面提到的Employee相關信息的主頁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標簽我們還沒有涉及到,不過將在標簽相關章節(jié)中學習它們?,F(xiàn)在,假設s:textfield標簽印出一個輸入字段,并且s:submit印出一個提交按鈕。我們?yōu)槊總€標簽使用了label屬性,即為每個標簽創(chuàng)建標記。
我們將使用JSP文件的success.jsp,在定義的action返回SUCCESS的情況下調(diào)用它。
<%@ 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>
Action是使用注釋的地方。讓我們重新定義具有注釋的action類Employee,然后如下所示在Employee.java文件中添加一個名為validate()的方法。需要確保action類擴展了ActionSupport類,否則將不會執(zhí)行validate方法。
package cn.w3cschool.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; } }
我們在這個例子中使用了一些注釋,讓我們逐個了解一下:
首先是例子中包含的Results注釋,Results注釋是結(jié)果的集合。在這個注釋集合下,有兩個結(jié)果注釋。它們包含對應于execute方法結(jié)果的名稱,還包含對應于execute()的返回值所提的視圖位置。
下一個注釋是Action注釋,這可用于裝飾execute()方法。Action方法還接收一個值,該值是調(diào)用action的URL。
最后,我們使用了兩個validation注釋。我們已經(jīng)配置了必填字段驗證器上的name字段和整數(shù)范圍驗證器上的age字段,此外還為驗證指定了自定義的信息。
實際上我們真的不需要struts.xml配置文件,可以刪除這個文件,直接來查看web.xml文件的內(nèi)容:
<?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>
現(xiàn)在,右鍵單擊項目名稱,然后單擊“Export”> “WAR File”以創(chuàng)建WAR文件。然后在Tomcat的webapps目錄中部署WAR文件。最后,啟動Tomcat服務器并嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp,將顯示以下界面:
先不要輸入任何所需的信息,只點擊“Submit”按鈕。你將看到以下結(jié)果:
輸入所要求的信息而非錯誤類的字段,如名稱為“text”,年齡為30,然后點擊Submit按鈕,可以看到以下界面:
Struts2 應用程序可以使用Java5注釋來替代XML和Java屬性的配置。你可以查看與不同類別相關的最重要注釋的列表:Struts2 注釋類型。
更多建議: