Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

java - Get the List object value from a jsp to action class

Iterating List object in a JSP whose value coming from ViewAction class which is displaying proper.

Below is the jps code.

<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 and Bean class code is as follows

In the action class list object name is 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;
    }   
}

Bean:

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

I need the multiple/single updated form field value from jsp to action class in order to do the updated operation. But the list(beanList) value is nullified in the action class. Since it is nullified i can not do the update operation. 1)In the new action class(EditAction.java) how to initialise the list object(beanList) ? It is the same way as i declare in ViewAction.java 2)Is the Jsp sysntax is proper ? Request you to help on this. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add a default no-args Constructor to your Bean Class.

The default no-args Constructor is called like that because it is the default: if you don't specify any Constructor, it is created automatically.

If instead you specify another Constructor, for example one with parameters like your, the no-args Constructor is not automatically created anymore, and you have to declare it explicitly if you need it.

Struts2 needs the no-args Constructor to create your beans.

For example, you could have a bean with a Constructor taking 10 parameters, and specify only one of them in the JSP page: Struts must be able to create the object and set the single field (through the Setter) without caring about the nine missing parameters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...