Thursday, November 20, 2008

Beginning JSP Page Struts Text Tag Step 26

Following my tutorial, this is a simple page to add a category to the database.


Replace your addcategory.jsp code with this page.
This page contains one text field that is attached to your form. When you submit your form, you will grap this category, create an object and save that object to the database.



<%@ include file="include-taglibs.jspf" %>


<html:form action="/UserAction.do?action=saveCategory" enctype="multipart/form-data" >
<br></br>
<table>
<tr><td>
<B>Add a Category</B>
</td></tr><tr><td>
Name of Book Category <html:text name="userForm" property="category"/>
</td></tr><tr><td>
<input type="submit" value="Submit"/></td></tr>
</table>
</html:form>



You will need to add this method to your UserAction class.
This method will retrieve the submitted information from the form and save it to the database.

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);
return mapping.findForward("addcategory");
}

You will need to add this information to your form. When the jsp form is submitted, the category value will be set using these methods.

private String category="";
public void setCategory(String category)
{
this.category=category;
}
public String getCategory()
{
return this.category;
}

If your page doesn't show the text boxes or the list of categories, it might be because your addcategory.jsp page isn't including include-taglibs.jspf page that contains the tag libraries.

Go To Step 27

0 comments: