Struts 1.3遍历动态的问题列表

时间:2014-02-19 17:36:34

标签: java jsp struts javabeans

非常感谢任何帮助:

在我的JSP中,我有一个动态的问题列表,其中包含每个问题的输入字段:

<logic:iterate name="listOfQuestions" id="listOfQuestionsId" indexId="indexId">
        <tr>
            <td align="right" width="100%"><bean:message key='<%= "prompt.question" + (indexId.intValue() +1)%>'/>:&nbsp;&nbsp;</td><td width="100%" nowrap="nowrap"><bean:write name="listOfQuestionsId"/></td>
        </tr>   

        <tr align="center">
            <td align="right" width="50%"><bean:message key="prompt.answer"/>:&nbsp;&nbsp;</td>
            <td align="left" width="50%"><html:password property="questions" size="30" maxlength="40" indexed="true"></html:password></td>  
        </tr>       
</logic:iterate>

问题和答案字段显示正常。

我唯一的问题是尝试访问动作类中所有输入字段的值。

以下是我的表单: MultipleQuestionsForm

public class MultipleQuestionsForm extends ActionForm {

    private List<String> questions=null;

            /**
     * @return the questions
     */
    public List<String> getQuestions() {
        return questions;
    }


    /**
     * @param questions the questions to set
     */
    public void setQuestions(List<String> questions) {
        this.questions = questions;
    }

      //omitted the rest (Validate, constructor, reset method)
}

以下是我ActionClass的一部分:

getQuestions()返回null

//Use the ValidateInfoForm to get the request parameters
MultipleQuestionsForm validateQuestionsForm = (MultipleQuestionsForm) form;
List<String> listOfquestions = validateQuestionsForm.getQuestions();

for(String s: listOfquestions) System.out.println(s); //nullPointer since getQuestions() doesn't return the input values

1 个答案:

答案 0 :(得分:2)

您如何期望您的问题属性应从jsp / view呈现为List<String> questions?您是否尝试过调试validateQuestionsForm?如果是这样,请检查您的问题属性。您需要做的就是将列表属性更改为String array。在你的MultipleQuestionsForm中就像这样,

private String[] questions;

此属性的getter setter。现在您可以接收字符串数组并迭代它。希望这会有所帮助。