1、Struts学习简单实例 作者:王志东,您可以通过zhidong.wang@与我取得联系。 本人对WEB开发不是很熟悉,正在学习这方面的知识。在学习过程中自己写了几行代码,拿出来与大家分享,文中有不妥地方还请多提意见。 本例描述了一个在eclipse中开发Struts应用的简单过程,非常简单。 一:搭建开发环境 1. 下载并安装JDK5.0,我都会安装,我想大家就没什么问题了,呵呵。 2. Tomcat安装,大家可以直接下载它的安装文件安装就可以了。 3. 下载Eclipse并解压到你喜欢的目录。 4. GEF安装程序下载 URL:http://download.
2、eclipse.org/tools/gef/downloads/drops/R-3.1-200507071758/index.php 5. Eclipse HTML Editor安装程序下载 URL:https://sourceforge.jp/projects/amateras/files/?release_id=16537#16537 6. StrutsIDE安装程序下载 URL:https://sourceforge.jp/projects/amateras/files/?release_id=16537#16537 7. Sysdeo Eclipse Tomcat Launc
3、her plugin下载 URL: 将上述4、5、6、7的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,点击nex
4、t。在下一个对话页面,保持默认设置,点击finished。这时,我们在eclipse的package explorer中会看到新建的工程textweb,创建完成。 2、加入struts框架 File->new->others,打开新建向导对话框,找到Amateras->Struts->Add Struts Support,选中点击next按钮。 保持默认设置,点击Finish按钮。这时,在eclipse的package explorer中会看到增加了很多struts的库文件,在WEB-INF下也增加了很多struts的配置文件。到此我们已经在项目加入了Struts框架。
5、 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,打开新建向
6、导对话框,找到Amateras->JSP File,点击next按钮,FileName改为index.jsp,点击Finish。然后打开index.jsp文件进行编辑,内容如下: <%@page pageEncoding="GBK" contentType="text/html; charset=gb2312" %>
9、sh按钮。在右边的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 sta
10、tic 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) { th
11、is.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; } } form类再写完属性后,get和set方法可以通过eclipse的source中的命令来自动
12、生成,在右键菜单中,也不详细说了,非常简单的,^_^。 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 or
13、g.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 serialVersionUI
14、D = 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
15、 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 Log
16、inForm) { LoginForm theForm = (LoginForm) form; if(theForm.getUsername().equals("test") && theForm.getPassword().equals("123456")) { return new ActionForward("/welcome.do?type=true"); } else { return new ActionForward("/welcome.do?type=fals
17、e"); } } } catch (Exception e) { } // this shouldn't happen in this example return null; } } 注意这里是直接用ActionForward转向的,你也可以按照struts中提供的空白例程struts-blank.war中的做法进行转向,可以比较一下会有收获的。创建登录成功页面。 同创建index.jsp页面相同,我们创建welcome.jsp页面,均使用默认设置。并编辑其内容如下: <%@page pa
18、geEncoding="GBK" contentType="text/html; charset=GBK" %>
19、
}
else{
out.print("对不起,你输入的用户名或者密码错误!");
}
%>
7、配置Struts-config.xml:
添加formbean的配置,在和标签之间加入:
21、
forward="/index.jsp"/>






