1.如何安裝struts:
首先到http://jakarta.apache.org/struts下載struts,建議使用release版,現在最高版本為1.2.6,有多種os版本(windows,linus...),下載后解壓開來,可以看到這個目錄:lib和webapps,webapps下有一些war文件。假設你的tomcat裝在c:tomcat下,則將那些war文件拷貝到c:tomcatwebapps,重新啟動tomcat即可。打開瀏覽器,在地址欄中輸入:http://localhost:8080/struts-example/index.jsp,若能見到“powered by struts”的深藍色圖標,即說明成功了。這是struts自帶的一個例子,附有詳細的說明文檔,可以做為初學者的入門教程。另外,struts還提供了一系統實用對象:xml處理、通過java reflection apis自動處理javabeans屬性、國際化的提示和消息等
2.練習做一個實例:
    一個用戶注冊系統,用戶通過網頁輸入相關信息:注冊id號,密碼,email,若注冊成功,則返回成功提示信息,反之出現注冊失敗提示信息。 
以下是相關文件的部分核心代碼。 
項目建立:
  正式開發前,需要在tocmat(我的tomcat裝在c: omcat)中建立此項目。比較快的一種建立方式為:在c: omcatwebapps下新建目錄test,再將c: omcatwebappsstruts-example下的 
web-inf目錄拷貝到test目錄下,然后將testweb-inf下的src和classes目錄清空,以及struts-config.xml文件中內容清空即可。這樣,我們需要的struts類包及相關的配置文件就都齊了。開發時,將jsp文件放在test目錄下,java原文件放在testweb-infsrc下,編譯后的類文件放在testweb-infclasses下。 
注冊頁面:reguser.jsp
<%@ page contenttype="text/html;charset=utf-8" language="java" %> 
<%@ taglib uri="/web-inf/struts-bean.tld" prefix="bean" %> 
<%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %> 
<html:html locale="true"> 
<head> 
<title>reguser</title> 
<html:base/> 
</head> 
<body bgcolor="white"> 
<html:errors/> 
<html:form action="/reguseraction" focus="logname"> 
<table border="0" width="100%"> 
<tr> 
<th align="right"> 
logname: 
</th> 
<td align="left"> 
<html:text property="logname" size="20" maxlength="20"/> 
</td> 
</tr> 
<tr> 
<th align="right"> 
password: 
</th> 
<td align="left"> 
<html:password property="password" size="20" maxlength="20"/> 
</td> 
</tr> 
<tr> 
<th align="right"> 
e-mail: 
</th> 
<td align="left"> 
<html:password property="email" size="30" maxlength="50"/> 
</td> 
</tr> 
<tr> 
<td align="right"> 
<html:submit property="submit" value="submit"/> 
</td> 
<td align="left"> 
<html:reset/> 
</td> 
</tr> 
</table> 
</html:form> 
</body> 
</html:html> 
此jsp頁面不同于普通的jsp頁,因為它大量運用了taglib,這些taglib對初學者而言,可能難于掌握,可這卻是struts的精華之一。靈活運用,將大大提高開發效率。
struts-config.xml:
<struts-config> 
<form-beans> 
<form-bean name="reguserform" 
type="org.cjea.struts.example. reguserform "/> 
</form-beans> 
<action-mappings> 
<action path="/reguseraction" 
type=" org.cjea.struts.example.reguseraction " 
attribute=" reguserform " 
scope="request" 
validate="false"> 
<forward name="failure" path="/ messagefailure.jsp"/> 
<forward name="success" path="/ messagesuccess.jsp"/> 
</action> 
</action-mappings> 
</struts-config> 
struts的核心是controller,即actionservlet,而actionservlet的核心就是struts-config.xml,struts-config.xml集中了所有頁面的導航定義。對于大型的web項目,通過此配置文件即可迅速把握其脈絡,這不管是對于前期的開發,還是后期的維護或升級都是大有裨益的。掌握struts-config.xml是掌握struts的關鍵所在。
formbean:reguserform
package org.cjea.struts.example;
import javax.servlet.http.httpservletrequest; 
import org.apache.struts.action.actionform; 
import org.apache.struts.action.actionmapping; 
public final class reguserform extends actionform{
private string logname; 
private string password; 
private string email; 
public reguserform(){ 
logname = null; 
password = null; 
email = null; 
} 
public string getlogname() { 
return this.logname; 
} 
public void setlogname(string logname) { 
this.logname = logname; 
} 
public void setpassword(string password) { 
this.password = password; 
} 
public string getpassword() { 
return this.password; 
} 
public void setemail(string email) { 
this.email = email; 
} 
public string getemail() { 
return this.email; 
} 
public void reset(actionmapping mapping, httpservletrequest request) 
{ 
logname = null; 
password = null; 
email = null; 
} 
} 
每一個formbean 都必須繼承actionform類,formbean是對頁面請求的封裝。即把http request 封裝在一個對象中,需要說明的一點就是多個http request可以共用一個formbean,便于維護和重用。
actionbean:reguseraction
package org.cjea.struts.example;
import javax.servlet.http.*; 
import org.apache.struts.action.*; 
public final class reguseraction extends action 
{ 
public actionforward perform(actionmapping mapping, 
actionform form, httpservletrequest req, 
httpservletresponse res) 
{ 
string title = req.getparameter("title"); 
string password = req.getparameter("password"); 
string email = req.getparameter("email"); 
/* 
取得用戶請求,做相應數據庫操作,略 
*/ 
} 
} 
formbean的產生是為了提供數據給actionbean,在actionbean中可以取得formbean中封裝的數據,經相應的邏輯處理后,調用業務方法完成相應業務要求。
servlet的演變:在常規的 jsp,servlet,javabean三層結構中,jsp實現view的功能,servlet實現controller的功能,javabean實現model的實現。
在struts中,將常規情況下的servlet拆分與actionservlet、formbean、actionbean三個部分。actionservlet配合struts-config.xml,專職完成頁面導航,而不再負責具體的數據獲取與相應邏輯,這兩部分功能由formbean和actionbean來完成。
3.struts優缺點
優點:
struts跟tomcat、turbine等諸多apache項目一樣,是開源軟件,這是它的一大優點。使開發者能更深入的了解其內部實現機制。
除此之外,struts的優點主要集中體現在兩個方面:taglib和頁面導航。taglib是struts的標記庫,靈活動用,能大大提高開發效率。另外,就目前國內的jsp開發者而言,除了使用jsp自帶的常用標記外,很少開發自己的標記,或許struts是一個很好的起點。
關于頁面導航,我認為那將是今后的一個發展方向,事實上,這樣做,使系統的脈絡更加清晰。通過一個配置文件,即可把握整個系統各部分之間的聯系,這對于后期的維護有著莫大的好處。尤其是當另一批開發者接手這個項目時,這種優勢體現得更加明顯。
缺點:
taglib是struts的一大優勢,但對于初學者而言,卻需要一個持續學習的過程,甚至還會打亂你網頁編寫的習慣,但是,當你習慣了它時,你會覺得它真的很棒。
struts將mvc的controller一分為三,在獲得結構更加清晰的同時,也增加了系統的復雜度。
struts從產生到現在還不到半年,但已逐步越來越多運用于商業軟件。雖然它現在還有不少缺點,但它是一種非常優秀的j2ee mvc實現方式,如果你的系統準備采用j2ee mvc架構,那么,不妨考慮一下struts。
4.struts實施經驗:
1)、基于struts架構的項目開發,首先需要有一個很好的整體規劃,整個系統中包括哪幾個模塊,每個模塊各需要多少formbean和actionbean等,而且最好有專人負責struts-config.xml的管理。開發基于struts的項目的難點在于配置管理,尤其是對struts-config.xml的管理
2)、如果你的項目非常緊,并且項目組中又沒有富有經驗的struts開發人員,建議不要冒然采用struts。struts的掌握需要一個過程,對于一個熟練的jsp程序員,自學大概需要半個月左右的時間。如果結合titls,則需要更長的時間
3)、如果你在網頁中大量運用taglib,那么你的美工將做出部分犧牲。當你結合tiles,功能增強的同時,這種犧牲尤為明顯。當然,你對功能和美觀的取舍由你自己決定
4)、taglib是一個好東西,但靈活運用它卻需要一個過程,如果你不想在taglib上花太多的時間,那么只需理解與form有關的幾個標記,其它的標記就放著吧,以后再看,先去研究actionservlet和struts-config.xml,你會覺得很有成就感
5)、struts是否只適合于大型項目呢?no!struts適合于各種大小的項目,當然,對于大型項目,它所體現出來的優勢更加明顯。
中國最大的web開發資源網站及技術社區,新聞熱點
疑難解答