Using struts: checkbox list

S

silvia.fama

HI!

I'm starting studying struts and I need some help.

My exercise is to retrive a list of person from a table on a db and to
present it on a jsp: userList.jsp
This list contains also a checkbox, one for evevery element of the
list.
The checked element will be deleted or updated.

The problem is that I'm not able to know which is the checked element.
How can I resolve this problem?
I tried to insert into the action ActUserList the "scoper=request" but
it seems doesn't work with list.


I attach here the code.

******************
struts-config.xml

<form-beans>
<form-bean name="userListForm"
type="form.UserListForm" >
</form-bean>
<form-bean name="userForm"
type="form.UserForm">
</form-bean>
</form-beans>

<action path="/ActUserList" input="/list/userList.jsp"
type="action.ActUserList" name="userListForm" validate="false">
<forward name="update" path="/DspUpdateUser.do"></forward>
<forward name="delete" path="/DspUserList.do"></forward>
<forward name="insert" path="/jsp/userRegistration.jsp"></forward>
</action>

<action path="/DspUserList"
type="display.DisplayUserList"
name="userListForm" >
<forward name="success" path="/list/userList.jsp"></forward>
</action>
******************

*******************
userForm.java

public class UserForm extends ActionForm {

private String firstName;
private String lastName;
private String userName;
private String password;
private String checkPassword;
private String email;
private String phone;
private String fax;
private boolean registred;
private boolean checked;
.....
(methods follows....)

*********************

userListForm.java

public class UserListForm extends ActionForm {


List userList;

....

(methods follows....)

*********************
DisplayUserList.jsp

public class DisplayUserList extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception{


UserListForm users = (UserListForm)form;

//user lists
UserDAO userDAO = new UserDAO();
List userListDAO = userDAO.findAll();


//copying db user list into user form
ArrayList userFormList = new ArrayList();

for(int i=0; i< userListDAO.size(); i++){
UserRegistrationForm userForm = new
UserRegistrationForm((User)userListDAO.get(i));
userForm.setChecked(false);
userFormList.add(userForm);
}


List list = Collections.synchronizedList(userFormList);

users.setUserList(list);

return mapping.findForward("success");

}

}

******************************************************************

ActUserList.java

public class ActUserList extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

UserListForm userList = (UserListForm)form;


List users = userList.getUserList();
UserDAO userDAO = new UserDAO();
String result = null;


if (request.getParameter("submit").toString().equals("delete")) {
result = "delete";
} else if
(request.getParameter("submit").toString().equals("update")) {
result = "update";
} else
result = "insert";

for (int i = 0; i < users.size(); i++) {
if (((UserForm) users.get(i)).isChecked() == true) {
// delete selected records
if (result.equals("delete") == true) {
User user = new User(((UserForm) users.get(i)).getEmail(),
((UserForm) users.get(i)).getFirstName(),
((UserForm) users.get(i)).getLastName(),
((UserForm) users.get(i)).getPassword(),
((UserForm) users.get(i)).getPhone(),
((UserForm) users.get(i)).getFax());
userDAO.delete(user);
} else if (result.equals("update")) {
request.setAttribute("userUpdate", (UserForm) users.get(i));
}

}

}

return mapping.findForward(result);
}
}


*********************

userList.jsp


<head>
<title>User's List</title>
</head>
<html:html>
<body>
<table border="1">

<html:form action="/ActUserList">

<tr>
<th>
<bean:message key="userRegistration.firstName" />
</th>
<th>
<bean:message key="userRegistration.lastName" />
</th>
<th>
<bean:message key="userRegistration.email" />
</th>
<th>
<bean:message key="checked" />
</th>

</tr>
<logic:iterate id="user" name="userListForm" property="userList">
<tr>
<td>
<bean:write name="user" property="firstName" />
</td>
<td>
<bean:write name="user" property="lastName" />
</td>
<td>
<bean:write name="user" property="email" />
</td>
<td>
<html:checkbox name="user" property="checked" />
</td>
</tr>
</logic:iterate>

<tr>
<td>
<input type="submit" name="update" value="update">
</td>
<td>
<input type="submit" name="delete" value="delete">
</td>
<td>
<input type="submit" name="insert" value="insert">
</td>
</tr>
</html:form>
</table>
</body>
</html:html>


Kind regards,
Silvia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top