第一節 需求定義
簡單的需求如下圖
1 用戶處在Welcome.jsp 頁面,點擊登錄連接到達Login.jsp頁面
2 在Login.jsp輸入用戶名密碼,點擊登陸按鈕,轉到LoginAction處理
3.1 在登陸成功的情況下,再回到Welcome.jsp頁面,在頁面上顯示登錄信息
3.2 在登陸失敗的情況下,返回到Login.jsp顯示登陸錯誤信息
|

第二節 使用maven建立基本開發結構
使用如下命令建立Struts2 應用程序框架結構
mvn archetype:create
-DgroupId=com.jpleasure
-DartifactId=login
-DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-starter
-DarchetypeVersion=2.0.5-SNAPSHOT -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository
目錄說明
生成的Struts2開發框架目錄滿足一般的maven項目,主要由以下目錄組成:
src
├─main 源代碼目錄
│ ├─java java代碼
│ ├─resources 資源文件等
│ └─webapp Web目錄
│ ├─jsp JSP目錄
│ ├─styles CSS目錄
│ └─WEB-INF WEB-INF目錄
└─test 測試代碼目錄
├─java java代碼
└─resources 資源文件等
重要文件:
src\main\resources
applicationContext.xml Spring配置文件
log4j.properties log4j配置文件
struts.properties struts參數文件
struts.xml struts配置文件
xwork-conversion.properties xwork參數文件
src\main\webapp\WEB-INF
decorators.xml
dwr.xml DWR配置文件
sitemesh.xml SiteMesh配置文件
web.xml Web部署描述文件
進入工程目錄(login目錄)使用如下命令建立Eclipse工程文件
mvn eclipse:eclipse
使用如下命令打包工程
mvn package
應用程序打包完成之后可以再login\target目錄中看到login.war文件,這個文件就是最終的成果文件
使用如下命令運行應用程序
mvn jetty:run
也可以將login.war拷貝到tomcat的webapps目錄下來運行struts2應用程序。
打開IE在瀏覽器中輸入:
http://localhost:8080/login
可以看到如下畫面

說明自動生成的框架已經可以運行。
第三節 補充Login功能
下面我們來完成login功能
首先將login工程導入到Eclipse中
之后建立在src\main\java目錄中建立LoginAction類,代碼清單如下:
package com.jpleasure.login.action;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
public class LoginAction extends ActionSupport {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String forward() throws Exception {
return SUCCESS;
}
@Override
public String execute() throws Exception {
if("admin".equals(name)) {
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("name", name);
return SUCCESS;
} else {
return INPUT;
}
}
}
Action具有以下特點:
1 從ActionSupport繼承而來。
2 包含一些屬性和getter,setter方法,這些屬性用來保存客戶端傳遞到服務器的數據,或者用來作為在JSP上顯示的數據的源。
3 有一些返回String類型,無參數的方法,在LoginAction中是execute方法forwardLogin方法,這些方法就是Action需要執行的方法。
SUCCESS和INPUT都在com.opensymphony.xwork2.Action類中定義,類型為String,分別用來表示成功和輸入結果。
在src\main\webapp中建立Welcome.jsp和Login.jsp兩個JSP文件
Welcome.jsp代碼清單如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome</title>
</head>
<body>
<s:url action="forwardLogin" id="forwardLoginUrl"></s:url>
<s:a href="%{forwardLoginUrl}">Forward Login</s:a>
<%
if(session.getAttribute("name") != null){
out.println("<br/> login success.");
}
%>
</body>
</html>
Login.jsp 代碼清單如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<s:url action="login" id="loginUrl"></s:url>
<s:form action="%{loginUrl}">
<s:textfield label="Name" name="name"/>
<s:password label="Password" name="password" />
<s:submit></s:submit>
</s:form>
</body>
</html>
這些JSP有如下特點:
1 有一個特殊的taglib聲明,<%@taglib prefix="s" uri="/struts-tags" %>這是struts2的標準taglib聲明。
2 一些s標簽
<s:url action="forwardLogin" id="forwardLoginUrl"></s:url>定義了一個叫做forwardLoginUrl的指向forwardLogin的Action的路徑。
同理:<s:url action="login" id="loginUrl"></s:url>定義了一個叫做loginUrl指向loginaction的Action的路徑。
<s:a href="%{forwardLoginUrl}">Forward Login</s:a>定義了一個指向forwardLogin的鏈接,其中href使用了上面定義的內容。
<s:form action="%{loginUrl}">
<s:textfield label="Name" name="name"/>
<s:password label="Password" name="password" />
<s:submit></s:submit>
</s:form>
定義了一個向login Action提交的form,這個Form提交name和password兩個字段,這個Form的action指向使用了前面定義的loginUrl變量。
配置src\main\resources\struts.xml 文件
在默認的package中添加如下內容
<package name="myPackage" extends="struts-default">
<!-- login begin -->
<action name="forwardLogin" method="forward"
class="com.jpleasure.login.action.LoginAction">
<result>/Login.jsp</result>
</action>
<action name="login" class="com.jpleasure.login.action.LoginAction">
<result name="input">/Login.jsp</result>
<result>/Welcome.jsp</result>
</action>
<!-- login end -->
…
</package>
每一個action表示一種向服務器的請求,name屬性定了了action的名字,class是action對應的服務器端執行的Action類的名字,method是類中的被執行的方法的名字。
Result表示默認的結果(SUCCESS的結果),result可以添加name屬性來表示對應的返回結果,例如:input
為了讓應用程序開始的時候能夠顯示Welcome.jsp我們必須修改src\main\webapp\WEB-INF\web.xml,將其中的的welcome-file改為:
<welcome-file-list>
<welcome-file>Welcome.jsp</welcome-file>
</welcome-file-list>
第三節 詳細描述Login的功能
當我們使用IE訪問login程序的時候,其實我們是向服務器發送了一個如下的請求:
http://localhost:8080/login
當應用服務器收到請求的時候,根據/login可以判斷你所訪問的應用程序是login,之后根據web.xml中定義的welcome-file找到默認的歡迎畫面:Welcome.jsp
Welcome.jsp表示的過程中建立了一個如下的鏈接:
<a href="/login/forwardLogin.action">Forward Login</a>
我們點擊這個鏈接的時候向服務器提交了一個如下的請求:
http://localhost:8080/login/forwardLogin.action 的請求
Struts2框架根據/forwardLogin.action找到對應的Action配置描述(Struts2框架會自動去掉.action后綴,之后進行匹配查找操作):
<action name="forwardLogin" method="forward"
class="com.jpleasure.login.action.LoginAction">
<result>/Login.jsp</result>
</action>
使用LoginAction類的forward方法處理,當forward方法返回SUCCESS的時候,返回給客戶端Login.jsp頁面。
Login.jsp表示的過程中建立了一個如下的Form:
<form id="login" onsubmit="return true;" action="/login/login/login.action" method="POST">
<table class="wwFormTable">
<tr>
<td class="tdLabel"><label for="login_name" class="label">Name:</label></td>
<td><input type="text" name="name" value="" id="login_name"/></td>
</tr>
<tr>
<td class="tdLabel"><label for="login_password" class="label">Password:</label></td>
<td><input type="password" name="password" id="login_password"/></td>
</tr>
<tr>
<td colspan="2">
<div align="right"><input type="submit" id="login_0" value="Submit"/></div>
</td>
</tr>
</table>
</form>
當點擊Submit按鈕提交Form的時候,name和password輸入分別被提交,這個Form提交的路徑是:/login/login/login.action
根據如下Action配置:
<action name="login" class="com.jpleasure.login.action.LoginAction">
<result name="input">/Login.jsp</result>
<result>/Welcome.jsp</result>
</action>
Struts2框架找到對應的LoginAction,同時將提交上來的name和password賦值給LoginAction實例,因為沒有說明login調用的方法,所以Struts框架調用默認的方法:execute
ExtJS教程-
Hibernate教程-
Struts2 教程-
Lucene教程