一、準(zhǔn)備安裝程序
1、JDK 5.0 安裝程序下載
下載地址:
https://jsecom15b.sun.com/ECom/EComActionServlet
/DownloadPage:~:com.sun.
sunit.sdlc.content.DownloadPageInfo;
jsessionid=502E87C71D77E3BC297C08B35
DAC9AD4;jsessionid=502E87C71D77E3BC297C08B35DAC9AD4 |
同意協(xié)議后,可以下載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版,由于是測(cè)試版,所以,可能存在不確定的因素,所以,我們這里下載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
這里需要對(duì)應(yīng)版本的JDT,如果下載的是Eclipse SDK版本就自帶了,如果沒(méi)有,同一個(gè)下載頁(yè)面的靠后部分有JDT的下載鏈接,下載后將對(duì)應(yīng)的文件分別拷貝進(jìn)eclipse安裝目錄下的對(duì)應(yīng)的文件夾即可。
這里我們下載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。
好了,到此我們準(zhǔn)備好了所有的安裝包。
二、安裝JDK并配置開(kāi)發(fā)環(huán)境?
這里只需要雙擊JDK安裝包的exe文件,按照提示安裝即可。安裝完成后,將JDK安裝目錄下的bin文件夾設(shè)置進(jìn)環(huán)境變量的path路徑中去。同時(shí),將JRE的lib目錄下的rt.jar包設(shè)置進(jìn)環(huán)境變量的classpath中去。?
這里我們把JDK安裝到了默認(rèn)安裝路徑C:\Program Files\Java\jdk1.5.0_02下。這里就不多說(shuō)了,網(wǎng)上可以搜索到很多JDK安裝配置的文章。?
三、安裝tomcat服務(wù)器?
只需要雙擊tomcat安裝包的exe文件,按照提示安裝即可,這里我們的安裝目錄為D:\Tomcat5.5,管理員密碼設(shè)置為12345678。?
四、安裝Eclipse開(kāi)發(fā)工具?
只需要把eclipse-SDK-3.1-win32.zip安裝包解壓,然后把里面的eclipse文件夾剪切至想放置的目錄即可,這里我們把eclipse安裝到了D:\eclipse目錄下。?
在安裝目錄下雙擊eclipse.exe文件,即可啟動(dòng)eclipse。中間會(huì)詢問(wèn)默認(rèn)的工作目錄,你可以按其默認(rèn)設(shè)定的,也可以自己設(shè)定工作目錄,這里設(shè)置為E:\workspace目錄。請(qǐng)選中Use this as the default and do not ask again選項(xiàng),這樣以后啟動(dòng)就不會(huì)再詢問(wèn)了。?
到此eclipse安裝成功。?
五、安裝tomcatPlugin插件?
安裝很簡(jiǎn)單,解壓我們已經(jīng)下載的安裝包tomcatPluginV31beta.zip,將解壓后的com.sysdeo.eclipse.tomcat_3.1.0.beta文件夾拷貝至D:\eclipse\plugins目錄下即可。我們關(guān)掉已經(jīng)打開(kāi)的eclipse,重新打開(kāi),OK,現(xiàn)在我們會(huì)發(fā)現(xiàn)工具欄上多了幾個(gè)雄貓的圖標(biāo),就是tomcat的啟動(dòng)、停止和重啟動(dòng)的按鈕。打開(kāi)eclipse的window/preferences,我們?cè)谧筮叺臉?shù)種也可以找到tomcat一項(xiàng)。?
點(diǎn)擊tomcat項(xiàng),在右邊我們選擇version 5.x版本,然后設(shè)定tomcat home為D:\Tomcat5.5,Contexts directory為D:\Tomcat5.5\conf\Catalina\localhost。到此tomcat plugin設(shè)置完畢。?
六、安裝GEF插件?
同樣,將GEF-ALL-3.1.zip解壓縮,然后拷貝解壓后的eclipse目錄下的三個(gè)文件夾到D:\eclipse目錄下,覆蓋所有的現(xiàn)有文件夾。?
好了,到此GEF安裝完成。
七、安裝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、創(chuàng)建action對(duì)象
同創(chuàng)建form的過(guò)程相同,我們只是新建一個(gè)com.is.action包,同樣的過(guò)程,打開(kāi)新建向?qū)В皇沁x擇Struts Action,創(chuàng)建LoginAction.java類(lèi),均選默認(rèn)值。我們編輯LoginAction為如下內(nèi)容:
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轉(zhuǎn)向的,你也可以按照struts中提供的空白例程struts-blank.war中的做法進(jìn)行轉(zhuǎn)向,可以比較一下會(huì)有收獲的。
7、創(chuàng)建登錄成功頁(yè)面
同創(chuàng)建index.jsp頁(yè)面相同,我們創(chuàng)建welcome.jsp頁(yè)面,均使用默認(rèn)設(shè)置。并編輯其內(nèi)容如下:
<%@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("對(duì)不起,你輸入的用戶名或者密碼錯(cuò)誤!");
}
%>
</body>
</html> |
8、增加Struts-config.xml中的配置
添加formbean的配置,在 和 標(biāo)簽之間加入:
<form-bean
name="loginForm"
type="com.is.form.LoginForm"/> |
添加jsp文件的映射,在 和 標(biāo)簽之間加入:
<action
path="/index"
forward="/index.jsp"/>
<action
path="/welcome"
forward="/welcome.jsp"/> |
添加action文件的映射,在 和 標(biāo)簽之間加入:
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> |
到此我們可以運(yùn)行測(cè)試程序了。
9、運(yùn)行測(cè)試程序
右鍵點(diǎn)擊testweb工程根目錄,點(diǎn)擊菜單中的Tomcate project->update context definition,將工程部署進(jìn)tomcat,成功后會(huì)提示操作成功。
點(diǎn)擊菜單欄中的雄貓圖標(biāo)啟動(dòng)tomcat,然后在IE地址欄中輸入http://localhost:8080/testweb/index.do,我們會(huì)看到index.jsp的頁(yè)面內(nèi)容。
九、創(chuàng)建測(cè)試工程
如果已經(jīng)完成了上面所有步驟,現(xiàn)在可以重新啟動(dòng)eclipse,使新安裝的插件生效,開(kāi)始正式開(kāi)發(fā)了。
1、使用Sysdeo Tomcat Plugin創(chuàng)建tomcat工程:
File->new->others,打開(kāi)新建向?qū)?duì)話框,在樹(shù)中找到j(luò)ava->tomcat projects,選中,點(diǎn)擊next按鈕。在projects name中輸入textweb,選中Use default,點(diǎn)擊next。在下一個(gè)對(duì)話頁(yè)面,保持默認(rèn)設(shè)置,點(diǎn)擊finished。這時(shí),我們?cè)趀clipse的package explorer中會(huì)看到新建的工程testweb,創(chuàng)建完成。
2、加入struts框架
File->new->others,打開(kāi)新建向?qū)?duì)話框,找到Amateras->Struts->Add Struts Support,選中點(diǎn)擊next按鈕。
保持默認(rèn)設(shè)置,點(diǎn)擊Finish按鈕。這時(shí),在eclipse的package explorer中會(huì)看到增加了很多struts的庫(kù)文件,在WEB-INF下也增加了很多struts的配置文件。到此我們已經(jīng)在項(xiàng)目加入了Struts框架。
3、編輯struts-config.xml文件
在WEB-INF文件夾下可以找到,右鍵點(diǎn)擊菜單中選擇open with->Amateras XML Editer可以直接對(duì)xml文本進(jìn)行編輯,選擇open with->struts-config.xml editor可以在圖形模式下對(duì)文件進(jìn)行編輯。
在右邊的outline中點(diǎn)擊相應(yīng)的struts對(duì)象可以添加新的對(duì)象進(jìn)去。這里我們只是說(shuō)明這里有一個(gè)比較方便的struts-config.xml文件的編輯器,后面我們將開(kāi)發(fā)一個(gè)簡(jiǎn)單的小程序。
4、新建一個(gè)頁(yè)面index.jsp
File->new->others,打開(kāi)新建向?qū)?duì)話框,找到Amateras->JSP File,點(diǎn)擊next按鈕,F(xiàn)ileName改為index.jsp,點(diǎn)擊Finish。然后打開(kāi)index.jsp文件進(jìn)行編輯,內(nèi)容如下:
<%@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、創(chuàng)建form數(shù)據(jù)對(duì)象
打開(kāi)File->new->package對(duì)話框,name中輸入com.is.form,點(diǎn)擊Finish按鈕。在右邊的Package Explorer樹(shù)中找到剛才創(chuàng)建的包,右鍵點(diǎn)擊com.is.form包,菜單中的new->others,找到Amateras->struts->Struts Action Form,點(diǎn)擊next,在對(duì)話框中name欄輸入LoginForm,點(diǎn)擊Finish按鈕。
編輯LoginForm類(lèi)的內(nèi)容為:
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;
}
} |
注意,這里的兩個(gè)屬性分別對(duì)應(yīng)我們jsp中form中的兩個(gè)輸入控件的名稱(chēng),為什么這樣做,可以去看struts的幫助文檔了,我就不詳細(xì)說(shuō)了,還有form類(lèi)再寫(xiě)完屬性后,get和set方法可以通過(guò)eclipse的source中的命令來(lái)自動(dòng)生成,在右鍵菜單中,也不詳細(xì)說(shuō)了,去網(wǎng)上查資料吧,關(guān)于eclipse的使用有很多的文檔。
|