鍍金池/ 教程/ Java/ 注釋
注釋
主題/模板
驗證
有用的資源
Struts 2 注解類型
實例
攔截器
異常處理
表單標簽
結(jié)果類型
值棧/OGNL
Spring 集成
數(shù)據(jù)標簽
環(huán)境配置
配置
類型轉(zhuǎn)換
動作
Hibernate 集成
本地化
發(fā)送郵件
Ajax 標簽
數(shù)據(jù)庫訪問
體系結(jié)構(gòu)
文件上傳
Tiles 集成
概述
基本的 MVC 架構(gòu)
控制標簽

注釋

正如前面所提到的,Struts 提供了兩種形式的配置。傳統(tǒng)的方式是為所有的配置使用 struts.xml 文件。目前為止,在本教程中我們已經(jīng)見過了太多這樣的例子。另一種配置 Struts 的方式是使用 Java 5 注釋功能。使用 struts 注釋,我們可以實現(xiàn) 零配置。

在你的項目中開始使用注釋之前,請確保在你的 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

現(xiàn)在讓我們看看不用 struts.xml 文件中的可用的配置而是用注釋代替,應(yīng)該怎么做。

為了解釋 Struts2 中只是的概念,我們應(yīng)該重新考慮在 Struts 2 - 驗證 章節(jié)中解釋的驗證示例。

這里我們采用 Employee 的例子,它使用一個簡單的頁面來獲取姓名和年齡,并且我們會設(shè)置兩個驗證來確保用戶會輸入姓名以及年齡在 28-65 之間。所以讓我們從例子中的主 JSP 頁面開始。

創(chuàng)建主頁面

讓我們編寫主頁面 JSP 文件 index.jsp,用于與上述信息相關(guān)的 Employee。

<%@ 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 標簽,Struts 標簽我們還沒有涉及到,但是在標簽相關(guān)的章節(jié)中我們會學(xué)習(xí)到。但是現(xiàn)在,只能假設(shè) s:textfield 標簽產(chǎn)生一個輸入字段,s:submit 標簽產(chǎn)生一個提交按鈕。我們已經(jīng)為每個標簽使用了標記屬性,會為每個標簽創(chuàng)建標記。

創(chuàng)建視圖

我們使用 JSP 文件 success.jsp,當定義的操作返回 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>

創(chuàng)建操作

這是注釋會被用到的地方。讓我們用注釋重新定義操作類 Employee,然后將如下所示的 validate() 方法添加到 Employee.java 文件中。確保你的操作類擴展了 ActionSupport 類,否則你的驗證方法不會被執(zhí)行。

package com.tutorialspoint.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;
   }
}

在這個例子中我們已經(jīng)使用了幾個注釋。讓我們一個一個的仔細瀏覽一下:

  • 首先,我們包含了 Results 注釋。Results 注釋時結(jié)果的集合。在結(jié)果注釋下,我們有兩個結(jié)果注釋。結(jié)果注釋有名字,對應(yīng)于該執(zhí)行方法的輸出。它們還包括視圖被服務(wù)的位置,對應(yīng)于從 execute() 返回的值。

  • 下一個注釋是 Action 注釋。這是用于裝飾 execute() 方法的。操作方法帶有一個值,該值是這個操作被調(diào)用的 URL。

  • 最后,我使用了兩個 validation 注釋。我已經(jīng)在 name 字段配置了必需的字段驗證器,并且在 age 字段配置了整數(shù)范圍驗證器。我還為驗證指定了一個自定義的消息。

配置字段

我們實在不需要 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 文件。然后將這個 WAR 文件部署到 Tomcat 的 web 應(yīng)用程序的目錄中。最后,啟動 Tomcat 服務(wù)器并嘗試訪問 URL http://localhost:8080/HelloWorldStruts2/index.jsp. 這將為你呈現(xiàn)如下所示的畫面:

http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts12.jpg" alt="" />

現(xiàn)在不要鍵入任何必需的信息,只是點擊提交按鈕。你會看到如下所示的結(jié)果:

http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts121.jpg" alt="" />

鍵入必需的信息,但是輸入一個錯誤的表單字段,如姓名輸入為 "test",年齡輸入為 30,然后點擊 提交按鈕。你會看到如下所示的結(jié)果:

http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts122.jpg" alt="" />

Struts 2 注釋類型

Struts 2 應(yīng)用程序可以使用 Java 5 注釋作為 XML 和 Java 屬性配置的可選項。你可以查看與不同類別相關(guān)的最重要的注釋列表:

Struts 2 注釋類型

4j:struts_annotations_types 這頁要翻譯,直接加在下面,不用單獨一頁。

上一篇:異常處理下一篇:體系結(jié)構(gòu)