Friday, November 21, 2008

Display List in JSP using Iterate Tag Step 27

You will need to display the list of categories that you added to the database. You will want to use the iterate tag to accomplish this.

In the addcategory.jsp page, you will want to add this code before the closing form tag. This will iterate through the list of categories that you have in the database.

<b>Current Category List</b><br></br><nested:iterate property="bookCategoryList"><nested:write property="category"/></nested:iterate>

When you first come into your application, you will need to load your category list. Replace your addCategory method with this code in your action class:

public ActionForward addCategory(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserBookService service = ServiceFactory.getInstance().getUserBookService();
List categoryList=service.getBookCategory();
UserForm userForm=(UserForm)form;
userForm.setBookCategoryList(categoryList);
return mapping.findForward("addcategory");
}

When you save a new category, you will need to add it to your list. Replace your saveCategory method with this code:

public ActionForward saveCategory(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm=(UserForm)form;
UserBookService service = ServiceFactory.getInstance().getUserBookService();
Bookcategory category=new Bookcategory();
String name=userForm.getCategory();
category.setCategory(name);
service.save(category);
List categoryList=userForm.getBookCategoryList();
categoryList.add(category);
return mapping.findForward("addcategory");
}

You will also need to add the methods to your form to hold the data. Add this code to your UserForm.

List categoryList;
public List getBookCategoryList()
{
return this. categoryList;
}
public void setBookCategoryList(List categoryList)
{
this.categoryList=categoryList;
}

When you view this page, you should see a list of your current categories displayed on your page.

Go To Step 28

0 comments: