/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. ---
  2. title: Jog
  3. in_menu: false
  4. sort_info: 2
  5. --- name:overview
  6. # Jog
  7. Using Scalate with JAXRS (Jersey) on Guice
  8. --- name:content pipeline:jade
  9. .left
  10. :markdown
  11. # Overview
  12. .right
  13. :markdown
  14. 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
  15. 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
  16. scalate create guice mygroup myartifactid
  17. .left
  18. :markdown
  19. # Dependency Injection
  20. .right
  21. :markdown
  22. 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.
  23. 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.
  24. 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.
  25. The ServletContextListener class configured in this part of the web.xml file
  26. {pygmentize:: xml}<listener>
  27. <listener-class>somePackage.ServletContextListener</listener-class>
  28. </listener>
  29. {pygmentize}
  30. 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.
  31. 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.
  32. .left
  33. :markdown
  34. # Scalate and JAXRS
  35. .right
  36. :markdown
  37. 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.
  38. 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.
  39. For example if you had a resource bean like this
  40. {pygmentize:: scala}
  41. package somePackage
  42. import com.sun.jersey.api.view.ImplicitProduces
  43. import javax.ws.rs.{GET, Path, Produces}
  44. @ImplicitProduces(Array("text/html;qs=5"))
  45. @Path("/foo")
  46. @Produces(Array("text/xml", "application/json"))
  47. class MyResource {
  48. @GET
  49. def get = new SomeDTO(this)
  50. }
  51. {pygmentize}
  52. 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**.
  53. 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**.
  54. .left
  55. :markdown
  56. # See Also
  57. .right
  58. :markdown
  59. * [WAR Overlay](war-overlay.html) describes how we use a WAR overlay to include the [Console](console.html) in your application
  60. * [Scalate Console](console.html)
  61. * [Documentation](index.html) for further information on user guides and template references