簡體   English   中英

從jsp到action類獲取List對象的值

[英]Get the List object value from a jsp to action class

JSP中的迭代列表對象,其值來自顯示正確的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到操作類的多個/單個更新的表單字段值 ,以便執行更新的操作。 但是list(beanList)值在操作類中為空。 由於它無效,因此我無法執行更新操作。 1)在新的動作類(EditAction.java)中如何初始化列表對象( beanList )? 這與我在ViewAction.java中聲明的方式相同。2)Jsp sysntax是否正確? 要求您提供幫助。 提前致謝。

向您的Bean類添加一個默認的無參數構造函數

默認的no-args構造函數之所以這樣稱呼是因為它是默認的:如果您不指定任何構造函數,則會自動創建它。

相反,如果你指定另一個構造函數,例如一個與像您的參數,無參數的構造不會自動創建了,你必須 ,如果你需要它明確聲明它。

Struts2需要no-args構造函數來創建您的bean。

例如,您可能有一個帶有10個參數的構造函數的Bean,並在JSP頁面中僅指定了其中一個:Struts必須能夠創建對象並設置單個字段(通過Setter),而不必擔心缺少9個參數參數。

您必須使用類型轉換,在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM