0

私はModelDriven自分のActionクラスで使用しましたが、以前は正常に機能していましたが、現在は機能しなくなりました。

JSP で Struts のタグを使用すると<s:debug>、次の結果が見つかりました。

StrutsValueStackデバッグ:

Struts ValueStack デバッグ

JSPページの値に次のようにアクセスしています:

<s:property value="categoryName"/>
<s:property value="categoryId"/>       // I typed here

私がそれらにアクセスした場合:

<s:property value="category.categoryName"/>
<s:property value="category.categoryId"/>

私の質問は次のとおりです。

  1. プロパティを 2 回取得するのはなぜですか?
  2. それらが複数回取得されるのを避ける方法は?
  3. 多くの人が避けるように提案しているのを見てきましたがModelDriven、なぜですか?

アップデート:

  1. 実際、私はプロパティを 3 回取得しています。3 つ目のプロパティをハイライトするのを忘れていました。私のActionクラスを見てください。

  2. 1 で強調表示されModelたアクションでクラスの個別のプロパティを定義していません。これらのプロパティを設定するにはどうすればよいですか?

  3. 何か、私は実装時に間違っていますModelDrivenか?

ここに私のモデルがありCategoryActionます:

public class CategoryAction extends ActionSupport implements ModelDriven<Category>,
                                Preparable, SessionAware, ServletRequestAware, ServletContextAware {

    private static final long serialVersionUID = 1L;
    private Category category;
    private Category [] categories;

    private ServletContext servletContext;
    private Map<String, Object> session;
    private ServletRequest request;

    @Override
    public Category getModel() {
        return this.category;
    }
    @Override
    public void prepare() throws Exception {
        this.category = new Category();
        if( this.jmain == null )
            this.jmain = new Jmain();
    }
    public void prepareCrudCategory() throws Exception {
        this.categoryService = new CategoryService();
    }
    @Override
    public String execute() throws Exception {
        System.out.println("----------CategoryAction#execute()-----------");
        if( this.category.getCategoryId() == 0)
            this.category = this.jmain.getCategory( 1 );        //Get Main Category
        else
            this.category = this.jmain.getCategory( this.category.getCategoryId() );
        System.out.println(this.category.toString());
        return super.execute();
    }


    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
    public Category[] getCategories() {
        return categories;
    }
    public void setCategories(Category[] categories) {
        this.categories = categories;
    }

}

更新 2:

これは私の JSP コード スニペットです。

<form action="add-category" id="id-add-id-entry-form" name="addIdEntry" method="post">
    <input type="hidden" id="opType" name="opType" value='<s:property value="opType"/>'> 

    <br /> <br />
    <center>
        <span id="id-message-span">
            <s:if test="hasActionMessages()">
                <h3 style="font-size: 22px; color: #FD0006; text-shadow: 2px 2px 3px rgba(255, 255, 255, 0.1); margin-left: 330px;">
                    <s:actionmessage />
                </h3>
            </s:if>
            <s:if test="hasActionErrors()">
                <h3 style="font-size: 16px; color: #FD0006; text-shadow: 2px 2px 3px rgba(255, 255, 255, 0.1); margin-left: 330px;">
                    <s:actionerror />
                </h3>
            </s:if>
        </span>
        <div id="id-id-entry-div" class="class-id-entry-div class-center">
            <fieldset style="height: 100%; border-style: solid; border-width: 2px; border-color: #FEC458;"
                        >
                <legend class="PageTitleUpperCase"> Add Category </legend>
                <table cellspacing="5" cellpadding="2" border="0" width="65%" class="class-center">
                    <tr>
                        <td>
                            <span style="color: red">*</span>
                            <label class="Label_Green"> Patent Category Name :</label>
                            <s:if test=""></s:if>
                        </td>
                        <td>
                            <s:property value="opType"/><br>
                            <s:property value="categoryId"/>
                            <s:property value="categoryName"/>
                            <s:if test="%{opType == 0}">
                                <%-- <s:property value="categoryName"/> --%>
                                <input type="hidden" name="parentCategoryId" value='<s:property value="categoryId"/>' >
                                <label class="Label_Green">
                                    <s:property value="categoryName"/>
                                </label>
                            </s:if>
                            <s:else>
                                <%-- <s:property value="parentCategoryName"/> --%>
                                <input type="hidden" name="parentCategoryId" value='<s:property value="parentCategoryId"/>' >
                                <label class="Label_Green">
                                    <s:property value="parentCategoryName"/>
                                </label>
                            </s:else>
                            
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span style="color: red">*</span>
                            <label class="Label_Green"> Category Name :</label>
                            <s:if test=""></s:if>
                        </td>
                        <td >
                            <input type="hidden" name="categoryId" 
                                <s:if test="%{opType == 0}">value='0'</s:if>
                                <s:else>value='<s:property value="categoryId"/>'</s:else>
                            >
                                 
                            <input id="id-category-name-text" type="text" name="categoryName"
                                required="required" size="40" placeholder="Enter Category Name Here..."
                                <s:if test="%{opType == 1}">value='<s:property value="categoryName"/>'</s:if> >
                        </td> 
                    </tr>
                    <tr>
                        <td></td>
                        <td align="left"><input type="submit" id="submit_img"
                            <s:if test="opType != 0"> value='Update'</s:if>
                            <s:else> value='Submit' </s:else> >
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>
        <s:debug/>
    </center>
</form>

ValueStackデバッグタグを使用してコンテンツを取得する方法:

を使用した後<s:debug/>[debug]クリックするとリンクが表示され、次の値が表示されます。

デバッグタグを使用した値スタックの内容

<s:debug/>リスト カテゴリ ページの場合、[debug]クリックするとリンクが表示され、次の値が表示されます。

リストカテゴリページ

4

1 に答える 1

1

modelDrivenインターセプターはモデルをのtopにプッシュするValueStackため、モデルとアクションの両方がスタックに含まれます。

ValueStackStruts フレームワークによって実装されるインターフェースであり、リクエストの処理中にデータを操作できます。どのように機能するかは、ここから読むことができます。

のさまざまなレベルで、同じ名前/キーを持つプロパティを見つけることができます。検索の順序は、値が見つかるまで、スタックの下ValueStackから順に検索されます。top

ModelDrivenaction を使用すると、フォーム フィールドをモデル オブジェクトに簡単にマッピングできますが、同じアクション クラスに対して複数のモデルを実行する必要がある場合は、問題が生じる可能性があります。

検証、型変換、インターセプターで動作する Struts フレームワークのその他の機能で問題が発生する場合があります。

ModelDrivenまた、実装されたインターフェイスから削除したり、インターセプター スタックを再構成してインターセプターを無効にしたりmodelDrivenずに問題を解決できない場合は、ModelDriven.

于 2016-12-08T18:07:08.867 に答える