Jsp之Struts入門簡介_JSP教程
推薦:正則表達(dá)式入門簡介簡單的說,正則表達(dá)式是一種可以用于模式匹配和替換的強(qiáng)有力的工具。我們可以在幾乎所有的基于UNIX系統(tǒng)的工具中找到正則表達(dá)式的身影,例如,vi編輯器,Perl或PHP腳本語言,以及awk或sedshell程序等。此外,象Java script 這種客戶端的腳本語言也提供了對正
Jsp之Struts從產(chǎn)生到現(xiàn)在還不到半年,但已逐步越來越多運(yùn)用于商業(yè)軟件。雖然它現(xiàn)在還有不少缺點(diǎn),但它是一種非常優(yōu)秀的J2EE MVC實(shí)現(xiàn)方式,如果你的系統(tǒng)準(zhǔn)備采用J2EE MVC架構(gòu),那么,不妨考慮一下Struts,下面本文對Jsp之Struts做一簡要介紹。
1.安裝Struts:
首先到http://jakarta.apache.org/Struts下載Struts,建議使用release版,現(xiàn)在最高版本為1.2.6,有多種OS版本(windows,linus...),下載后解壓開來,可以看到這個目錄:lib和webapps,webapps下有一些WAR文件。假設(shè)你的Tomcat裝在c:\\Tomcat下,則將那些WAR文件拷貝到C:\\Tomcat\\webapps,重新啟動Tomcat即可。打開瀏覽器,在地址欄中輸入:http://localhost:8080/Struts-examples/,若能見到“powered by Struts”的深藍(lán)色圖標(biāo),即說明成功了。這是Struts自帶的一個例子,附有詳細(xì)的說明文檔,可以做為初學(xué)者的入門教程。另外,Struts還提供了一系統(tǒng)實(shí)用對象:XML處理、通過Java reflection APIs自動處理JavaBeans屬性、國際化的提示和消息等
2.練習(xí)做一個實(shí)例:
一個用戶注冊系統(tǒng),用戶通過網(wǎng)頁輸入相關(guān)信息:注冊ID號,密碼,EMAIL,若注冊成功,則返回成功提示信息,反之出現(xiàn)注冊失敗提示信息。
項(xiàng)目建立:
正式開發(fā)前,需要在Tocmat(我的tomcat裝在c:\\tomcat)中建立此項(xiàng)目。比較快的一種建立方式為:在C:\\tomcat\\webapps下新建目錄test,再將C:\\tomcat\\webapps\\struts-example下的WEB-INF目錄拷貝到test目錄下,然后將test\\WEB-INF下的src和classes目錄清空,以及struts-config.xml文件中內(nèi)容清空即可。這樣,我們需要的Struts類包及相關(guān)的配置文件就都齊了。
開發(fā)時,將JSP文件放在test目錄下,Java原文件放在test\\WEB-INF\\src下,編譯后的類文件放在test\\WEB-INF\\classes下。
注冊頁面: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> [Page] <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頁,因?yàn)樗罅窟\(yùn)用了taglib,這些taglib對初學(xué)者而言,可能難于掌握,可這卻是Struts的精華之一。靈活運(yùn)用,將大大提高開發(fā)效率。
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\"/> |
Struts的核心是Controller,即ActionServlet,而ActionServlet的核心就是Struts-config.xml,Struts-config.xml集中了所有頁面的導(dǎo)航定義。對于大型的WEB項(xiàng)目,通過此配置文件即可迅速把握其脈絡(luò),這不管是對于前期的開發(fā),還是后期的維護(hù)或升級都是大有裨益的。掌握Struts-config.xml是掌握Struts的關(guān)鍵所在。
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類,F(xiàn)ormBean是對頁面請求的封裝。即把HTTP request 封裝在一個對象中,需要說明的一點(diǎn)就是多個HTTP request可以共用一個FormBean,便于維護(hù)和重用。
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\"); /* 取得用戶請求,做相應(yīng)數(shù)據(jù)庫操作,略 */ } } |
FormBean的產(chǎn)生是為了提供數(shù)據(jù)給ActionBean,在ActionBean中可以取得FormBean中封裝的數(shù)據(jù),經(jīng)相應(yīng)的邏輯處理后,調(diào)用業(yè)務(wù)方法完成相應(yīng)業(yè)務(wù)要求。
Servlet的演變:在常規(guī)的 JSP,Servlet,JavaBean三層結(jié)構(gòu)中,JSP實(shí)現(xiàn)View的功能,Servlet實(shí)現(xiàn)Controller的功能,JavaBean實(shí)現(xiàn)Model的實(shí)現(xiàn)。
在Struts中,將常規(guī)情況下的Servlet拆分與ActionServlet、FormBean、ActionBean三個部分。ActionServlet配合Struts-config.xml,專職完成頁面導(dǎo)航,而不再負(fù)責(zé)具體的數(shù)據(jù)獲取與相應(yīng)邏輯,這兩部分功能由FormBean和ActionBean來完成。
3.Struts優(yōu)缺點(diǎn)
缺點(diǎn):
Taglib是Struts的一大優(yōu)勢,但對于初學(xué)者而言,卻需要一個持續(xù)學(xué)習(xí)的過程,甚至還會打亂你網(wǎng)頁編寫的習(xí)慣,但是,當(dāng)你習(xí)慣了它時,你會覺得它真的很棒。
Struts將MVC的Controller一分為三,在獲得結(jié)構(gòu)更加清晰的同時,也增加了系統(tǒng)的復(fù)雜度。
優(yōu)點(diǎn):
Struts跟Tomcat、Turbine等諸多Apache項(xiàng)目一樣,是開源軟件,這是它的一大優(yōu)點(diǎn)。使開發(fā)者能更深入的了解其內(nèi)部實(shí)現(xiàn)機(jī)制。
除此之外,Struts的優(yōu)點(diǎn)主要集中體現(xiàn)在兩個方面:Taglib和頁面導(dǎo)航。Taglib是Struts的標(biāo)記庫,靈活動用,能大大提高開發(fā)效率。另外,就目前國內(nèi)的JSP開發(fā)者而言,除了使用JSP自帶的常用標(biāo)記外,很少開發(fā)自己的標(biāo)記,或許Struts是一個很好的起點(diǎn)。
關(guān)于頁面導(dǎo)航,我認(rèn)為那將是今后的一個發(fā)展方向,事實(shí)上,這樣做,使系統(tǒng)的脈絡(luò)更加清晰。通過一個配置文件,即可把握整個系統(tǒng)各部分之間的聯(lián)系,這對于后期的維護(hù)有著莫大的好處。尤其是當(dāng)另一批開發(fā)者接手這個項(xiàng)目時,這種優(yōu)勢體現(xiàn)得更加明顯。
分享:AJAX實(shí)現(xiàn)web頁面級聯(lián)菜單本例中只要你選擇成員分類名稱就會自動顯示成員名稱: 首先在eclipse中建一個項(xiàng)目,這里為Easyjf-menu,對應(yīng)的瀏覽器頁面代碼為: Easyjf-menu.jsp @page contentType=”text/html;charser=UTF-8” language=”java”% head …….. /head script language=”j
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復(fù)習(xí)整理
- JSP腳本元素和注釋復(fù)習(xí)總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實(shí)現(xiàn)
- 網(wǎng)頁模板:關(guān)于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網(wǎng)站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關(guān)鏈接:
- 教程說明:
JSP教程-Jsp之Struts入門簡介
。