Using struts: checkbox list

Discussion in 'Java' started by silvia.fama@googlemail.com, Jul 7, 2008.

  1. Guest

    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
    , Jul 7, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    0
    Views:
    512
    tshad
    Apr 15, 2005
  2. Vikram

    disable checkbox list checkbox

    Vikram, Jan 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,385
    ThunderMusic
    Jan 25, 2006
  3. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    809
    David Wier
    Oct 6, 2006
  4. Jason Huang
    Replies:
    0
    Views:
    543
    Jason Huang
    Jun 29, 2007
  5. Joey Pang
    Replies:
    4
    Views:
    509
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page