My Java Blog

Personal Java Blog

Multiple Models in struts

在Struts框架下大型Web应用程序的开发 刘正仁


http://www.googles.com.cn 时间:2005-8-7 18:35:00 来源:互联网络 票数:0等级:点击:6

(应用程序多模块团队的开发)

struts1.1允许将单个Struts应用划分成几个模块,每个模块有自己的Struts配置文件,JSP页面,Action等等。这个新特性是为了解决大中型的开发队伍抱怨最多的一个问题,即为了更好的支持并行开发允许多个配置文件而不是单个配置文件。

下面就近我在开发知识库时顺便做的一个样例说一下自己的体会,从应用程序的结构,配置,模块之间的跳转三个方面做些说明,在说明之前,解释我下面所用到的两个术语避免产生岐义。

多模块应用程序:在本文中是指开发Struts应用程序时,不同的应用模块独立地配置自己的struts-config.xml文件。

单模块应用程序:在本文中是指开发Struts应用程序时,不同的应用模块只有一个struts-config.xml文件。(实际上也是多个模块的)

我的Struts应用程序名为CliffordWeb,下面有两个模块:样例模块(Exsample)和知识库模块(Repository),样例模块是为了说明问题而添加的。

一、在多模块应用程序开发时的应用程序目录结构。

多模块应用程序与单模块应用程序目录结构基本上是一样的。所以如果我们要将现有的单模块应用程序转化为多模块应用程时,目录结构几乎不需要做任何修改。(最好每个模块的目录都是一级目录)

CliffordWeb

……. WEB-INF

……. ……. …….

……. ……. struts-config.xml

……. ……. struts-config-Repository.xml

……. ……. struts-config-Exsample.xml

……. ……. …….

……Exsample(应用模块)

……. …….Jsp

……Repository(应用模块)

……. ……. Jsp

…….Images

…….JS

…….Include

…….

从上面的目录结构我们可以看到它基本上和我们目前开发的结构是一样的。只是在WEB-INF目录中多了两个Struts配置文件(struts-config-Repository.xmlstruts-config- Exsample.xml)。这两个配置文件分别对应知识库和登录两个模块。

二、相关的配置文件及作用

在多模块应用程序开发时,我们需要配置Web.xml及各个应用模块的Struts配置文件,下面我给出各个配置文件的样例。(注意蓝色的部分)

1、   web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

  <display-name>CliffordWeb</display-name>

  <filter>

    <filter-name>SetCharacterEncoding</filter-name>

    <filter-class>com.jwz.common.filters.SetCharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>GBK</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>SetCharacterEncoding</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <servlet>

    <servlet-name>RepositoryCategoryService</servlet-name>

    <servlet-class>com.clifford.repository.servlet.CategoryService</servlet-class>

  </servlet>

  <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>config/Repository</param-name>

      <param-value>/WEB-INF/struts-config-Repository.xml</param-value>

</init-param>

    <init-param>

      <param-name>config/Logon</param-name>

      <param-value>/WEB-INF/struts-config-Logon.xml</param-value>

</init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

  </servlet>

  <servlet>

    <servlet-name>debugjsp</servlet-name>

    <description>Added to compile JSPs with debug info</description>

    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>

    <init-param>

      <param-name>classdebuginfo</param-name>

      <param-value>true</param-value>

    </init-param>

    <load-on-startup>3</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>RepositoryCategoryService</servlet-name>

    <url-pattern>/Repository/CategoryService</url-pattern>

  </servlet-mapping>

  <servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  <servlet-mapping>

    <servlet-name>debugjsp</servlet-name>

    <url-pattern>*.jsp</url-pattern>

  </servlet-mapping>

  <taglib>

    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri> 

    <taglib-location>/WEB-INF/struts-template.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>

  </taglib>

</web-app>

2、   struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

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

<struts-config>

  <global-forwards>

    <forward name="error" path="/error.jsp" />

    <forward name="root" path="/root.jsp" />

    <forward name="index" path="/index.htm" />

    <forward name="login" path="/startlogin.jsp" />

  </global-forwards>

  <action-mappings>

       <action   path="/ModuleSwitch"   type="org.apache.struts.actions.SwitchAction"/>

  </action-mappings>

  <message-resources parameter="com.cf.oa.ApplicationResources" />

  <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>

3、   struts-config-Repository.xml

<?xml version="1.0" encoding="UTF-8"?>

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

<struts-config>

  <action-mappings>

    <action path="/Knowledge" type="com.clifford.repository.actions.KnowledgeAction">

      <forward name="0" path="/Script/GenericList.jsp" />

      <forward name="32" path="/Script/GenericInfo.jsp" />

      <forward name="10" path="/Script/MyDocList.jsp" />

      <forward name="30" path="/Script/DocEditForm.jsp" />

      <forward name="20" path="/Script/AdminPriCatList.jsp" />

    </action>

  </action-mappings>

</struts-config>

4、   struts-config-Exsample.xml

<?xml version="1.0" encoding="UTF-8"?>

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

<struts-config>

  <action-mappings>

    <action path="/Index" forward="/Script/Index.jsp"/>

  </action-mappings>

</struts-config>

从上面的配置文件中我们可以看到在web.xml中,添加了两个应用模块的自己的struts配置文件,其他的没有做任何变动。我们注要一下应用模块配置文件的规则,对于应用模块XXXX,那么它的struts配置文件为struts-config-XXXX.xml。如

    <init-param>

      <param-name>config/Repository</param-name>

      <param-value>/WEB-INF/struts-config-Repository.xml</param-value>

</init-param>

建议模块名,配置文件名 以及应用模块的目录名保持一致。

我们看一下各个Struts配置文件的作用:

struts-config.xml 文件是Struts的默认配置文件(当然这个文件也不是必须的),在我样例中主要是配置一些全局性的东西。如全局ForwardExceptionResource等等。在调用Action时,如果在其他模块配置文件中找不到,会自动在这个文件中找相应的Action。另外我将SwitchActin也放在这个文件中(等下我会说到这个Action的作用)。

struts-config-Repository.xmlstruts-config-Exsample.xml主要是用于各个应用模块的配置,在这个文件中可以独立地配置这个应用模块要用到的ActionActionForwordResource等等。在不同的文件中充许重名。

三、在各个模块中的Action跳转

如果要从一个模块的Action跳转到另一个模块的Action,有两种方法:

一种是直接指定路径,如<a href="/CliffordWeb/Exsample/Index.do">转到Exsample</a>

另一种是使用SwitchAction,这也就是我为什么要在struts-config.xml定义SwitchAction的原因。用法如下

<a href="/ CliffordWeb /ModuleSwitch.do?prefix=/Exsample&page=/Index.do">转到ExsampleSwitchAction</a>

两种方法原理上是一样的,可以根据自己的习惯来选择。
The previous link :
http://www.googles.com.cn/info/2005/058817141912652.htm

posted on 2005-11-16 13:37 Eric Yang 阅读(56) 评论(0)  编辑  收藏 所属分类: Struts


只有注册用户登录后才能发表评论。


网站导航: