网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.JProfiler跟踪和检查系统性能的好.
.成员函数的说明和使用.
.JBuilder2005实现重构之重命名.
.构建J2EE应用程序的任务列表.
.RMI和CORBA的比较.
.JBuilder2005创建开发文档之创建.
.实现真正的J2EE便携式应用.
.Java中文问题详解(推荐).
.用Java实现断点续传.
.引用一个能发email的JavaMail的例.
.使用Java Web Services Develope.
.Jdk和Tomcat的安装与配置.
.解释器-Java.exe.
.程序设计:用JAXB生成一个XML文档.
.浅析Spring提供的事务管理方法.
.构建自己的基于Java的超级计算机.
.POJO应用架构:Spring与EJB 3.0的.
.Java技巧101applet间通信的替代方.
.Java基本教学:线程入门.
.利用实体EJB来避免性能缺陷:一种.

Eclipse开发struts完全指南二(全)

发表日期:2008-1-5



  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,点击Finish按钮。
  
  编辑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()
  {
  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;
  }
  }
  
  注重,这里的两个属性分别对应我们jsp中form中的两个输入控件的名称,为什么这样做,可以去看struts的帮助文档了,我就不具体说了,还有form类再写完属性后,get和set方法可以通过eclipse的source中的命令来自动生成,在右键菜单中,也不具体说了,去网上查资料吧,关于eclipse的使用有很多的文档。
  
  七、安装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、创建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 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转向的,你也可以按照struts中提供的空白例程struts-blank.war中的做法进行转向,可以比较一下会有收获的。
  
  7、创建登录成功页面
  
  同创建index.jsp页面相同,我们创建welcome.jsp页面,均使用默认设置。并编辑其内容如下:
  
  <%@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("对不起,你输入的用户名或者密码错误!");
  }
  %>
  </body>
  </html>
  
  8、增加Struts-config.XML中的配置
  
  添加formbean的配置,在和标签之间加入:
  
  <form-bean
  name="loginForm"
  type="com.is.form.LoginForm"/>
  
  添加jsp文件的映射,在和标签之间加入:
  
  <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"/>
  
  修改后的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>
  
  到此我们可以运行测试程序了。
  
  9、运行测试程序
  
  右键点击testweb工程根目录,点击菜单中的Tomcate project->update context definition,将工程部署进tomcat,成功后会提示操作成功。
  
  点击菜单栏中的雄猫图标启动tomcat,然后在IE地址栏中输入http://localhost:8080/testweb/index.do,我们会看到index.jsp的页面内容。
上一篇:Eclipse开发struts完全指南一(全) 人气:1636
下一篇:用JBoss Eclipse IDE开发J2EE应用系统 人气:675
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐