0

顧客のすべての詳細を含むJSPページを表示する custprofileviewアクションがあり、私のJSPではすべてのフィールドが私のようなものです

<s:textfield name="custprofileVO.email" value="%{custprofileVO.email}" />
<s:textfield name="custprofileVO.phone" value="%{custprofileVO.phone}" />

そして、 Action を呼び出す送信ボタンがありますupdatecustprofile

実際updatecustprofileには、プロパティを直接マッピングする代わりに、 private CustprofileVO custprofileVO; setter と getter を持つメンバー変数があります。

CustprofileVOクラスには のようなフィールドがありemailphone他のすべてのフィールドにはセッターとゲッター メソッドがあります。

問題は次のとおりです。updatecustprofileアクションではPrepareableインターフェイスを実装しています。prepare()メソッドの実装ではcustprofileVO.setDefaultID("Select");、さらに4つのフィールドを設定していますが、送信ボタンをクリックしてプログラムを実行するとNPE、最初の行に表示されますcustprofileVO.setDefaultID("Select");

フレームワークがインスタンス化されていないよう CustprofileVO custprofileVOです。custprofileVOフィールドの設定のすぐ上で手動でインスタンス化する と(そうcustprofileVO = new CustprofileVO()することで機能します。問題は-理想的にはstruts2フレームワークが実行していないインスタンスを提供する必要があり、理由を理解したい.

さらに、prepare メソッドで手動で設定 custprofileVOすると機能しますが、フィールド名が XML を使用して検証を適用し custprofileVO.email、その検証とその検証 も適用しcustprofileVO.phoneました。

送信ボタンをクリックして検証しようとすると、検証が実行されますが、すべてのテキストボックスのデータが空白になるため、画面にすべてのフィールドのメッセージが表示されます。

データが削除されるのはなぜですか?

4

1 に答える 1

0

JSP からのオブジェクトを自分でインスタンス化しないでください。

の前にによってprepare()実行されるメソッドでそれを取得するには、特別な stack を使用する必要がありますPrepare Interceptor Param InterceptorparamsPrepareParamsStack

 <!-- An example of the paramsPrepareParams trick. This stack
             is exactly the same as the defaultStack, except that it
             includes one extra interceptor before the prepare interceptor:
             the params interceptor.

             This is useful for when you wish to apply parameters directly
             to an object that you wish to load externally (such as a DAO
             or database or service layer), but can't load that object
             until at least the ID parameter has been loaded. By loading
             the parameters twice, you can retrieve the object in the
             prepare() method, allowing the second params interceptor to
             apply the values on the object. -->
        <interceptor-stack name="paramsPrepareParamsStack">
            <interceptor-ref name="exception"/>
            <interceptor-ref name="alias"/>
            <interceptor-ref name="i18n"/>
            <interceptor-ref name="checkbox"/>
            <interceptor-ref name="multiselect"/>
            <interceptor-ref name="params">
                <param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
            </interceptor-ref>
            <interceptor-ref name="servletConfig"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="chain"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="fileUpload"/>
            <interceptor-ref name="staticParams"/>
            <interceptor-ref name="actionMappingParams"/>
            <interceptor-ref name="params">
                <param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
            </interceptor-ref>
            <interceptor-ref name="conversionError"/>
            <interceptor-ref name="validation">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
            <interceptor-ref name="workflow">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
        </interceptor-stack>
于 2014-04-07T08:34:29.200 に答える