2010-06-09 43 views
0

我正在使用struts2.1.6 + Spring 2.5我在我的應用程序中有四個模塊。如何在struts2中進行URL驗證

  1. 註冊模塊
  2. 管理模塊
  3. 報價模塊
  4. 位置模塊。

在註冊模塊中,客戶可以註冊自己,只有在註冊之後,他才能訪問其餘三個模塊。

我想實現類似於如果被調用的動作屬於註冊模塊,它將正常工作,但如果被調用的動作屬於這三個模塊的其餘部分,則應首先檢查用戶是否已登錄並且會話沒有超時。如果是的話,它應該正常進行,否則它應該重定向到登錄頁面。

通過研究,我發現攔截器可以用於此目的,但在繼續之前,我認爲最好從專家那裏獲得一些反饋意見。

請建議它應該如何做,如果可能的話提出一些代碼建議。

這裏是我的struts.xml 文件(該struts.xml中包含屬於每個模塊四個不同的配置文件):

<struts> 
    <include file="struts-default.xml" /> 
    <constant name="struts.i18n.reload" value="false" /> 
    <constant name="struts.objectFactory" value="spring" /> 
    <constant name="struts.devMode" value="false" /> 
    <constant name="struts.serve.static.browserCache" value="false" /> 
    <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
    <constant name="struts.multipart.maxSize" value="10000000" /> 
    <constant name="struts.multipart.saveDir" value="C:/Temporary_image_location" /> 

    <include file="/com/action/mappingFiles/registration_config.xml" /> 
    <include file="/com/action/mappingFiles/admin_config.xml" /> 
    <include file="/com/action/mappingFiles/quote.xml" /> 
    <include file="/com/action/mappingFiles/location_config.xml" /> 

</struts> 

樣品registration_config.xml文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <package name="registration" extends="struts-default" 
     namespace="/my_company"> 

     <action name="LoginView" class="registration" method="showLoginView"> 
      <result>....</result> 
      <result name="input">...</result> 
     </action> 
     </package> 
</struts> 

樣本admin_config.xml文件是:

<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> 
    <struts> 
     <package name="admin" extends="struts-default" 
      namespace="/my_company"> 

      <action name="viewAdmin" class="admin" method="showAdminView"> 
       <result>....</result> 
       <result name="input">...</result> 
      </action> 
      </package> 
    </struts> 

在兩個struts2 xml配置文件的其餘部分有相同的代碼。我在所有四個配置文件中使用了相同的命名空間,並且包名不同(正如你所見)

回答

0

注意:標準做法是爲每個包使用不同的命名空間, 「/ my_company/admin」用於管理包等。

使用攔截器是正確的方法:它將身份驗證與操作本身分離。您可以定義兩個不同的攔截器堆棧,一個需要用戶登錄,另一個不需要。首先從struts-default.xml複製攔截器堆棧,然後根據需要進行自定義。這些定義可以放置在一個抽象的基本軟件包:

<package name="my-base" abstract="true" extends="struts-default"> 
    <interceptors> 
     <interceptor-stack name="login-required"> 
      <interceptor-ref name="exception"/> 
      <interceptor-ref name="alias"/> 
      <!-- etc --> 
     </interceptor-stack> 
     <interceptor-stack name="login-not-required"> 
      <!-- etc --> 
     </interceptor-stack> 
    </interceptors> 
</package> 

那麼你其他的包只需要擴展這個基礎包:

<package name="admin" extends="my-base" namespace="/my_company/admin"> 
    <default-interceptor-ref name="login-required"/> 

    <!-- actions defined here --> 
</package> 
+0

所以,這意味着我需要創建一個自定義攔截器可以說LoginInterceptor並在攔截器堆棧名稱「需要登錄」我需要添加該攔截器。我對嗎? – 2010-06-10 14:54:14

+0

沒錯。我想你可以在網上找到一些教程,例如http://www.vitarara.org/cms/struts_2_cookbook/creating_a_login_interceptor – 2010-06-11 01:23:40