Search code examples
javastruts-1

Retrieving multi select list values to insert into database in struts


I am developing an Online Book Library application in struts...

I have a user form wherein user will enter his details like first name,last name etc and also there would be a books list where he will select some books that he wants..an d later on I want to insert those details into 2 tables..i.e user details in obl_users and and books that user select in users_books.

I used the below code for the list ..

<% 
if(request.getAttribute("booksNameList") != null) { 
%>
    <html:select property="displayBooks" multiple="true" size="5">
    <logic:iterate id="booksNameList" name="booksNameList" scope="request"> 
        <html:option value="${booksNameList.bookId}" ><bean:write name="booksNameList" property="longTitle" /></html:option>

    </logic:iterate>
    </html:select>

<%
}
%>

Initially when user request form then the form will have pre populated list will all the books name in database..I am not sure about the code that i wrote to get book id in the value part of i.e value="${booksNameList.bookId}" ..

In my addUser() I want to iterate through the user selected books like this..

for (int i = 0; i < selectedBooks.length; i++) {

                //insertBooks.setInt(1, generatedKeys.getInt(1));
                //insertBooks.setInt(2, Integer.parseInt(selected[i]));

                //insertBooks.addBatch();
            }

but for that how to get the user selected books..

Here is my user.java

public class User extends ActionForm {

    private int userId;
    private String firstName;
    private String lastName;
    private String middleName;
    private String username;
    private String password;
    private String contactNumber;
    private String membershipNumber;
    private String role;
    private String email;
    private String address;
    private String comments;
    private String dateOfBirth;
    private int oblStatus;
    private String createdDate;
    private String updatedDate;
    private String createdBy;
    private String updatedBy;
    private String displayBooks;

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getMiddleName() {
        return middleName;
    }
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getContactNumber() {
        return contactNumber;
    }
    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }
    public String getMembershipNumber() {
        return membershipNumber;
    }
    public void setMembershipNumber(String membershipNumber) {
        this.membershipNumber = membershipNumber;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getComments() {
        return comments;
    }
    public void setComments(String comments) {
        this.comments = comments;
    }
    public String getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
    public int getOblStatus() {
        return oblStatus;
    }
    public void setOblStatus(int oblStatus) {
        this.oblStatus = oblStatus;
    }
    public String getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }
    public String getUpdatedDate() {
        return updatedDate;
    }
    public void setUpdatedDate(String updatedDate) {
        this.updatedDate = updatedDate;
    }
    public String getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
    public String getUpdatedBy() {
        return updatedBy;
    }
    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }
    public String getDisplayBooks() {
        return displayBooks;
    }
    public void setDisplayBooks(String displayBooks) {
        this.displayBooks = displayBooks;
    }



}

book bean class:

private String bookId;
    private String longTitle;
    private String shortTitle;
    private String isbn;
    private String dateOfPublication;
    private String noOfPages;
    private String boundType;
    private String dvdAvailability;
    private String noOfAvailableCopies;
    private int oblStatus;
    private String createdDate;
    private String updatedDate;
    private String createdBy;
    private String updatedBy;
    private String displayAuthors;
    private int[] authorIds;

please guide me...i am totally new to struts


Solution

  • You have to put "displayBooks" property as a String array like;

    private String[] displayBooks;
    

    Since you want to select multiple values on the form, Array or ArrayList can be the only better option to use.