Eclipse開發(fā)struts完全指南_第1頁
Eclipse開發(fā)struts完全指南_第2頁
Eclipse開發(fā)struts完全指南_第3頁
Eclipse開發(fā)struts完全指南_第4頁
Eclipse開發(fā)struts完全指南_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、一、準(zhǔn)備安裝程序 1、JDK 5.0 安裝程序下載 下載地址:/DownloadPage:com.sun.sunit.sdlc.content.DownloadPageInfo;jsessionid=502E87C71D77E3BC297C08B35DAC9AD4;jsessionid=502E87C71D77E3BC297C08B35DAC9AD4八、創(chuàng)建測試工程 如果已經(jīng)完成了上面所有步驟,現(xiàn)在可以重新啟動(dòng)eclipse,使新安裝的插件生效,開始正式開發(fā)了。 1、使用Sysdeo Tomcat Plugin創(chuàng)建tomcat工程: File->new->others,打開新建向?qū)?/p>

2、對話框,在樹中找到j(luò)ava->tomcat projects,選中,點(diǎn)擊next按鈕。在projects name中輸入textweb,選中Use default,點(diǎn)擊next。在下一個(gè)對話頁面,保持默認(rèn)設(shè)置,點(diǎn)擊finished。這時(shí),我們在eclipse的package explorer中會(huì)看到新建的工程testweb,創(chuàng)建完成。 2、加入struts框架 File->new->others,打開新建向?qū)υ捒颍业紸materas->Struts->Add Struts Support,選中點(diǎn)擊next按鈕。 保持默認(rèn)設(shè)置,點(diǎn)擊Finish按鈕。這時(shí),在ec

3、lipse的package explorer中會(huì)看到增加了很多struts的庫文件,在WEB-INF下也增加了很多struts的配置文件。到此我們已經(jīng)在項(xiàng)目加入了Struts框架。 3、編輯struts-config.xml文件 在WEB-INF文件夾下可以找到,右鍵點(diǎn)擊菜單中選擇open with->Amateras XML Editer可以直接對xml文本進(jìn)行編輯,選擇open with->struts-config.xml editor可以在圖形模式下對文件進(jìn)行編輯。 在右邊的outline中點(diǎn)擊相應(yīng)的struts對象可以添加新的對象進(jìn)去。這里我們只是說明這里有一個(gè)比較方便的

4、struts-config.xml文件的編輯器,后面我們將開發(fā)一個(gè)簡單的小程序。 4、新建一個(gè)頁面index.jsp File->new->others,打開新建向?qū)υ捒?,找到Amateras->JSP File,點(diǎn)擊next按鈕,F(xiàn)ileName改為index.jsp,點(diǎn)擊Finish。然后打開index.jsp文件進(jìn)行編輯,內(nèi)容如下: <%page pageEncoding="GBK"contentType="text/html; charset=gb2312" %><html><head>&l

5、t;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"cel

6、lspacing="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

7、</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"

8、; name="Submit" value="提交"></td></tr></table></form></body></html>package com.is.form;import org.apache.struts.action.ActionForm;public class LoginForm extends ActionForm private static final long serialVersionUID = 1L;private String usernam

9、e = ""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() re

10、turn username;/* param username The username to set.*/public void setUsername(String username) this.username = username;注意,這里的兩個(gè)屬性分別對應(yīng)我們jsp中form中的兩個(gè)輸入控件的名稱,為什么這樣做,可以去看struts的幫助文檔了,我就不詳細(xì)說了,還有form類再寫完屬性后,get和set方法可以通過eclipse的source中的命令來自動(dòng)生成,在右鍵菜單中,也不詳細(xì)說了,去網(wǎng)上查資料吧,關(guān)于eclipse的使用有很多的文檔。 package com.is.acti

11、on; 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.LoginF

12、orm; 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,

13、/ 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; cha

14、rset=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("/we

15、lcome.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)建登錄成功頁面 同創(chuàng)建index.jsp頁面相同,我們創(chuàng)建welcome.jsp頁面,均使用默認(rèn)設(shè)置。并編輯其內(nèi)容如下: <%page pageEncoding="GBK" contentTy

16、pe="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&&t

17、ype.equals("true") out.print("歡迎您的光臨!"); else out.print("對不起,你輸入的用戶名或者密碼錯(cuò)誤!"); %> </body> </html>8、增加Struts-config.xml中的配置 添加formbean的配置,在和標(biāo)簽之間加入: <form-bean name="loginForm" type="com.is.form.LoginForm"/>添加jsp文件的映射,在和標(biāo)簽之間加入: <

18、;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=

19、"true"/>修改后的struts-config.xml大致如下形式: <?xml version="1.0"?> <!DOCTYPE struts-config PUBLIC "-/Apache Software Foundation/DTD Struts Configuration 1.2/EN""/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources&

20、gt; </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="/i

21、ndex" 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"/> <p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論