This example demonstrates how to connect JSP pages to resources. The example is copied from the Bookstore example presented by the Stapler project that provides a way to staple URIs to Java classes to build RESTful Web applications such as Hudson.
A bookstore Web application is presented that is capable of presenting books, CDs and tracks of CDs.
The example consists of four web resources implemented by the following:
com.sun.jersey.samples.bookstore.resources.Bookstore
com.sun.jersey.samples.bookstore.resources.Book
com.sun.jersey.samples.bookstore.resources.CD
com.sun.jersey.samples.bookstore.resources.Track
The mapping of the URI path space is presented in the following table:
URI path | Resource class | HTTP methods |
---|---|---|
/ | Bookstore | GET |
/count | Bookstore | GET |
/time | Bookstore | GET |
/items/{itemid} | Book, CD | GET |
/items/{itemid}/tracks/{num} | Track | GET |
The mapping of the views to URI paths and JSP pages is presented in the following table:
View | URI path | JSP page |
---|---|---|
Bookstore:index.jsp | / | /com/sun/jersey/samples/bookstore/resources/Bookstore/index.jsp |
Bookstore:count.jsp | /count | /com/sun/jersey/samples/bookstore/resources/Bookstore/count.jsp |
Bookstore:time.jsp | /time | /com/sun/jersey/samples/bookstore/resources/Bookstore/time.jsp |
Book:index.jsp | /items/{itemid} | /com/sun/jersey/samples/bookstore/resources/Book/index.jsp |
CD:index.jsp | /items/{itemid} | /com/sun/jersey/samples/bookstore/resources/CD/index.jsp |
Track:index.jsp | /items/{itemid}/tracks/{num} | /com/sun/jersey/samples/bookstore/resources/Track/index.jsp |
Bookmark example runs on Glassfish V3 application server (http://glassfish.dev.java.net), which can be run as an embedded container.
Run the example as follows:
Build and deploy the project by executing maven 2 from the project directory
mvn glassfish:run
Goto the URL:
http://localhost:8080/Bookstore/
This example shows how to support polymorphism of resources and JSP pages. Hence it is possible to add another resource, such as a DVD resource with associated JSP pages, which extends Item without having to change the logic of Bookstore or the existing JSP pages.
Each relative JSP page referenced using the Views annotation declared on a resource class corresponds to:
A relative JSP page is resolved by converting the fully qualified class name of the resource class into a path and appending the name of JSP page to that path. For example, when a GET is performed on the URI path "/" then the relative JSP page is "index.jsp" as referenced by the Bookstore resource class. This is resolved to "/com/sun/jersey/samples/bookstore/resources/Bookstore/index.jsp" that resides in the Web pages of the WAR. Similarly when a GET is performed on URI path "/count" then the relative JSP page is "count.jsp" as referenced by Bookstore resource class.
The runtime fowards to the absolute JSP page using the servlet RequestDispatcher.forward method. The variable "it" is automatically set to the instance of Bookstore so that the index.jsp, or count.jsp, has access to the Bookstore instance as a Java bean.
If a resource class inherits from another resource class then it will automatically inherit the views from the super class.
A JSP page may also include views using the inheritance mechanism, for example the index,jsp page associated with the Book resource class includes a footer.jsp page whoose location is specified by the super class, Item.