检索多选列表值以插入到struts中的数据库中

时间:2013-09-27 07:30:32

标签: java struts-1

我正在struts中开发一个在线图书馆应用程序......

我有一个用户表单,其中用户将输入他的详细信息,如名字,姓氏等,还有一个书籍列表,他将选择一些他想要的书籍。稍后我想插入这些细节分为2个表..用户详细信息在obl_users和用户在users_books中选择的书籍。

我使用下面的代码列表..

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

<%
}
%>

最初当用户请求表单时,表单将有预先填充的列表,所有书籍都将在数据库中命名。我不确定我为了value="${booksNameList.bookId}"的值部分而编写的代码来获取书籍ID ..

在我的addUser()中,我想遍历用户选择的书籍,如下所示..

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

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

                //insertBooks.addBatch();
            }

但为此如何让用户选择书籍..

这是我的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;
    }



}

书豆类:

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;

请指导我......我对struts来说是全新的

2 个答案:

答案 0 :(得分:3)

您必须将“displayBooks”属性设置为String数组;

private String[] displayBooks;

由于您希望在表单上选择多个值,因此可以使用Array或ArrayList作为唯一更好的选项。

答案 1 :(得分:0)

Here 你可以找到一个如何做的例子或获得一个项目列表