Struts1ActionFormBeanの使用に問題があります。私の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>
そして私のActionformBeanコードの一部:
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;
}
}
問題は:
ユーザーは/RuleList.doに移動し、ルールのリストを表示します。検索パラメータを/RulesList.doアクションに転送するためのBeanとして使用されるruleSearchForm 。最初、このBeanは空であり、getLevels()のみが「空の値」+スーパークラスメソッドから取得したレベルのリストを返します。
ユーザーは/CreateRule.doに移動し、ruleFormはユーザー入力の収集に使用されます。レベルプロパティはselectboxで使用されます。そして、レベルのリスト+空の行が表示されます。この空の行は、RuleForm(ruleFormという名前)には追加されません。RuleFormのサブクラスが追加されました。静的フィールドがなく、他の名前のスーパークラスActionForm Beanが、そのサブラスインスタンスから値を取得するのはなぜですか?
ユーザーがRuleを保存し、/ RuleList.doにリダイレクトされた場合、ユーザーには「ruleForm」の値が入力された(つまり入力された)検索フォーム(「ruleSearchForm」)が表示されます。
どういう意味ですか?助けてください、私はActionFormBeans間のこのデータの混乱を理解していません
UPD:FormActionBeanの継承を変更しました。BaseFormBeanを紹介しました。このBaseFormBeanには、RuleFormとRuleSearchFormの2つの子があります。それは役に立たなかった。それでも、あるBeanの属性は別の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>