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 )將會在下面的部分介紹。
相關文章
相關標籤/搜索