一、準備安裝程式
1、JDK 5.0 安裝程式下載
下載地址:
https://jsecom15b.sun.com/ECom/EComActionServlet/DownloadPage:~:com.sun.sunit.sdlc.content.DownloadPageInfo;jsessionid=502E87C71D77E3BC297C08B35DAC9AD4;jsessionid=502E87C71D77E3BC297C08B35DAC9AD4 |
同意協議後,可以下載jdk-1_5_0_05-windows-i586-p.exe。
2、tomcat 5.5.9下載
下載地址:http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi
最新版本是5.5.11alpha版,由於是測試版,所以,可能存在不確定的因素,所以,我們這裡下載5.5.9 exe。
3、Sysdeo Eclipse Tomcat Launcher plugin 3.1beta版下載
下載地址:http://www.sysdeo.com/eclipse/tomcatplugin
這裡下載tomcatPluginV31beta.zip。
4、eclipse 3.1安裝程式下載
下載地址:http://www.eclipse.org/downloads/index.php
這裡需要對應版本的JDT,如果下載的是Eclipse SDK版本就內建了,如果沒有,同一個下載頁面的靠後部分有JDT的下載連結,下載後將對應的檔案分別拷貝進eclipse安裝目錄下的對應的檔案夾即可。
這裡我們下載eclipse-SDK-3.1-win32.zip 。
5、GEF 3.1 安裝程式下載
下載地址:
http://download.eclipse.org/tools/gef/downloads/drops/R-3.1-200507071758/
index.php
這裡我們下載GEF-ALL-3.1.zip。
6、Eclipse HTML Editor 1.6.7 安裝程式下載
下載地址:https://sourceforge.jp/projects/amateras/files/?release_id=16537
#16537
這裡我們下載:tk.eclipse.plugin.htmleditor_1.6.7.zip。
7、StrutsIDE 1.1.7 安裝程式下載
下載地址:https://sourceforge.jp/projects/amateras/files/?release_id=16537#16537
這裡我們下載:tk.eclipse.plugin.struts_1.1.7.zip。
好了,到此我們準備好了所有的安裝包。
二、安裝JDK並配置開發環境
這裡只需要雙擊JDK安裝包的exe檔案,按照提示安裝即可。安裝完成後,將JDK安裝目錄下的bin檔案夾設定進環境變數的path路徑中去。同時,將JRE的lib目錄下的rt.jar包設定進環境變數的classpath中去。
這裡我們把JDK安裝到了預設安裝路徑C:\Program Files\Java\jdk1.5.0_02下。這裡就不多說了,網上可以搜尋到很多JDK安裝配置的文章。
三、安裝tomcat伺服器
只需要雙擊tomcat安裝包的exe檔案,按照提示安裝即可,這裡我們的安裝目錄為D:\Tomcat5.5,管理員密碼設定為12345678。
四、安裝Eclipse開發工具
只需要把eclipse-SDK-3.1-win32.zip安裝包解壓,然後把裡面的eclipse檔案夾剪下至想放置的目錄即可,這裡我們把eclipse安裝到了D:\eclipse目錄下。
在安裝目錄下雙擊eclipse.exe檔案,即可啟動eclipse。中間會詢問預設的工作目錄,你可以按其預設設定的,也可以自己設定工作目錄,這裡設定為E:\workspace目錄。請選中Use this as the default and do not ask again選項,這樣以後啟動就不會再詢問了。
到此eclipse安裝成功。
五、安裝tomcatPlugin外掛程式
安裝很簡單,解壓我們已經下載的安裝包tomcatPluginV31beta.zip,將解壓後的com.sysdeo.eclipse.tomcat_3.1.0.beta檔案夾拷貝至D:\eclipse\plugins目錄下即可。我們關掉已經開啟的eclipse,重新開啟,OK,現在我們會發現工具列上多了幾個雄貓的表徵圖,就是tomcat的啟動、停止和重啟動的按鈕。開啟eclipse的window/preferences,我們在左邊的樹種也可以找到tomcat一項。
點擊tomcat項,在右邊我們選擇version 5.x版本,然後設定tomcat home為D:\Tomcat5.5,Contexts directory為D:\Tomcat5.5\conf\Catalina\localhost。到此tomcat plugin設定完畢。
六、安裝GEF外掛程式
同樣,將GEF-ALL-3.1.zip解壓縮,然後拷貝解壓後的eclipse目錄下的三個檔案夾到D:\eclipse目錄下,覆蓋所有的現有檔案夾。
好了,到此GEF安裝完成。
八、建立測試工程
如果已經完成了上面所有步驟,現在可以重新啟動eclipse,使新安裝的外掛程式生效,開始正式開發了。
1、使用Sysdeo Tomcat Plugin建立tomcat工程:
File->new->others,開啟建立嚮導對話方塊,在樹中找到java->tomcat projects,選中,點擊next按鈕。在projects name中輸入textweb,選中Use default,點擊next。在下一個對話頁面,保持預設設定,點擊finished。這時,我們在eclipse的package explorer中會看到建立的工程testweb,建立完成。
2、加入struts架構
File->new->others,開啟建立嚮導對話方塊,找到Amateras->Struts->Add Struts Support,選中點擊next按鈕。
保持預設設定,點擊Finish按鈕。這時,在eclipse的package explorer中會看到增加了很多struts的庫檔案,在WEB-INF下也增加了很多struts的設定檔。到此我們已經在項目加入了Struts架構。
3、編輯struts-config.xml檔案
在WEB-INF檔案夾下可以找到,右鍵點擊菜單中選擇open with->Amateras XML Editer可以直接對xml文本進行編輯,選擇open with->struts-config.xml editor可以在圖形模式下對檔案進行編輯。
在右邊的outline中點擊相應的struts對象可以添加新的對象進去。這裡我們只是說明這裡有一個比較方便的struts-config.xml檔案的編輯器,後面我們將開發一個簡單的小程式。
4、建立一個頁面index.jsp
File->new->others,開啟建立嚮導對話方塊,找到Amateras->JSP File,點擊next按鈕,FileName改為index.jsp,點擊Finish。然後開啟index.jsp檔案進行編輯,內容如下:
<%@page pageEncoding="GBK"contentType="text/html; charset=gb2312" %><html><head><meta http-equiv="Content-Type"content="text/html; charset=gb2312"/><title></title></head><body><form name="form1" method="post" action="/testweb/logincheck.do"><table width="300" border="0"cellspacing="0" cellpadding="0"><tr align="center"><td colspan="2">使用者登入資訊</td></tr><tr><td>使用者名稱</td><td><input name="username" type="text" id="username"size="12">user </td></tr><tr><td>使用者密碼</td><td><input name="password" type="text" id="password" size="12">123456 </td></tr><tr align="center"><td colspan="2"><input type="submit" name="Submit" value="提交"></td></tr></table></form></body></html> |
5、建立form資料對象
開啟File->new->package對話方塊,name中輸入com.is.form,點擊Finish按鈕。在右邊的Package Explorer樹中找到剛才建立的包,右鍵點擊com.is.form包,菜單中的new->others,找到Amateras->struts->Struts Action Form,點擊next,在對話方塊中name欄輸入LoginForm,點擊Finish按鈕。
編輯LoginForm類的內容為:
package com.is.form;import org.apache.struts.action.ActionForm;public class LoginForm extends ActionForm {private static final long serialVersionUID = 1L;private String username = "";private String password = "";/*** @return Returns the password.*/public String getPassword(){return password;} /*** @param password The password to set.*/public void setPassword(String password){ this.password = password;}/*** @return Returns the username.*/public String getUsername() {return username;}/*** @param username The username to set.*/public void setUsername(String username) {this.username = username;}} |
注意,這裡的兩個屬性分別對應我們jsp中form中的兩個輸入控制項的名稱,為什麼這樣做,可以去看struts的協助文檔了,我就不詳細說了,還有form類再寫完屬性後,get和set方法可以通過eclipse的source中的命令來自動產生,在右鍵菜單中,也不詳細說了,去網上查資料吧,關於eclipse的使用有很多的文檔。
七、安裝Eclipse HTML Editor外掛程式
解壓縮tk.eclipse.plugin.htmleditor_1.6.7.zip包,然後將plugins目錄拷貝至D:\eclipse目錄下覆蓋原檔案夾即可。到此Eclipse HTML Editor外掛程式安裝完成。
八、安裝StrutsIDE外掛程式
解壓縮tk.eclipse.plugin.struts_1.1.7.zip包,然後將plugins目錄拷貝至D:\eclipse目錄下覆蓋原檔案夾即可。
好了,到此StrutsIDE外掛程式安裝完成。
6、建立action對象
同建立form的過程相同,我們只是建立一個com.is.action包,同樣的過程,開啟建立嚮導,只是選擇Struts Action,建立LoginAction.java類,均選預設值。我們編輯LoginAction為如下內容:
package com.is.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.is.form.LoginForm; public class LoginAction extends Action { private static final long serialVersionUID = 1L; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { // this line is here for when theinput page is upload-utf8.jsp, // it sets the correct characterencoding for the response String encoding = request.getCharacterEncoding(); if ((encoding != null) && (encoding.equalsIgnoreCase("GB2312"))){ response.setContentType("text/html; charset=GB2312"); } else { response.setContentType("text/html; charset=GBK"); } try { if (form instanceof LoginForm) { LoginForm theForm = (LoginForm) form; if(theForm.getUsername().equals("user") && theForm.getPassword().equals("123456")) { return new ActionForward("/welcome.do?type=true"); } else { return new ActionForward("/welcome.do?type=false"); } } } catch (Exception e) { } // this shouldn't happen in this example return null; } } |
注意這裡是直接用ActionForward轉向的,你也可以按照struts中提供的空白常式struts-blank.war中的做法進行轉向,可以比較一下會有收穫的。
7、建立登入成功頁面
同建立index.jsp頁面相同,我們建立welcome.jsp頁面,均使用預設設定。並編輯其內容如下:
<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %><html> <head> <meta http-equiv="Content-Type" content="text/html;charset=GBK"/> <title></title> </head> <body> <% String type = request.getParameter("type"); if(type!=null&&type.equals("true")){ out.print("歡迎您的光臨!"); } else{ out.print("對不起,你輸入的使用者名稱或者密碼錯誤!"); } %> </body> </html> |
8、增加Struts-config.xml中的配置
添加formbean的配置,在和標籤之間加入:
<form-bean name="loginForm" type="com.is.form.LoginForm"/> |
添加jsp檔案的映射,在和標籤之間加入:
<action path="/index" forward="/index.jsp"/> <action path="/welcome" forward="/welcome.jsp"/> |
添加action檔案的映射,在和標籤之間加入:
path="/logincheck" type="com.is.action.LoginAction" name="loginForm" scope="request" validate="true"/> |
修改後的struts-config.xml大致如下形式:
<?xml version="1.0"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN""http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources> </data-sources> <form-beans> <form-bean name="loginForm" type="com.is.form.LoginForm"/> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> </global-forwards> <action-mappings> <action path="/index" forward="/index.jsp"/> <action path="/welcome" forward="/welcome.jsp"/> <action path="/logincheck" type="com.is.action.LoginAction" name="loginForm" scope="request" validate="true"/> </action-mappings> <controller processorClass= "org.apache.struts.tiles.TilesRequestProcessor"/> <message-resources parameter="MessageResources"/> <plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/> <set-property property="moduleAware" value="true"/> </plug-in> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames"value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> </struts-config> |
到此我們可以運行測試程式了。
9、運行測試程式
右鍵點擊testweb工程根目錄,點擊菜單中的Tomcate project->update context definition,將工程部署進tomcat,成功後會提示操作成功。
點擊功能表列中的雄貓表徵圖啟動tomcat,然後在IE地址欄中輸入http://localhost:8080/testweb/index.do,我們會看到index.jsp的頁面內容。