Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序(一)

本系列教程将详细介绍Struts 1.x 的基本原理和使用方法,读者能够参阅Struts 2系列教程》 来比较Struts 1.x Struts 2.x 的相同点和不一样点。
   在这篇文章中将以一个简单的例子
(mystruts) 来演示如何使用MyEclipse 来开发、运行Struts 程序,并给出了解决ActionForm 出现乱码问题的方法。读者能够从本文中了解开发Struts 1.x 程序的基本过程。

1、本文给出的程序要实现什么功能

    mystruts 是一个录入和查询产品信息的程序。为了方便起见,本例中的产品信息表只包括了产品ID 、产品名称和产品价格三个字段。mystruts 的主要功能以下:
  
1. 接受用户输入的产品ID 、产品名称和产品价格。
2. 验证这些字段的合法性。若是某些字段的输入不合法(如未输入产品ID ),程序会forward 到一个信息显示页,并显示出错缘由。
3. 若是用户输入了正确的字段值,程序会将这些字段值保存到数据库中,并显示“保存成功”信息。
4. 用户输入产品名称,并根据产品名称进行模糊查询。若是存在符合要求的产品信息。程序会以表格形式显示这些产品的信息,同时显示记录数。若是未查到任何记录,会显示“没有符合要求的记录! ”信息。

2、编写程序前的准备工做

1. 创建数据库

    在编写程序以前,须要创建一个数据库( struts )和一个表 (t_products) ,创建数据库和表的 SQL 脚本以下所示:

  # 创建数据库struts
  
CREATE   DATABASE   IF   NOT   EXISTS  struts  DEFAULT   CHARACTER   SET  GBK;

  # 创建表t_products
  
CREATE   TABLE   IF   NOT   EXISTS  struts.t_products (
    product_id 
varchar ( 4 NOT   NULL ,
    product_name 
varchar ( 50 NOT   NULL ,
    price 
float   NOT   NULL ,
    
PRIMARY   KEY   (product_id)
  ) ENGINE
= InnoDB  DEFAULT  CHARSET = gbk;

创建一个支持 struts1.x samples 工程

   
MyEclipse 创建一个samples 工程(Web 工程),如今这个samples 工程还不支持Struts1.x (没有引入相应的Struts jar 包、struts-config.xml 文件以及其余和Struts 相关的配置)。然而,在MyEclipse 中这一切并不须要咱们手工去加入。而只须要使用MyEclipse 的【New Struts Capabilities 】对话框就能够自动完成这些工做。
    首先选中samples 工程,而后在右键菜单中选择【MyEclipse > New Struts Capabilities 】,启动【New Struts Capabilities 】对话框。对默认的设置须要进行以下的改动:
(1)将Struts specification改成Struts 1.2。
(2) Base package for new classes 改成struts
(3) Default application resources 改成struts.ApplicationResources

改完后的【New Struts Capabilities 】对话框如图1 所示。


图1
    在设置完后,点击Finish 按钮关闭对话框。在向samples 工程添加支持Struts 的功能后,主要对samples 工程进行了三个操做。
1 )引入了Struts 1.2 jar 包(在samples 的工程树中多了一个Struts 1.2 Libraries 节点)。
2 )在WEB-INF 目录中添加了一个struts-config.xml 文件。文件的默认内容以下面的代码所示:
   <? xml version="1.0" encoding="UTF-8" ?>
  
<! 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  />
    
< form-beans  />
    
< global-exceptions  />
    
< global-forwards  />
    
< action-mappings  />
    
< message-resources  parameter ="struts.ApplicationResources"   />
  
</ struts-config >
3 )在WEB-INF 中的web.xml 文件中添加了处理Struts 动做的ActionServlet 的配置,代码以下:
< servlet >
    
< servlet-name > action </ servlet-name >
    
< servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
    
< init-param >
      
< param-name > config </ param-name >
      
< param-value > /WEB-INF/struts-config.xml </ param-value >
    
</ init-param >
    
< init-param >
      
< param-name > debug </ param-name >
      
< param-value > 3 </ param-value >
    
</ init-param >
    
< init-param >
      
< param-name > detail </ param-name >
      
< param-value > 3 </ param-value >
    
</ init-param >
    
< load-on-startup > 0 </ load-on-startup >
  
</ servlet >
  
< servlet-mapping >
    
< servlet-name > action </ servlet-name >
    
< url-pattern > *.do </ url-pattern >
  
</ servlet-mapping >
    到目前为止,samples 工程已经彻底支持Struts 了。读者能够看到,若是不使用MyEclipse ,那么上面所列出的配置文件的内容都必须手工输入。所以,使用MyEclipse 来开发Struts 程序能够省去不少配置xml 文件的工做。
3、实现程序的首页 (index.jsp)
    首先在<samples 工程目录> 中创建一个mystruts 目录,而后在<samples 工程目录>" mystruts 目录中创建一个index.jsp 文件,这个文件的内容以下。
   < %@ page  pageEncoding ="GBK" % >
  
< %--  引用Struts tag--% >
  
< %@ taglib  uri ="http://struts.apache.org/tags-html"  prefix ="html" % >
  
< html >
      
< head >
          
< title > 主界面 </ title >
      
</ head >
      
< body >
          
< table  align ="center"  cellpadding ="10"  width ="100%" >
              
< tr >
                  
< td  align ="right"  width ="50%" >
                  
< %--  使用Struts tag--% >
                      
< html:link  forward ="newProduct" > 录入产品信息 </ html:link >
                  
</ td >
                  
< td >
                      
< html:link  forward ="searchProduct" > 查询产品信息 </ html:link >
                  
</ td >
              
</ tr >
          
</ table >
      
</ body >
  
</ html >

    MyEclipse 中启动Tomcat (若是Tomcat 处于启动状态,在修改完配置文件后,建议在MyEclipse Servers 页从新发布samples 工程,以使修改生效)。在IE 中输入以下的URL
    咱们发如今输入上面的URL 后,在IE 中并未显示正确的运行结果,而是抛出了以下的异常:
java.net.MalformedURLException: Cannot retrieve ActionForward named newProduct

   
这个异常代表程序并未找到一个叫newProduct forward forward 将在后面详细地讲述)。所以,能够判定,在JSP 中使用forward 时,这个forward 必须存在。下面咱们来添加index.jsp 页面中所使用的两个forward newProduct searchProduct 。这两个forward 分别引向了创建产品信息的页面(newProduct.jsp) 和查询产品信息的页面(searchProduct.jsp) 。咱们能够在struts-config.xml 文件中<struts-config> 节点中添加两个全局的forward ,代码以下:
   < global-forwards >
      < forward  name ="newProduct"  path ="/mystruts/newProduct.jsp"   />
      < forward  name ="searchProduct"  path ="/mystruts/searchProduct.jsp"   />
</ global-forwards >
    上面的代码中所示的newProduct.jsp searchProduct.jsp 目前并不存在(将在之后实现这两个JSP 页面),如今从新输入上述的URL ,会获得如图2 所示的效果。


图2
    若是想让index.jsp 成为默认的JSP 页面,能够在web.xml 中的<welcome-file-list> 节点中加入以下的内容:
  < welcome-file > index.jsp </ welcome-file >
    这时在IE 中只要输入以下的URL 就能够访问index.jsp 页面了。
4、实现添加和查询产品信息页面

   
在本节中主要实现了用于输入产品信息(newProduct.jsp )和查询产品信息(searchProduct.jsp) JSP 页面。
    newProduct.jsp 页面中有一个form ,在form 中含有三个文本框,用于分别输入产品ID 、产品名称和产品价格。
<samples 工程目录>"mystruts 目录中创建一个newProduct.jsp 文件,代码以下:
   <% @ page pageEncoding = " GBK " %>
  
<% @ taglib uri = " http://struts.apache.org/tags-html "  prefix = " html " %>
  
< html >
      
< head >
          
< title > 录入产品信息 </ title >
      
</ head >
      
< body >
          
<% --  向saveProduct动做提交产品信息  -- %>
          
< html:form  action ="saveProduct" >  
              
< table  width ="100%" >
                  
< tr >
                      
< td  align ="center" >
                          产品编号:
                          
< html:text  property ="productID"  maxlength ="4"   />
                          
< p >
                              产品名称:
                              
< html:text  property ="productName"   />
                          
< p >
  
                              产品价格:
                              
< html:text  property ="price"   />
                      
</ td >
                  
</ tr >
                  
< tr >
                      
< td  align ="center" >
                          
< br >
                          
< html:submit  value =" 保存 "   />
                      
</ td >
                  
</ tr >
              
</ table >
          
</ html:form >
      
</ body >
  
</ html >
    searchProduct.jsp 页面中有一个 form ,为了方便起见,在 form 中只提供了一个文本框用于对产品名称进行模糊查询。在 <samples 工程目录 >" mystruts 目录中创建一个 searchProduct.jsp 文件,代码以下:
   <% @ page pageEncoding = " GBK " %>
  
<% @ taglib uri = " http://struts.apache.org/tags-html "  prefix = " html " %>
  
< html >
      
< head >
          
< title > 查询产品信息 </ title >
      
</ head >
      
< body >
          
<% --   向searchProduct动做提交查询请求  -- %>
          
< html:form  action ="searchProduct" >
              
< table  width ="100%" >
                  
< tr >
                      
< td  align ="center" >
                          产品名称:
                          
< html:text  property ="productName"   />
                      
</ td >
                  
</ tr >
                  
< tr >
                      
< td  align ="center" >
                          
< br >
                          
< html:submit  value =" 查询 "   />
                      
</ td >
                  
</ tr >
              
</ table >
          
</ html:form >
      
</ body >
  
</ html >

    如今启动Tomcat ,并使用以下两个URL 来访问newProduct.jsp searchProduct.jsp
IE 中输入上面的两个URL 后,并不能显示出相应的界面,而会抛出 JspException 异常,代表未找到 saveProduct searchProduct 动做。从这一点能够看出,若是在 JSP 中使用 Struts Action ,这些 Action 必须事先在 struts-config.xml 文件中定义,不然, JSP 程序就没法正常访问。在这两个页面所使用的动做( saveProduct searchProduct )将会在下面的部分介绍。