سؤال

Any help would be greatly appreciated:

In my JSP I have a dynamic list of questions with an input field for each question as such:

<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>

The questions and answer fields are being displayed fine.

My only problem, is trying to access the value of the all the input fields in my action class.

Here is my form: 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)
}

Here is part of my ActionClass:

getQuestions() returns 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
هل كانت مفيدة؟

المحلول

How do you expect your questions property should render as List<String> questions from your jsp/view? Have you tried debug your validateQuestionsForm? if so please check your questions property. All you need to do is change your list property into String array. Like this in your MultipleQuestionsForm,

private String[] questions;

And getter setter for this property. Now you can receive as string array and iterate it. Hope this helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top