seaairland

           

          Eclipse開發struts完全指南

          Eclipse開發struts完全指南
          一、準備安裝程序
            
            1、JDK 5.0 安裝程序下載
            
            下載地址:
            
            https://jsecom15b.sun.com/ECom/EComActionServlet
            /DownloadPage:~:com.sun.
            sunit.sdlc.content.DownloadPageInfo;
            jsessionid=502E87C71D77E3BC297C08B35
            DAC9AD4;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 the
            input page is upload-utf8.jsp,
            
            // it sets the correct character
            encoding 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的頁面內容。

          posted on 2006-03-30 04:07 chenhui 閱讀(388) 評論(0)  編輯  收藏 所屬分類: 好文收集

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          介紹 IOC

          友情鏈接

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 郁南县| 昂仁县| 醴陵市| 高清| 姚安县| 彭山县| 兰西县| 高雄市| 上杭县| 丹凤县| 赣州市| 丹巴县| 秦安县| 灵台县| 台湾省| 巨鹿县| 会同县| 二连浩特市| 图木舒克市| 廊坊市| 乌拉特前旗| 镇江市| 新巴尔虎右旗| 临沂市| 锡林浩特市| 喀什市| 宁远县| 襄城县| 哈密市| 广安市| 明水县| 县级市| 武宣县| 星座| 邹平县| 普格县| 卢龙县| 南通市| 连州市| 类乌齐县| 泽库县|