【问题标题】:Get the List object value from a jsp to action class从jsp到action类中获取List对象值
【发布时间】:2013-11-02 08:05:26
【问题描述】:

在 JSP 中迭代 List 对象,其值来自正确显示的 ViewAction 类。

下面是jps代码。

<s:iterator value="beanList" status="stat">
    <tr> 
         <td>    
             <input type="checkbox" name="subCheckBox" />
         </td>   
         <td>                 
             <s:textfield name="beanList[%{#stat.index}].rollnumber" 
                          value="%{rollnumber}" theme="simple"/>
         </td>
         <td>
             <s:textfield name="beanList[%{#stat.index}].name" 
                          value="%{name}" theme="simple"/>
         </td>
         <td>
             <s:textfield name="beanList[%{#stat.index}].location" 
                          value="%{location}" theme="simple"/>
         </td> 
    </tr>     
</s:iterator>

ViewAction.java和Bean类代码如下

动作类列表中的对象名是beanList

public class ViewCheckboxAction extends ActionSupport  {
    HttpServletRequest request = ServletActionContext.getRequest();
    String viewData = "select * from student order by rollno";
    List<Bean> beanList;

    public List<Bean> getBeanList() {
        return beanList;
    }  

    public void setBeanList(ArrayList<Bean> beanList) {
        this.beanList = beanList;
    }

    public String execute() {
        beanList = new ArrayList<Bean>();
        DbConnection db = new DbConnection();
        int counter = 0;
        try {
            Statement st = db.getConnection().createStatement();
            ResultSet res = st.executeQuery(viewData);
            while(res.next()) {
                  counter++;
                  Bean bean = new Bean(res.getInt(1),
                                       res.getString(2),
                                       res.getString(3));
                  rollNumber.add(res.getString("rollno"));
                  beanList.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { 
                db.removeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(counter>0)
           return SUCCESS;
        else 
           return ERROR;
    }   
}

豆子:

public class Bean {
    int rollnumber;
    String name;
    String location;

    public Bean(int x, String y, String z) {
        rollnumber = x;
        name = y;
        location = z;
    }

    getters and setters...

我需要从 jsp 到 action 的多个/单个更新的表单字段值 class 以执行更新的操作。但是列表(beanList) 值在动作类中无效。由于它已失效,我无法进行更新 手术。 1)在新的动作类(EditAction.java)中如何初始化列表对象(beanList)? 这与我在 ViewAction.java 中声明的方式相同 2)Jsp sysntax 是否正确? 请求您对此提供帮助。提前致谢。

【问题讨论】:

    标签: java jsp struts2


    【解决方案1】:

    您必须使用类型转换,在 ViewCheckboxAction-conversion.properties 文件中提供以下配置:

    KeyProperty_beanList=rollnumber
    Element_beanList=Bean
    CreateIfNull_beanList=true 
    

    当通过表单提交时,rollnumber 用作 beanList 中 Bean 实例的 KeyProperty。您可以将任何其他属性用于 Key Property 字段。 name 的值将设置为具有此特殊 id 的 MyBean 实例。 该列表没有为不可用的 id 值添加空值。这种方法避免了 OutOfMemoryErrors 的风险!

    <s:iterator value="beanList" id="bean">
        <tr> 
            <td>    
                 <input type="checkbox" name="subCheckBox" />
            </td>   
            <td>                 
                 <s:textfield name="beanList(%{bean.rollnumber}).rollnumber" value="%{rollnumber}" theme="simple"/>
            </td>
            <td>
                <s:textfield name="beanList(%{bean.rollnumber}).name" value="%{name}" theme="simple"/>
             </td>
             <td>
                 <s:textfield name="beanList(%{bean.rollnumber}).location" value="%{location}" theme="simple"/>
              </td> 
       </tr>     
    </s:iterator>
    

    参考:http://struts.apache.org/release/2.0.x/docs/type-conversion.html

    【讨论】:

      【解决方案2】:

      向您的Bean 类添加一个默认无参数构造函数

      default no-args Constructor 是这样调用的,因为它是默认的:如果你不指定 any Constructor,它是自动创建的。

      如果您改为指定另一个构造函数,例如带有您的参数的构造函数,则不再自动创建无参数构造函数,您必须显式声明它如果你需要的话。

      Struts2 需要无参数构造函数来创建您的 bean。

      例如,您可以有一个带有 Constructor 的 bean,它带有 10 个参数,并在 JSP 页面中只指定其中一个:Struts 必须能够创建对象并设置单个字段(通过 Setter)而不用关心九个缺失的参数。

      【讨论】:

      • 非常感谢它工作正常 :) 为什么 Bean 类中需要无参数构造函数?请帮助我。
      • 如果有效,您应该投票并接受答案。顺便说一句,我现在将进行编辑,让您了解发生了什么
      • 嘿,不错的收获。甚至没见过 OP 中的 Bean 类。
      • 谢谢@AleksandrM,TBH 没什么特别的。我仍然支持你所有的好消息;)
      猜你喜欢
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      相关资源
      最近更新 更多