Populate user defined objects from a list from the JSP

坚强是说给别人听的谎言 提交于 2019-12-25 05:04:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!