How to create a multiple selections list in Struts? and retrive seleted values ?
Hi friends,
With the help of this example, you can display multiple selections list and how to access selected values in struts. bookList list contains list of Book objects.
Java bean class.
public class Book{ int bookId; String bookName; public int getBookId() { return medId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this. bookName = bookName; }
}
In the Form Class :
public class BookForm { private List bookList; private String[] book; public void setBookList(List medList){ this.bookList = bookList; } public List getBookList(){ return this.bookList; } public String[] getBook() { return book; } public void setBook(String[] book) { this.book = book; }
}
In the Action class :
List medList = DAO.getBooks(); form.setBookList(bookList);
DAO Class :
DAO Class to retrieve Books in data base.
public static List getBooks(){ PreparedStatement pStmt = null; Connection con = null; boolean success = false; ResultSet rs = null; List bookList = new ArrayList(); try{ conn = getConnection(); String sql = " select * from BOOK "; pStmt = conn.prepareStatement(sql); rs = pStmt.executeQuery(); while(rs.next()){ Book book= new Book(); book.setBookId(rs.getInt("Book_ID")); book.setBookName(rs.getString("Book_NAME")); bookList.add(book); } }catch(Exception e){ e.printStackTrace(); }finally{ closeConnectionProp(conn,pStmt,rs); } return bookList; }
JSP Struts page:
<html:select name="BookForm" property="book" multiple="true"> <bean:define name="BookForm" property="BookList" id="bklist" /> <html:options collection="bklist" property="bookId" labelProperty="bookName" /> </html:select>
In the Action class again :
how to retrieve the selected values.
String[] book= form.getBook();
Thanks.
Ads