代码之家  ›  专栏  ›  技术社区  ›  ltalhouarne

Struts1.3遍历动态问题列表

  •  2
  • ltalhouarne  · 技术社区  · 11 年前

    如有任何帮助,将不胜感激:

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

    问题和答案字段显示良好。

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

    这是我的表格: 多个问题表单

    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 回复  |  直到 9 年前
        1
  •  2
  •   Vinoth Krishnan    11 年前

    您希望问题属性如何呈现为 List<String> questions 从您的jsp/视图?你试过调试你的 validateQuestionsForm ? 如果是,请检查您的问题属性。您只需将列表属性更改为 String array 。如您的MultipleQuestionsForm中所示,

    private String[] questions;
    

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