问题
In my Action class, I have a List of Questions
. that I would want to be populated from the view.
public class MyQuizTest extends ActionSupport {
public String addItemsToTemplate(){
List<Question> q= myQuestions;
System.out.println(q);
return "success";
}
public List<Question> getMyQuestions() {
return myQuestions;
}
public void setMyQuestions(List<Question> myQuestions) {
this.myQuestions = myQuestions;
}
private List<Question> myQuestions;
}
This is the question class
public class Question{
public boolean isChosen(){
retrun this.chosen
}
public void setChosen(boolean chosen){
this.chosen = chosen;
}
private boolean chosen;
}
And Here is the form that that handles that
<form method = "GET" action = "addItemsToTemplate">
<s:iterator value = "myQuestions" status="key" var = "questionItem">
<s:checkbox name = "myQuestions[%{#key.index}].chosen" label="Check Me for testing"/>
</s:iterator>
</form>
this method handles the form
public String addItemsToTemplate(){
List<Question> q= myQuestions;
System.out.println(q);
return "success";
}
Upon submission, the myQuestions
returns a null
. why is that?
I want to identify if the corresponding question has been chosen.
回答1:
where are you populating the myQuestions on the Action Layer.
If you are not populating it anywhere then when the myQuestions variable is encountered on the JSP it is empty/null due to which i think the flow will not go inside your iterator loop.
<s:iterator value = "myQuestions" status="key" var = "questionItem">
<s:checkbox name = "myQuestions[%{#key.index}].chosen" label="Check Me for testing"/>
</s:iterator>
Now if you submit the form, surely the myQuestions object is not mapped with anything from the JSP and thus is coming as null
来源:https://stackoverflow.com/questions/15650002/populate-user-defined-objects-from-a-list-from-the-jsp