icai項目開發日記(-)

  說來可笑,接到這個小項目的時候還不知道什麼是icai.後來Google 了一下.ICAI--智能計算機輔助教育系統(Intelligent Computer Assisted Instruction 簡稱ICAI).這個項目凝聚了興趣小組的心血.花了不少時間.現在終於成型.雖然有些功能不是很理想,但至少大家一直在努力.這個系統是學習大學本科教材<編譯原理>(清華大學出版)後的我們興趣小組的一次實踐.功能主要包括:文法語言,詞法分析,語法分析和中間代碼生成的常規分析.偶主要負責web頁面和web層的集成.在此記錄一二,全當備忘.
  使用工具:eclipse+myeclipse+tomcat+sql server;
  使用框架:struts+log4+dbcp;

  一.使用tiles 

tiles的典型用法有兩種.無論使用哪種方法.tiles是做爲一個插件放入到struts應用中的.所以必須在struts-config.xml中插入這個插件通常的配置如下:

 

<plug-in className="org.apache.struts.tiles.TilesPlugin" >
      
<!-- Path to XML definition file -->
      
<set-property property="definitions-config"
                      value
="/WEB-INF/tiles-defs.xml" />
      
<!-- Set Module-awareness to true -->
      
<set-property property="moduleAware" 
                      value
="true" />
</plug-in>  

第一種用法是先創建一個公用layout.jsp頁用來佈局.然後在其中引用tiles-defs.xml文件中配置好的元素:
文件layout.jsp:

<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="white">
  
<tr>
    
<td width="24%" height="10"> </td>
    
<td width="76%" height="10"><tiles:insert attribute="header"/></td>
  
</tr>
  
<tr>
    
<td width="24%" height="100%" align="right" valign="top"><tiles:insert attribute="sidebar"/></td>
    
<td width="76%" height="100%" align="left"><div id="body"><tiles:insert attribute="content"/> </div></td>
  
</tr>
</table>
<table width="100%" border=0 align="center" cellPadding=0 cellSpacing=0 bgcolor="WHITE" margin-bottom=0>
  
<tr>
    
<td height="64"><tiles:insert attribute="footer"/> </td>
  
</tr>
</table>

文件tiles-defs.xml就像一個零件裝配的工廠,通過它可以組裝各種網頁,這對於見面風格的統一很是方便.重用的是它可以繼承已有的組件以實現複用.

<?xml version="1.0" encoding="ISO-8859-1" ?>

 
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd"
>

<tiles-definitions>
   
<definition  name="sidebar-definition"   path="/WEB-INF/tiles/common/sidebar-layout.jsp">
      
<put name="bottom" value="/WEB-INF/tiles/common/sidebar-links.jsp"/>
   
</definition>  
   
<definition  name="base-definition"  path="/WEB-INF/tiles/common/layout.jsp">
      
<put name="top-header"  value="/WEB-INF/tiles/common/top-header.jsp"/>
      
<put name="sidebar" value="sidebar-definition" type="definition"/>
      
<put name="title"  value="/WEB-INF/tiles/common/title.jsp"/>
      
<put name="header"  value="/WEB-INF/tiles/common/header.jsp"/>
      
<put name="content" value=""/>
      
<put name="footer"  value="/WEB-INF/tiles/common/footer.jsp"/>
   
</definition>
<!--
    for defaul web
-->
   
<definition  name="index-definition"   extends="base-definition">
      
<put name="content" value="/WEB-INF/tiles/default/Welcome.jsp"/>   
   
</definition>
</tiles-definitions>


當然用tiles裝訂的網頁是不能直接在地址欄中用localhost:8080/myapp/index-definition 訪問的.

訪問的方法是是struts-config.xml中設置<action-mapping>屬性進行訪問:

<action-mappings>
    
<action path="/index"  forward="index-definition"/>
</action-mappings>


此時即可用http://localhost:8080/myapp/index.do 進行訪問.

  第二種方法其實和第一種方法大同小異.思想是在每一個.jsp頁面中佈局裝訂各個元素.例如:
index.jsp

<%@ page contentType="text/html;charset=GB2312"%>
<%@ taglib prefix="html" uri="/WEB-INF/struts-html.tld"%>
<%@ taglib prefix="bean" uri="/WEB-INF/struts-bean.tld"%>
<%@ taglib prefix="tiles" uri="/WEB-INF/struts-tiles.tld"%>

<tiles:insert page="/tile/layout.jsp" flush="true">
    
<tiles:put name="header" value="/tile/header.jsp"/>
    
<tiles:put name="navigation" value="/tile/navigation.jsp"/>
    
<tiles:put name="product" value="/tile/adminlogin.jsp"/>
    
<tiles:put name="page" value="/tile/page.jsp" />
    
<tiles:put name="footer" value="/tile/footer.jsp"/>
</tiles:insert>

它將用到layout.jsp這個文件:

<TABLE width="100%"border="0" align="center" cellpadding="0" cellspacing="0" >
            
<TR>
                
<TD colspan="3" align="center">
                    
<tiles:insert attribute="header" />
                
</TD>
            
</TR>
            
<TR>
                
<TD colspan="3" align="right">
                    
<tiles:insert attribute="navigation" />
                
</TD>
            
</TR>
            
<TR height="100%">
                
<TD width="1%" height="1"></TD>
                
<TD width="99%" align="center">
                    
<tiles:insert attribute="product" />
                
</TD>
                
<TD width="1%"></TD>
            
</TR>
            
<TR>
                
<TD width="1%" height="1"></TD>
                
<TD width="99%" align="right">
                    
<tiles:insert attribute="page" />
                
</TD>
                
<TD width="1%"></TD>
            
</TR>
            
<TR>
                
<TD colspan="3" align="center">
                    
<tiles:insert attribute="footer" />
                
</TD>
            
</TR>
 
</TABLE>

當然這時候就可以直接在地址欄中用http://localhost:8080/myapp/index.jsp 訪問了.使用tiles的好處就是可以靈活的組裝網頁的各個模塊.是實現複用的好工具.

  

發佈了15 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章