FFL2:FFL2 Main/Web Framework/FantacyEntityServlet
What is it?
A special servlet, uk.org.fantacy.servlet.FantacyEntityServlet, has been written to make loading and displaying an object from the database trivial.
EntityServlet magically loads data from the database and based on the URL and sends it to a jsp with a predictable name. In many cases this, coupled with the magic that Hibernate does, is all that's needed to put together a page and no Java need be written.
Example use of EntityServlet - RealDivision
Add the Servlet Mapping
We map app.RealDivision onto the entity servlet in web.xml as follows
<servlet-mapping> <servlet-name>EntityServlet</servlet-name> <url-pattern>/app.RealDivision</url-pattern> </servlet-mapping>
What the servlet will do - no code required
This means that any URLs of the form app.RealDivision will be sent to FantacyEntityServlet. What FantacyEntityServlet will then do is look at the URL it is being invoked with. In this case it's app.RealDivision, so it knows we want to load a uk.org.fantacy.model.RealDivision object up. It asks some themightystags.com Hibernate integraton code to load up a RealDivision for us using the given URL parameters. Providing all the columns in the primary key are given as URL parameters the entity should load up fine.
Having loaded our RealDivision, the servlet will then add it as a request attribute with the name realDivision (just change the first char to lower case).
Next the servlet dispatches our request to a JSP with the name realDivision.jsp in the "entity" directory.
Writing the JSP
That's our servlet dealt with so now it's on to the JSP. We use the lovely expression language (EL) to access our object as so
<h3><span>${realDivision.name}</span></h3>
In EL using a dot ('.') just calls the appropriate "get" method (getName in this case).
Because we are using Hibernate we can also access all the linked objects and it will magically load them from the database as and when required so we do the following to list all the teams in this division.
<c:forEach var="realClub" items="${realDivision.realClubs}"> <br> <ffl:realClub realClub="${realClub}"/> </c:forEach>
Here we've also used the JSTL "forEach" tag to iterate over all the objects in the collection of RealClubs. The clubs appear in alphabetical order, that's because the RealDivision.hbm.xml hibernate mapping has been tweaked to specify and order by when loading the RealClubs in a RealDivision, otherwise they'd just be in any old order.