Reputation: 75
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.
Upvotes: 2
Views: 8951
Reputation: 50281
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.
Upvotes: 3
Reputation: 941
You have to use Type Conversion, Provide following configuration in ViewCheckboxAction-conversion.properties file :
KeyProperty_beanList=rollnumber
Element_beanList=Bean
CreateIfNull_beanList=true
When submitting this via a form, the rollnumber is used as KeyProperty for the Bean instances in the beanList.You can use any other property for Key Property field. The value for name will be set to the MyBean instance with this special id. The List does not have null values added for unavailable id values. This approach avoids the risk of 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>
Refer : http://struts.apache.org/release/2.0.x/docs/type-conversion.html
Upvotes: 1