struts 多模塊配置

來源:http://blog.csdn.net/qking93415981/archive/2007/07/23/1702980.aspx

struts 多模塊配置

新一篇: struts 中的dispatch學習 | 舊一篇: Java New I/O的使用

 1、在web系統根目錄下建立與模塊名相同的文件夾

2、在WEB-INFO中建立同樣的與模塊名一一對應的文件夾,存放配置文件

3、web.xml中配置 

    <servlet>
        
<servlet-name>action</servlet-name>
        
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

        
<!-- Default -->
        
<init-param>
            
<param-name>config</param-name>
            
<param-value>/WEB-INF/struts-config.xml</param-value>
        
</init-param>

        
<!-- Exercise module -->
        
<init-param>
            
<param-name>config/exercise</param-name>
            
<param-value>/WEB-INF/exercise/struts-config.xml</param-value>
        
</init-param>

        
<!-- File Upload module -->
        
<init-param>
            
<param-name>config/upload</param-name>
            
<param-value>/WEB-INF/upload/struts-config.xml</param-value>
        
</init-param>

        
<!-- Validator module -->
        
<init-param>
            
<param-name>config/validator</param-name>
            
<param-value>/WEB-INF/validator/struts-config.xml,
                /WEB-INF/validator/struts-config-bundles.xml,
                /WEB-INF/validator/struts-config-i18nVariables.xml,
                /WEB-INF/validator/struts-config-type.xml,
                /WEB-INF/validator/struts-config-validwhen.xml
            
</param-value>
        
</init-param>

        
<!-- Dispatch Action module -->
        
<init-param>
            
<param-name>config/dispatch</param-name>
            
<param-value>/WEB-INF/dispatch/struts-config.xml</param-value>
        
</init-param>

        
<init-param>
            
<param-name>debug</param-name>
            
<param-value>2</param-value>
        
</init-param>
        
<init-param>
            
<param-name>detail</param-name>
            
<param-value>2</param-value>
        
</init-param>
        
<load-on-startup>2</load-on-startup>
    
</servlet>

 

 該系統中一共配置了四個模塊

4、在WEB-INFO下與模塊對應的文件夾中添加該模塊的struts配置文件,名稱沒有限制,可以在web.xml中進行指定。

5、在web系統根目錄下的與模塊對應的文件夾中放置對應的jsp文件。

6、注意:4、中action路徑都是相對於本模塊的

WEB-INFO/dispatch/struts-config.xml

        <action path="/dispatch-submit" 
                type
="org.apache.struts.webapp.dispatch.DispatchExampleAction"
                parameter
="dispatchMethod"
                name
="testForm"
                scope
="request">
            
<exception key="dispatch.NoSuchMethodException"
                       type
="java.lang.NoSuchMethodException"
                       path
="/dispatch.jsp"/>
            
<exception key="dispatch.ServletException"
                       type
="javax.servlet.ServletException"
                       path
="/dispatch.jsp"/>
            
<forward name="success" path="/dispatch.jsp"/>
        
</action>

在jsp中對應的action的寫法

          <html:form action="dispatch-submit" style="display:inline">
              
<input type="hidden" name="dispatchMethod" value="doFoo" />
              
<html:submit><bean:message key="button.foo.label" /></html:submit>
          
</html:form>

實際上客戶端的實際html代碼是:

          <form name="testForm" method="post" action="/strtus-eg/dispatch/dispatch-submit.do" style="display:inline">
              
<input type="hidden" name="dispatchMethod" value="doFoo" />
              
<input type="submit" value="Foo Button">
          
</form>

注:a、/strtus-eg/dispatch/dispatch-submit.do中 /struts-eg是prj名稱,dispatch即對應的模塊名

        b、xml配置文件中:parameter="dispatchMethod" ,

           jsp中<input type="hidden" name="dispatchMethod" value="doFoo" />,這種寫法表示在對應的action中調用doFoo這個方法進行請求處理,而不是默認的execute。如果客戶端沒有給action中parameter="dispatchMethod" 中的參數dispatchMethod賦值那麼就會以這個參數名"dispatchMethod"去找對應的方法。

     c、模塊之間的跳轉 

<html:link module="/exercise" page="/welcome.do">
<html:link module="/exercise" action="/welcome">

 module指明是哪一個模塊,page與action意思相同,action會自動給你加上.do.如果要跳到默認模塊[即WEB-INFO文件夾下面]設置module="/" 即可

       d、配置文件中設置從該模塊跳到別的模塊forward中 path="/../validator/index.jsp"      表示從該模塊跳到validator模塊中的index.jsp頁面,其中path必須以"/"開始,“/”表示的是該模塊的根目錄。 

        <action path="/dispatch-noparam" 
                type
="org.apache.struts.webapp.dispatch.DispatchExampleAction"
                name
="testForm"
                scope
="request">
            
<forward name="success" path="/../validator/index.jsp"/>
        
</action>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章