【问题标题】:Struts 1.3 iterate through dynamic list of questionsStruts 1.3 遍历动态问题列表
【发布时间】:2014-02-19 17:36:33
【问题描述】:

任何帮助将不胜感激:

在我的 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

【问题讨论】:

    标签: java jsp struts javabeans


    【解决方案1】:

    您希望您的 questions 属性如何从您的 jsp/view 呈现为 List&lt;String&gt; questions?你试过调试你的validateQuestionsForm吗?如果是这样,请检查您的问题属性。您需要做的就是将您的列表属性更改为String array。就像在您的 MultipleQuestionsForm 中一样,

    private String[] questions;
    

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

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 2016-10-01
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多