0

struts2.1.6 + Spring 2.5 を使用しています。アプリケーションには 4 つのモジュールがあります。

  1. 登録モジュール
  2. 管理モジュール
  3. 見積もりモジュール
  4. ロケーションモジュール。

登録モジュールでは、顧客は自分自身を登録することができ、登録後にのみ残りの 3 つのモジュールにアクセスできるようになります。

呼び出されているアクションが登録モジュールに属している場合は通常どおりに機能しますが、呼び出されているアクションが残りの 3 つのモジュールに属している場合は、最初にユーザーがログインしており、セッションがないかどうかを確認する必要があります。タイムアウトしました。はいの場合は、通常どおり続行する必要があります。それ以外の場合は、ログイン ページにリダイレクトする必要があります。

調査を通じて、インターセプターをこの目的に使用できることがわかりましたが、先に進む前に、専門家からフィードバックを得たほうがよいと考えました。

それがどのように行われるべきかを提案し、可能であればコードの提案をいくつか入れてください。

これが私のstruts.xmlファイルです (struts.xml には、各モジュールに属する 4 つの異なる構成ファイルが含まれています)。

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

残りの 2 つの struts2 xml 構成ファイルにも同じコードがあります。4つの構成ファイルすべてで、パッケージ名が異なる同じ名前空間を使用しました(ご覧のとおり)

4

1 に答える 1

0

注:標準的な方法では、パッケージごとに異なる名前空間を使用します。たとえば、adminパッケージの場合は「/ my_company/admin」などです。

インターセプターを使用するのが正しいアプローチです。それは、認証をアクション自体から切り離します。2つの異なるインターセプタースタックを定義できます。1つはユーザーがログインする必要があり、もう1つはログインしない必要があります。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>
于 2010-06-10T01:26:50.030 に答える