Strutsprimer.doc

上传人:hw****26 文档编号:3543500 上传时间:2019-06-03 格式:DOC 页数:10 大小:1.44MB
下载 相关 举报
Strutsprimer.doc_第1页
第1页 / 共10页
Strutsprimer.doc_第2页
第2页 / 共10页
Strutsprimer.doc_第3页
第3页 / 共10页
Strutsprimer.doc_第4页
第4页 / 共10页
Strutsprimer.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

1、Struts 学习简单实例作者:王志东,您可以通过 与我取得联系。本人对 WEB 开发不是很熟悉,正在学习这方面的知识。在学习过程中自己写了几行代码,拿出来与大家分享,文中有不妥地方还请多提意见。本例描述了一个在 eclipse 中开发 Struts 应用的简单过程,非常简单。一:搭建开发环境1. 下载并安装 JDK5.0,我都会安装,我想大家就没什么问题了,呵呵。2. Tomcat 安装,大家可以直接下载它的安装文件安装就可以了。3. 下载 Eclipse 并解压到你喜欢的目录。4. GEF 安装程序下载URL:http:/download.eclipse.org/tools/gef/do

2、wnloads/drops/R-3.1-200507071758/index.php5. Eclipse HTML Editor 安装程序下载URL:https:/sourceforge.jp/projects/amateras/files/?release_id=16537#165376. StrutsIDE 安装程序下载URL:https:/sourceforge.jp/projects/amateras/files/?release_id=16537#165377. Sysdeo Eclipse Tomcat Launcher plugin 下载URL:http:/ 4、5、6、7 的

3、eclipse 插件安装到 eclipse 中,推荐大家使用 links 方式安装,便于管理。安装完后,在 CMD 虾进入 eclipse 目录,使用 eclipse clean 启动 eclipse。你会发现上述组件安装成功。二:创建测试工程1、使用 Sysdeo Tomcat Plugin 创建 tomcat 工程:File-new-others,打开新建向导对话框,在树中找到 java-tomcat projects,选中,点击 next 按钮。在 projects name 中输入 textweb,选中 Use default,点击 next。在下一个对话页面,保持默认设置,点击 fi

4、nished。这时,我们在 eclipse 的 package explorer 中会看到新建的工程 textweb,创建完成。2、加入 struts 框架File-new-others,打开新建向导对话框,找到 Amateras-Struts-Add Struts Support,选中点击 next 按钮。保持默认设置,点击 Finish 按钮。这时,在 eclipse 的 package explorer 中会看到增加了很多 struts 的库文件,在 WEB-INF 下也增加了很多 struts 的配置文件。到此我们已经在项目加入了 Struts 框架。3、编辑 struts-confi

5、g.xml 文件在 WEB-INF 文件夹下可以找到,右键点击菜单中选择 open with-Amateras XML Editer 可以直接对 xml 文本进行编辑,选择 open with-struts-config.xml editor 可以在图形模式下对文件进行编辑。在右边的 outline 中点击相应的 struts 对象可以添加新的对象进去。这里我们只是说明这里有一个比较方便的 struts-config.xml 文件的编辑器,后面我们将开发一个简单的小程序。4、新建一个页面 index.jspFile-new-others,打开新建向导对话框,找到 Amateras-JSP Fi

6、le,点击next 按钮,FileName 改为 index.jsp,点击 Finish。然后打开 index.jsp 文件进行编辑,内容如下:用户登录信息用户名用户密码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,点击 Fin

7、ish 按钮。编辑 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()retur

8、n 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;f

9、orm 类再写完属性后, get 和 set 方法可以通过 eclipse 的 source 中的命令来自动生成,在右键菜单中,也不详细说了,非常简单的,_。6、创建 action 对象同创建 form 的过程相同,我们只是新建一个 com.is.action 包,同样的过程,打开新建向导,只是选择 Struts Action,创建 LoginAction.java 类,均选默认值。我们编辑 LoginAction 为如下内容:package com.is.action; import javax.servlet.http.HttpServletRequest; import javax.se

10、rvlet.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

11、 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

12、 encoding = request.getCharacterEncoding(); if (encoding != null) charset=GB2312“); else response.setContentType(“text/html; charset=GBK“); try if (form instanceof LoginForm) LoginForm theForm = (LoginForm) form; if(theForm.getUsername().equals(“test“) else return new ActionForward(“/welcome.do?type

13、=false“); catch (Exception e) / this shouldnt happen in this example return null; 注意这里是直接用 ActionForward 转向的,你也可以按照 struts 中提供的空白例程 struts-blank.war 中的做法进行转向,可以比较一下会有收获的。创建登录成功页面。同创建 index.jsp 页面相同,我们创建 welcome.jsp 页面,均使用默认设置。并编辑其内容如下:7、配置 Struts-config.xml:添加 formbean 的配置,在和标签之间加入:添加 jsp 文件的映射,在和标签

14、之间加入:添加 action 文件的映射,在和标签之间加入:path=“/logincheck“ type=“com.is.action.LoginAction“ name=“loginForm“ scope=“request“ validate=“true“/修改后的 struts-config.xml 大致如下形式:8、运行测试程序右键点击 testweb 工程根目录,点击菜单中的 Tomcate project-update context definition,将工程部署进 tomcat,成功后会提示操作成功。注意,我在 LoginAction 类中将用户名和密码写作 test 和 123456,所以其它都认为错误的。点击菜单栏中的雄猫图标启动 tomcat,然后在浏览器中输入http:/localhost:8080/textweb/index.do,我们会看到 index.jsp 的页面内容如下:当输入用户名(test)和密码(123456)正确时的界面如下:当输入不正确的用户名或密码,界面如下:到此结束,呵呵,是不是太简单了,也许你有更好的入门方法,请与大家分享。附全工程压缩包: Textweb.rar

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。