Create JSP Page with a Struts Select Box Step 28
The next step would be to create a page that will allow you to save a book to the database.
Add this code to your addbook.jsp page.
<%@ include file="include-taglibs.jspf" %>
<html:form action="/UserAction.do?action=saveBook" enctype="multipart/form-data" >
<br></br>
<table>
<tr><td>
<B>Add a Book</B>
</td></tr><tr><td>
Name of Book<html:text name="userForm" property="book"/>
</td></tr>
<tr><td>
Book Author<html:text name="userForm" property="author"/>
</td></tr>
<tr><td>
Name of Publisher<html:text name="userForm" property="bookPublisher"/>
</td></tr>
<tr><td>
Category<html:select name="userForm" property="categoryId">
<html:optionsCollection name="userForm" property="bookCategoryList" value="id" label="category" />
</html:select>
</td></tr>
<tr><td>
<input type="submit" value="Submit"/></td></tr>
</table>
</html:form>
This code has a text box for your book, bookAuthor, and bookPublisher. Those text boxes are attached to your form with the property attribute.
You also have a select box. We need to assign a book category to your book. The category is contained in another table and we need to establish the relationship between your book and the category it belongs too. You can attach an entire collection to the select box. The collection here is the category list that you already set in your form. You have a property for the list that will take the id from the category that is selected and put it into your form under the categoryId property. The label is the name of the category that the user will see since we don't want to display to them the categoryId. In older versions of struts, you would use the html:options tag to add a collection to the select box.
You now will need to add these methods to your UserForm.java class so your jsp page can set the property values.
private String book="";
private String author="";
private String bookPublisher="";
private String categoryId="";
public String getBook()
{
return this.book;
}
public void setBook(String book)
{
this.book=book;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public String getBookPublisher()
{
return this.bookPublisher;
}
public void setBookPublisher(String bookPublisher)
{
this.bookPublisher=bookPublisher;
}
public String getCategoryId()
{
return this.categoryId;
}
public void setCategoryId(String categoryId)
{
this.categoryId=categoryId;
}
When the user submits the form, they call the saveBook method. Therefore, you will need to add this method to your UserAction class.
public ActionForward saveBook(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm=(UserForm)form;
UserBookService service = ServiceFactory.getInstance().getUserBookService();
Book book=new Book();
book.setBookAuthor(userForm.getAuthor());
book.setBookName(userForm.getBook());
book.setBookPublisher(userForm.getBookPublisher());
Bookcategory cat=service.getOneBookCategory(new Long(userForm.getCategoryId()));
List
bookList.add(book);
service.save(cat);
book.setBookcategory(cat);
service.save(book);
userForm.getBookList().add(book);
return mapping.findForward("addbook");
}
This method sets the property values into a Book class and then saves the Book to the database. It saves the category first after setting the new book to prevent a org.hibernate.TransientObjectException. Since Book requires a Bookcategory.java object, we pull the category from the database and set that object in the Book.
You will need to add this method to your UserBookService.java class.
public Bookcategory getOneBookCategory(Long id);
You will need to add this method to your UserBookServiceImpl.java class.
public Bookcategory getOneBookCategory(Long id)
{
return dao.getOneBookCategory(id);
}
You will also need to add this method to your UserBookDAO.java class. This method will retrieve the Bookcategory object from the database based on the Id.
public Bookcategory getOneBookCategory(Long id)
{
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Bookcategory category=null;
try {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
category=(Bookcategory)sessionFactory.getCurrentSession().get(Bookcategory.class,id);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException re) {
System.out.println("Error In Hibernate getBook"+re.toString());
throw re;
}
return category;
}
Go To Step 29

2 comments:
Hello, I am responding to the Website needed, we can definitely assist you, please contact us at 602-404-4444 or visit us online at http://www.directconnectcommunications.com
Thanks, but I don't recall responding about some website needs.
Post a Comment