2011-03-10 42 views
1

我有一個使用Struts1 ActionForm bean的問題。請看看我的struts-config的一部分:Struts1和ActionForm繼承,數據混亂

<!-- 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豆代碼:

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。做並看到規則列表。 ruleSearchForm用作將搜索參數傳輸到/RulesList.do操作的bean。最初這個bean是空的,只有getLevels()返回「空值」+從超類方法得到的層次列表。

  2. 用戶轉到/CreateRule.do,ruleForm用於收集用戶輸入。 級別屬性用於選擇框。我得到那裏的水平列+空行。此空行不添加在RuleForm(名爲ruleForm)中。它添加了RuleForm的子類。爲什麼具有NO靜態字段的超類ActionForm bean以其他名稱從它的子類實例中獲取值???

  3. 如果用戶保存規則並已被重定向到/RuleList.do,他會看到來自「ruleForm」的值填充(即填充)搜索表單(「ruleSearchForm」)。

這是什麼意思?請幫忙,我不明白這個數據混亂ActionForm豆

UPD: 現在,我已經改變了FormAction豆的繼承。我已經介紹了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> 
+0

您還可以顯示jsp的相關部分,即表單嗎? – 2011-03-10 23:36:31

回答

1

這聽起來很奇怪,一個子類方法會被調用你描述的情況。爲了找出它發生的原因,你需要調試你的代碼 - 在RuleSearchForm.getLevels()方法的if結構中放置一個斷點,看看它是否真的被調用(以及調用來自哪裏)。

除此之外,您可以嘗試將級別的填充邏輯完全從表單中移除,而是在動作中進行。因此,在RuleCreate行動,你會做這樣的事情:

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

這種代碼真的難以遵循,你還是離開了或改變了一些映射(RuleCreate或CreateRule)。我的猜測是,你會誤解使用哪種表單聲明來填充字段和選項集合。當你創建一個表單如

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

的選擇框中的值是從與RuleSave動作在你的struts-config相關form bean的拍攝,而不是從轉發到這個JSP動作的形式。

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

渲染HTML元素...的形式豆位於,並在必要時創建的,基於所述相關聯的ActionMapping表單bean規範。

0

[問題已解決。 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頁面被呈現。它也被解決了。
+0

您應該接受此答案以表明問題已解決。 – 2011-04-23 10:02:51

+0

謝謝,我已經完成了。 – Sergey 2011-04-24 13:36:05