Jsp之Struts從產生到現在還不到半年,但已逐步越來越多運用于商業軟件。雖然它現在還有不少缺點,但它是一種非常優秀的J2EE MVC實現方式,如果你的系統準備采用J2EE MVC架構,那么,不妨考慮一下Struts,下面本文對Jsp之Struts做一簡要介紹。
1.安裝Struts:
首先到http://jakarta.apache.org/Struts下載Struts,建議使用release版,現在最高版本為1.2.6,有多種OS版本(windows,linus...),下載后解壓開來,可以看到這個目錄:lib和webapps,webapps下有一些WAR文件。假設你的Tomcat裝在c://Tomcat下,則將那些WAR文件拷貝到C://Tomcat//webapps,重新啟動Tomcat即可。打開瀏覽器,在地址欄中輸入:http://localhost:8080/Struts-examples/,若能見到“powered by Struts”的深藍色圖標,即說明成功了。這是Struts自帶的一個例子,附有詳細的說明文檔,可以做為初學者的入門教程。另外,Struts還提供了一系統實用對象:XML處理、通過Java reflection APIs自動處理JavaBeans屬性、國際化的提示和消息等
2.練習做一個實例:
一個用戶注冊系統,用戶通過網頁輸入相關信息:注冊ID號,密碼,EMAIL,若注冊成功,則返回成功提示信息,反之出現注冊失敗提示信息。
項目建立:
正式開發前,需要在Tocmat(我的tomcat裝在c://tomcat)中建立此項目。比較快的一種建立方式為:在C://tomcat//webapps下新建目錄test,再將C://tomcat//webapps//struts-example下的WEB-INF目錄拷貝到test目錄下,然后將test//WEB-INF下的src和classes目錄清空,以及struts-config.xml文件中內容清空即可。這樣,我們需要的Struts類包及相關的配置文件就都齊了。
開發時,將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/"); /* 取得用戶請求,做相應數據庫操作,略 */ } } |
新聞熱點
疑難解答