【问题标题】:Struts1 and ActionForm inheritance, mess of dataStruts1和ActionForm继承,数据乱七八糟
【发布时间】:2011-03-10 22:43:02
【问题描述】:

我在使用 Struts1 ActionForm bean 时遇到问题。请查看我的 struts-config 的一部分:

<!-- RuleSearchForm is a sublass of RuleForm -->
    form-beans>
            <form-bean name="ruleForm" 
                type="forms.RuleForm">
            </form-bean>
            <form-bean name="ruleSearchForm" 
                type="forms.RuleSearchForm">
            </form-bean>
        </form-beans>
    <!-- Mappings -->
    <action path="/RuleList" 
            type="actions.RuleList"
            name="ruleSearchForm"
            scope="session"
            validate="false">
        <forward name="success" path="/html/view/RuleList.jsp"></forward>
    </action>
    <action path="/RuleCreate" 
            type="actions.RuleCreate" 
            name="ruleForm" 
            scope="request" 
            validate="false">
        <forward name="success" path="/html/view/CreateUpdateRule.jsp"></forward>
    </action>

还有我的部分 Actionform bean 代码:

public class RuleForm extends ActionForm {
    protected Integer   crid;       
    protected List levels;        
    /** Some other fileds go here */

    public Collection getLevels(){
        if(levels == null){
            levels = DAOClass.getLevels();
            Collections.reverse(levels);
        }
        return levels;
    }

    /** Other getters/setters go here */
}

public class RuleSearchForm extends RuleForm{

    /**
     * Avoid filter reset. If needs to be reset use {@link RuleForm#resetBeanFields()} directly.
     * */
    public void reset(ActionMapping mapping, HttpServletRequest request) {

    }

    /**
     * Add empty value. User should have an opportunity not to set value for this field.
     * */
    public Collection getLevels(){
        if(levels == null || levels.size() == 0){
            super.getLevels();
            levels.add(0, new Level());
        }
        return levels;
    }
}

问题是:

  1. 用户转到 /RuleList.do 并查看规则列表。 ruleSearchForm 用作将搜索参数传输到 /RulesList.do 操作的 bean。最初 tihs bean 是空的,只有 getLevels() 返回“空值”+从超类方法获得的级别列表。

  2. 用户转到/CreateRule.do,ruleForm 用于收集用户输入。 levels 属性用于选择框。我得到了级别列表+空行。这个空行没有添加到 RuleForm(命名为 ruleForm)中。它添加了 RuleForm 的子类。为什么没有静态字段的超类 ActionForm bean 使用其他名称从它的子类实例中获取值???

  3. 如果用户保存 Rule 并被重定向到 /RuleList.do,他会看到填充(即填充)搜索表单(“ruleSearchForm”),其中包含来自“ruleForm”的值。

这是什么意思?请帮忙,我不明白 ActionForm Beans 之间的这些乱七八糟的数据

更新: 现在,我更改了 FormAction bean 的继承。我已经介绍了 BaseFormBean。这个 BaseFormBean 有两个孩子:RuleForm 和 RuleSearchForm。 它没有帮助。一个 bean 的仍然属性被移动到另一个。

我的jsp代码: CreateUpdateRule.jsp:

<html:form action="/RuleSave.do">
<html:hidden property="crid"/>
<table border="0" cellpadding="2" cellspacing="0">
    <tr>
        <td><bean:message key="rule.levelId"/></td>
        <td><html:select property="levelId">
                <html:optionsCollection  property="levels" value="clid" label="name" />
            </html:select>
    </tr>
    <tr>
        <td><bean:message key="rule.timeStart"/></td>
        <td><html:text property="timeStartStr"/></td>
    </tr>   
    <tr>
        <td><bean:message key="rule.timeEnd"/></td>
        <td><html:text property="timeEndStr"/></td>
    </tr>   

    <tr>
        <td>
            <html:submit styleClass="wpsButtonText"><bean:message key="application.submit"/></html:submit>
        </td>
        <td>
            <input type="button" onclick="cancelOperation()" class="wpsButtonText" value="<bean:message key="application.cancel"/>" />
            <html:link styleClass="cancelLink" page="/RuleList.do"></html:link>
        </td>
    </tr>
</table
</html:form>

我的 RuleList.jsp:

<html:form action="/CritRuleList.do" >
<table style="width: 100%;">
    <tr>
        <td><bean:message key="rule.levelId"/></td>
        <td><html:select property=levelId">
                <html:optionsCollection  property="levels" value="clid" label="name" />
            </html:select>
        </td>
    </tr>
    <tr>    
        <td><bean:message key="rule.timeStart"/></td>
        <td><html:text property="timeStartStr" /></td>
    </tr>
    <tr>    
        <td><bean:message key="rule.timeEnd"/></td>
        <td><html:text property="timeEndStr" /></td>
    </tr>
    <tr>    
        <td colspan="2">
            <html:submit styleClass="wpsButtonText"><bean:message key="application.search"/></html:submit>
            <input type="button" onclick="cancelOperation(this)" class="wpsButtonText" value="<bean:message key="critrule.searchClear"/>" />
            <html:link styleClass="cancelLink" page="/RuleResetSearchFilter.do"></html:link>
        </td>   
    </tr>
</table>    
</html:form>

【问题讨论】:

  • 能不能把jsp的相关部分也展示一下,也就是表格?

标签: struts struts-1


【解决方案1】:

在您描述的情况下会调用子类方法,这听起来很奇怪。要找出它发生的原因,您需要调试代码 - 在 RuleSearchForm.getLevels() 方法的 if 构造中放置一个断点,看看它是否真的被调用(以及调用来自哪里)。

除此之外,您可以尝试将关卡的填充逻辑完全从表单中移开,而是在动作中执行。因此,在 RuleCreate 操作中,您将执行以下操作:

List levels = DAOClass.getLevels();
Collections.reverse(levels);
request.setAttribute("levels", levels);

【讨论】:

    【解决方案2】:

    这段代码真的很难理解,而且您仍然遗漏或更改了一些映射(RuleCreate 或 CreateRule)。我的猜测是您误解了使用哪种表单声明来填充字段和选项集合。当您创建这样的表单时

    <html:form action="/RuleSave.do">
    

    选择框的值取自与您的 struts-config 中的 RuleSave 操作关联的表单 bean,而不是来自转发到此 jsp 的操作的表单。

    来自http://struts.apache.org/1.2.x/userGuide/struts-html.html#form

    呈现一个 HTML 元素 ... 表单 bean 的位置,并在必要时创建,基于关联 ActionMapping 的表单 bean 规范。

    【讨论】:

      【解决方案3】:

      [问题已解决。 1. 正如 Tommi 所说,我检查了 DAO 代码。 DAO 返回保存在静态私有字段中的 bean 列表。

      /**
           * Add empty value. User should have an opportunity not to set value for this field.
           * */
          public Collection getLevels(){
              if(levels == null || levels.size() == 0){
                  super.getLevels();
                  levels.add(0, new Level());
              }
              return levels;
          }
      

      因此,每次添加空 bean 时,我都会更改该静态对象列表。可怕的错误。

      1. 参数传输。我已经为 WebSphere Portal 创建了 struts1 应用程序。门户没有重定向,因为我使用的是 portlet,而不是页面。这就是为什么请求参数存在于请求未点亮的最后一个jsp页面呈现的原因。也解决了。

      【讨论】:

      • 您应该接受这个答案以表明问题已经解决。
      猜你喜欢
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      相关资源
      最近更新 更多