从JSP的列表中填充用户定义的对象 [英] Populate user defined objects from a list from the JSP

查看:54
本文介绍了从JSP的列表中填充用户定义的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Action类中,我有一个Questions列表.我想从视图中填充.

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

这是问题类别

public class Question{

  public boolean isChosen(){
   retrun this.chosen
 }

  public void setChosen(boolean chosen){
    this.chosen  = chosen;
  }

  private boolean chosen;
}

这是处理该问题的表格

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

此方法处理表单

public String addItemsToTemplate(){
            List<Question>  q=  myQuestions;
            System.out.println(q);

            return "success";
        }

提交后,myQuestions返回null.这是为什么? 我想确定是否已选择了相应的问题.

Upon submission, the myQuestions returns a null. why is that? I want to identify if the corresponding question has been chosen.

推荐答案

您将在哪里填充操作层上的myQuestions.

where are you populating the myQuestions on the Action Layer.

如果您没有在任何地方填充它,那么当在JSP上遇到myQuestions变量时,该变量为空/空,因此我认为该流不会进入您的迭代器循环内.

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>

现在,如果您提交表单,则肯定不会将myQuestions对象与JSP中的任何内容进行映射,因此会变为null

Now if you submit the form, surely the myQuestions object is not mapped with anything from the JSP and thus is coming as null

这篇关于从JSP的列表中填充用户定义的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆