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...),下載后解壓開來,可以看到這個(gè)目錄: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自帶的一個(gè)例子,附有詳細(xì)的說明文檔,可以做為初學(xué)者的入門教程。另外,Struts還提供了一系統(tǒng)實(shí)用對象:XML處理、通過Java reflection APIs自動處理JavaBeans屬性、國際化的提示和消息等
2.練習(xí)做一個(gè)實(shí)例:
一個(gè)用戶注冊系統(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ā)時(shí),將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> |
| <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/"/> |
| 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; } } |
| 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ù)庫操作,略 */ } } |
新聞熱點(diǎn)
疑難解答
圖片精選