PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-website/src/documentation/jog.page

http://github.com/scalate/scalate
Visualforce Page | 87 lines | 61 code | 26 blank | 0 comment | 0 complexity | 0456fc792a6ab03f365bc4a6f9d44594 MD5 | raw file
 1---
 2title: Jog
 3in_menu: false
 4sort_info: 2
 5--- name:overview
 6# Jog
 7
 8Using Scalate with JAXRS (Jersey) on Guice
 9
10--- name:content pipeline:jade
11
12.left
13  :markdown
14    # Overview
15.right
16  :markdown
17    Jog is a way of using Scalate with JAXRS (using the [Jersey](https://jersey.dev.java.net/) reference implementation) for the RESTful controller layer along with [Guice](http://code.google.com/p/google-guice/) for dependency injection
18
19    The quickest way to get up to speed on Jog is trying the [getting started guide](getting-started.html). Though rather than creating the **jersey** archetype, create the **guice** archetype. So follow the instructions to [install the Scalate Tool](installing.html) then run
20
21        scalate create guice mygroup myartifactid
22
23.left
24  :markdown
25    # Dependency Injection
26.right
27  :markdown
28    Jog uses the [Guice Servlet](http://code.google.com/p/google-guice/wiki/Servlets) approach to configuring your web application along with configuring Scalate and Jersey. 
29
30    This lets you use dependency injection with Guice to configure your web application, the servlets and servlet filters and all the other resources and services used in your application without any icky XML configuration files and keeping a minimal simple web.xml.
31
32    If you try the [getting started guide](getting-started.html) guide, then your running application will have a **src/main/webapp/WEB-INF/web.xml** file which contains the Guice Servlet initialisation configuration. 
33
34    The ServletContextListener class configured in this part of the web.xml file
35
36    {pygmentize:: xml}<listener>
37      <listener-class>somePackage.ServletContextListener</listener-class>
38    </listener>
39    {pygmentize}
40    
41    is the Scala class which creates the Guice Module that performs all the dependency injection for your application which is in the **src/main/scala** directory. 
42
43    So if you need to inject any new services via Guice, just add a new **@Provides** annotated method to the ServletContextListener class.  See the comments in the code for samples.
44    
45.left
46  :markdown
47    # Scalate and JAXRS
48
49.right
50  :markdown
51  
52    Scalate operates as an implicit view provider in [Jersey](https://jersey.dev.java.net/). So if create a JAXRS resource which is annotated with **@ImplicitProduces** then your Jog web application will look for Scalate templates to render a HTML.
53
54    This uses a naming convention where Scalate will look in your web application for the view to use based on the class name of the resource bean being rendered. 
55
56    For example if you had a resource bean like this
57
58    {pygmentize:: scala}
59    package somePackage
60
61    import com.sun.jersey.api.view.ImplicitProduces
62    import javax.ws.rs.{GET, Path, Produces}
63
64    @ImplicitProduces(Array("text/html;qs=5"))
65    @Path("/foo")
66    @Produces(Array("text/xml", "application/json"))
67    class MyResource {
68  
69      @GET
70      def get = new SomeDTO(this)
71    }
72    {pygmentize}
73
74    If you ask for the URI **/foo** this resource can be rendered using **SomeDTO** as XML/JSON, or if a web browser or REST client asks for media type *text/html* Scalate will look for the template called **somePackage/MyResource.index.ssp** or  **somePackage/MyResource.index.scaml**.
75
76    You can have multiple HTML views of the resource by appending the view name. For example requesting **/foo/edit** would look for templates **somePackage/MyResource.edit.ssp** or  **somePackage/MyResource.edit.scaml**.
77
78.left
79  :markdown
80    # See Also
81    
82.right
83  :markdown
84    * [WAR Overlay](war-overlay.html) describes how we use a WAR overlay to include the [Console](console.html) in your application
85    * [Scalate Console](console.html)
86    * [Documentation](index.html) for further information on user guides and template references
87