標籤:
一.下載struts2.0.1http://struts.apache.org/downloads.html,下載struts-2.0.1-all.zip,這個壓縮包中包含了開發struts2所需的struts2-core.jar核心包以及其它struts2所依賴的JAR檔案,另外還有一些struts2的樣本程式以及一些HTML的API文檔。二.試用struts2.0.1 1. 建立一個WEB工程,將struts-2.0.1-all.zip壓縮包中的lib目錄下的所有jar檔案拷貝到WEB工程的/WEB-INF/lib目錄下。修改WEB-INF下的web.xml檔案,加入如下內容:<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> <!-- 這裡是設定struts2標籤,也可以不用設定,因為在struts-core.jar的META-INF目錄下已經包含了 這個tld檔案,J2EE容器會自動地載入它 --> <jsp-config> <taglib> <taglib-uri>/s</taglib-uri> <taglib-location> /WEB-INF/tlds/struts-tags.tld </taglib-location> </taglib> </jsp-config>在web.xml中定義了一個struts2的FilterDispathcer的filter,這個FilterDispatcher用來初始化struts2並且處理所有的WEB請求。 2. 建立一個登入頁面login.jsp:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%><html><head><title>登入頁面</title></head><body><s:form action="login"> <table align="center"> <caption><h3>使用者登入</h3></caption> <tr> <td><s:textfield label="使用者名稱" name="username" /></td> </tr> <tr> <td><s:password label="密 碼" name="password" /></td> </tr> <tr align="center"> <td><input type="submit" value="登入"/></td><td><input type="reset" value="重填" /></td> </tr> </table></s:form></body></html>3.編寫Action login:package org.rainlife.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private String username; private String password; @Override public String execute() throws Exception { if (!(getUsername().equals("rainlife")) && !(getPassword().equals("rainlife"))) { return ERROR; } else { return SUCCESS; } } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }}在這個LoginAction類中,繼承了ActionSupport。ActionSupport 是xwork2這個開源架構中的一個讓action能夠更加快速地工作的基類,它包含了action中許多可選服務的預設實現,可以讓我們更加容易地自訂一個action。在這裡我們定義了username和password兩個屬性並提供了相應的get/set方法。並且定義了一個execute()方法,該方法覆蓋ActionSupport類中的execute()方法,可以看到,它只是簡單地返回一個字串(”SUCCESS”或”INPUT”,而不像是在struts1中的返回一個ActionForward,這兩個字串也是在ActionSupport中定義的,在ActionSupport中定義了四個String屬性,分別為SUCCESS,INPUT,ERROR,LOGIN。這樣,我們的action就已經完成了,但還存在一個問題,怎麼樣讓struts2知道我們這個自訂的action,並且可以在HTML/JSP頁面中將動作提交給action呢?答案是需要配置struts.xml檔案。 4.配置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> <package name="struts2" extends="struts-default"> <action name="login" class="org.rainlife.struts2.action.LoginAction"> <result name="error">/error.jsp</result> <result name="success">/success.jsp</result> </action> </package></struts>在這個struts.xml設定檔中,可以發現和以前的struts-config.xml已經完全不一樣了,而在webwork的設定檔非常相似。在這裡,我們定義一個名name=”login”的action,通過class屬性指向了剛才我們建立的LoginAction類,這樣,就將我們定義的action告訴給了struts2。而在HTML/JSP頁面中,可以通過這個”login”這個name來將動作提交給相應的Action。如果在package中設定了namespace屬性,如namespace=”/struts2”,則在JSP頁面中應該將Form的action設定為”/struts2/login.action”。 5.建立error.jsp和success.jsp:error.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html> <head> <title>錯誤頁面</title> </head> <body> 您不能登入! </body></html>success.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html> <head> <title>成功頁面</title> </head> <body> 您已經登入! </body></html>Struts2會根據在LoginAction中返回的字串(ERROR或SUCCESS)來和在struts.xml中<result>中的name屬性所定義的字串匹配,並跳轉到相應頁面。
Eclipse搭建Struts架構,及一個簡單的Struts例子