But sometimes it makes sense to go directly from an HTML page to a JSP page, using a JavaBean as a way to keep consistency in the data.
A few constructs help us in managing this.
First of all, we can specify a JSP as action in a form, something like that:
<h2>Beer data</h2> <form action="TestBean.jsp"> Brand: <input type="text" name="brand"><br /> Size: <input type="text" name="size"><br /> <input type="submit"> </form>Pushing the submit button we go to the TestBean.jsp page, passing two parameters, named brand and size.
This is cool, but it is nothing compared of what we are doing in the receiving JSP page:
<jsp:useBean id="beer" class="ch08.BeerInfo"> <jsp:setProperty name="beer" property="*" /> </jsp:useBean>We creates a JavaBean, beer, referring to the class ch08.BeerInfo, initializing it using all (that is the meaning of the star) the properties that are passed as parameters to the JSP page. Naturally, to make it works, we should ensure the names of the parameters match with the names of the JavaBean properties.
If, for any reason, we can't give to the parameters the same names of the properties in the JavaBean, we can still make it, using the param attribute in the jsp:setProperty standard action. Something like this:
<jsp:setProperty name="beer" property="brand" param="someOtherName" />Once we have create the JavaBean, we can use it in the usual way:
<jsp:getProperty name="beer" property="brand" />, <jsp:getProperty name="beer" property="size" />More about this in the eighth chapter of Head First Servlet and JSP.
No comments:
Post a Comment