Eclipse开发struts完全指南_第1页
Eclipse开发struts完全指南_第2页
Eclipse开发struts完全指南_第3页
Eclipse开发struts完全指南_第4页
Eclipse开发struts完全指南_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一、准备安装程序 1、JDK 5.0 安装程序下载 下载地址:/DownloadPage:com.sun.sunit.sdlc.content.DownloadPageInfo;jsessionid=502E87C71D77E3BC297C08B35DAC9AD4;jsessionid=502E87C71D77E3BC297C08B35DAC9AD4八、创建测试工程 如果已经完成了上面所有步骤,现在可以重新启动eclipse,使新安装的插件生效,开始正式开发了。 1、使用Sysdeo Tomcat Plugin创建tomcat工程: File->new->others,打开新建向导

2、对话框,在树中找到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按钮。这时,在ec

3、lipse的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对象可以添加新的对象进去。这里我们只是说明这里有一个比较方便的

4、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>&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;注意,这里的两个属性分别对应我们jsp中form中的两个输入控件的名称,为什么这样做,可以去看struts的帮助文档了,我就不详细说了,还有form类再写完属性后,get和set方法可以通过eclipse的source中的命令来自动生成,在右键菜单中,也不详细说了,去网上查资料吧,关于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转向的,你也可以按照struts中提供的空白例程struts-blank.war中的做法进行转向,可以比较一下会有收获的。 7、创建登录成功页面 同创建index.jsp页面相同,我们创建welcome.jsp页面,均使用默认设置。并编辑其内容如下: <%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("对不起,你输入的用户名或者密码错误!"); %> </body> </html>8、增加Struts-config.xml中的配置 添加formbean的配置,在和标签之间加入: <form-bean name="loginForm" type="com.is.form.LoginForm"/>添加jsp文件的映射,在和标签之间加入: <

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

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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论