【问题标题】:Why ModelDriven has stopped working in Struts 2为什么 ModelDriven 在 Struts 2 中停止工作
【发布时间】:2017-04-23 17:28:20
【问题描述】:

我在Action 课程中使用过ModelDriven,以前它工作正常,现在它停止工作了。

当我在我的 JSP 中使用 Struts 的 <s:debug> 标记时,我发现以下结果:

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. 我的action中没有定义Model类的单独属性,用1高亮,这些属性如何设置?

  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 代码 sn-p:

<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 内容的:

使用&lt;s:debug/&gt;后,点击后得到[debug]链接,显示如下值:

&lt;s:debug/&gt; 用于列表类别页面,[debug] 链接单击它,显示以下值:

【问题讨论】:

  • ModelDriven 在推送动作后将模型推送到值堆栈的顶部。动作属性,例如model,仍然存在——它不会推送模型而不是动作。除了最简单的情况外,我通常避免使用 ModelDriven,因为我没有发现它过于有用。
  • @DaveNewton 请检查我的更新,以及如何在 以 1 突出显示处设置属性?

标签: java jsp struts2 model-driven valuestack


【解决方案1】:

modelDriven interceptor 将模型推送到 ValueStacktop,因此您在堆栈中同时拥有模型和动作。

ValueStack 是一个由 Struts 框架实现的接口,允许在处理请求时操作数据。它是如何工作的,您可以阅读here

您可以在ValueStack的不同层级上找到具有相同名称/键的属性,搜索的顺序是从top到堆栈向下直到找到值。

ModelDrivenaction 简化了将表单字段映射到模型对象,但如果您需要对同一个操作类执行多个模型,您可能会遇到困难。

您可能在验证、类型转换以及 Struts 框架中与拦截器一起使用的其他功能方面遇到困难。

如果你不能解决你的问题,而不是从实现的接口中删除ModelDriven,或者重新配置拦截器堆栈以禁用modelDriven interceptor,那么你可以不用ModelDriven

【讨论】:

猜你喜欢
  • 2012-07-30
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多