Presenting ActiveWeb making Java Web programming fun...again! Igor Polevoy
About me <ul><li>Developer like you
Have bad aftertaste of many web frameworks
Presented ActiveJDBC to CJUG last year at about the same time
Was suggested to shut up and make a change
Currently a Chief Technologist at ProductiveEdge, Chicago </li></ul>
But why? Don't we have enough frameworks in Java? Restlet Brill Aranea Web Framework RSF JSF RichFaces Strecks Google Web ...
All we need is: <ul><li>Simple to use, sophisticated on the inside
Full stack
Supporting TDD
Can test views
Dynamic
Clean URLs
Fast, lightweight (as few dependencies as possible)
Conforms to (few good) standards
Fun(because of immediate gratification) </li></ul>
History <ul><li>First version in 2009
In parallel development with ActiveJDBC
First put in production in summer 2010
Currently in production at major insurance company, 4 websites, clustered REST web services, internal tools, displacing le...
Meet new friend Navigate  to: http://localhost:8080/testapp/greeting?name=Bob Executes: public   class   GreetingControlle...
Lets TDD this public   cla ss  G r eetingControllerSpec   extends   ControllerSpec{ @Test public   void   shouldRenderHell...
Configuration in code public   class   DbConfig   extends   AbstractDBConfig   { public   void   init(AppContext   context...
Structure of project Standard Maven structure,  <ul><li>View are located under:  </li><ul><li>/WEB-INF/views </li></ul><li...
Layouts Default Layout: src/main/webapp/WEB-INF/views/layouts/default_layout.ftl < html > < head > < LINK   href = &quot;$...
<@content for and <@yield Page titles with custom tags < @content   for = &quot;title&quot; > Books List </@content> This ...
Unobtrusive JS and <@link_to < form   id = &quot;da_form&quot;   > First name:  < input   type = &quot;text&quot;   name =...
Partials Naming src/main/webapp/WEB-INF/views/greeting/_hello.ftl Rendering a partial: < @render   partial = &quot;hello&q...
Upcoming SlideShare
Loading in …5
×

ActiveWeb: Chicago Java User Group Presentation

3,141 views

Published on

Presentation on ActiveWeb given to Chicago Java User Group on August 16th 2011

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,141
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
20
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

ActiveWeb: Chicago Java User Group Presentation

  1. 1. Presenting ActiveWeb making Java Web programming fun...again! Igor Polevoy
  2. 2. About me <ul><li>Developer like you
  3. 3. Have bad aftertaste of many web frameworks
  4. 4. Presented ActiveJDBC to CJUG last year at about the same time
  5. 5. Was suggested to shut up and make a change
  6. 6. Currently a Chief Technologist at ProductiveEdge, Chicago </li></ul>
  7. 7. But why? Don't we have enough frameworks in Java? Restlet Brill Aranea Web Framework RSF JSF RichFaces Strecks Google Web Toolkit Aurora JPublish Jucas MyFaces WebOnSwing Chrysalis VRaptor SwingWeb Barracuda ThinWire Struts1/2 Turbine Tapestry Cocoon Spring Maverick Echo SOFIA Verge Anvil Jaffa Japple RIFE Swinglets Millstone Wicket DWR JSPWidget JOSSO JAT OpenXava Stripes Click ZK Caramba wingS Helma
  8. 8. All we need is: <ul><li>Simple to use, sophisticated on the inside
  9. 9. Full stack
  10. 10. Supporting TDD
  11. 11. Can test views
  12. 12. Dynamic
  13. 13. Clean URLs
  14. 14. Fast, lightweight (as few dependencies as possible)
  15. 15. Conforms to (few good) standards
  16. 16. Fun(because of immediate gratification) </li></ul>
  17. 17. History <ul><li>First version in 2009
  18. 18. In parallel development with ActiveJDBC
  19. 19. First put in production in summer 2010
  20. 20. Currently in production at major insurance company, 4 websites, clustered REST web services, internal tools, displacing legacy systems (Spring/Hibernate) </li></ul>
  21. 21. Meet new friend Navigate to: http://localhost:8080/testapp/greeting?name=Bob Executes: public class GreetingController extends AppController{ public void index(){ view(“name”, param(“name”)); } } Renders: /WEB-INF/views/greeting/index.ftl View code: Hello, ${name}! Output: Hello, Bob! A few conventions at work here: URL to Controller Default action view location by controller name view name by action name No configuration. In fact, ActiveWeb has no property files, no XML, no Yaml, no text files of any kind.
  22. 22. Lets TDD this public cla ss G r eetingControllerSpec extends ControllerSpec{ @Test public void shouldRenderHelloWorld(){ request().param(“name”, “Bob”).get(“index”); a(assigns().get(“name”)).shouldBeEqual(“Bob”); } } Test HTML content: public class G reet ingControllerSpec extends ControllerSpec{ @Test public void shouldRenderHelloWorld(){ request().param(“name”, “Bob”). integrateViews () .get(“index”); a(responseContent().contains(“Hello, Bob!”)).shouldBeTrue(); } } Convention at work: Controller name from spec name.
  23. 23. Configuration in code public class DbConfig extends AbstractDBConfig { public void init(AppContext context) { environment( &quot;development&quot; ) .jndi( &quot;jdbc/kitchensink_development&quot; ); environment( &quot;development&quot; ).testing() .jdbc( &quot;com.mysql.jdbc.Driver&quot; , &quot;jdbc:mysql://localhost/kitchensink_development&quot; , &quot;root&quot; , &quot;****&quot; ); environment( &quot;hudson&quot; ).testing() .jdbc( &quot;com.mysql.jdbc.Driver&quot; , &quot;jdbc:mysql://172.30.64.31/kitchensink_hudson&quot;, &quot;root&quot;, &quot;****&quot; ); environment( &quot;production&quot; ) .jndi( &quot;jdbc/kitchensink_production&quot; ); } } DSL for environments, JNDI, JDBC and testing mode You get help from IDE and from compiler, less likely to make a typo
  24. 24. Structure of project Standard Maven structure, <ul><li>View are located under: </li><ul><li>/WEB-INF/views </li></ul><li>Controllers are in </li><ul><li>app.controllers package . </li></ul></ul>Result: Huge selection of anything built under the sun for Maven in general and Maven Web specifically
  25. 25. Layouts Default Layout: src/main/webapp/WEB-INF/views/layouts/default_layout.ftl < html > < head > < LINK href = &quot;${context_path}/css/main.css&quot; rel = &quot;stylesheet&quot; /> < script src = &quot;${context_path}/js/jquery-1.4.2.min.js&quot; </ script > < script src = &quot;${context_path}/js/aw.js&quot; </ script > < title > ActiveWeb - < @yield to = &quot;title&quot; /></ title > </ head > < body > < div class = &quot;main&quot; > < #include &quot;header.ftl&quot; > ${page_content} < #include &quot;footer.ftl&quot; > </ div > </ body > </ html > Serves the same purpose as Tiles or Sitemesh, but integrated into the system as another template. There are wrapper/nested layouts too.
  26. 26. <@content for and <@yield Page titles with custom tags < @content for = &quot;title&quot; > Books List </@content> This sends content to < @yield to = &quot;title&quot; /> located in layout Can send any content to layout with content tag, including links to CSS, JS, etc: < @content for = &quot;js&quot; > < script src = &quot;${context_path}/js/page_specific.js&quot; type = &quot;text/javascript&quot; ></ script > </ @ > Content will be rendered in layout in place of < @yield to = &quot;js&quot; /> This allows to easily declare content specific for a page, but rendered outside page context.
  27. 27. Unobtrusive JS and <@link_to < form id = &quot;da_form&quot; > First name: < input type = &quot;text&quot; name = &quot;first_name&quot; >< br > Last name: < input type = &quot;text&quot; name = &quot;last_name&quot; > </ form > < @link_to controller = &quot;people&quot; action = &quot;do-get&quot; form = &quot;da_form&quot; destination = &quot;result&quot; > Ajax Get </ @ > Result will be inserted into: < div id = &quot;result&quot; ></ div > No JavaScript is generated, the HTML page is clean More ways to use Ajax with link_to Magic happens in aw.js
  28. 28. Partials Naming src/main/webapp/WEB-INF/views/greeting/_hello.ftl Rendering a partial: < @render partial = &quot;hello&quot; /> Rendering a collection with a partial (no ugly for loops building iterative HTML): Content of _fruit.ftl : Fruit name: ${fruit}<hr> Host page: < @render partial = &quot;fruit&quot; collection = fruits /> Result of rendering: Fruit name: apple<hr>Fruit name: prune<hr>Fruit name: pear<hr> Partial will iterate itself. Also: spacers, counters, first and last.
  29. 29. Lets flash public class BooksController extends AppController { @POST public void create(){ Book book = new Book(); book.fromMap(params1st()); if (book.save()){ flash( &quot;message&quot; , &quot;New book was added: &quot; + book.get( &quot;title&quot; )); redirect(BooksController. class ); } else { //handle errors } } } //Use in view: < @flash name = &quot;message&quot; /> Flash is a short-lived object, survives only one more request Use in POST/redirect for destructive operations
  30. 30. Custom tags 1. Develop: public class HelloTag extends FreeMarkerTag{ protected void render(Map params, String body, Writer writer) throws Exception { writer.write(“hello”); } } 2. Register: public class FreeMarkerConfig extends AbstractFreeMarkerConfig { public void init() { registerTag(“hello”, new HelloTag()); } } 3. Use: < @hello />
  31. 31. Dependency Injection with Google Guice public class HelloController extends AppController { private Greeter greeter; public void index(){ view( &quot;message&quot; , greeter.greet()); } @Inject public void setGreeter( Greeter greeter) { this .greeter = greeter; } } public class GreeterModule extends AbstractModule { protected void configure() { bind(Greeter. class ) .to(GreeterImpl. class ).asEagerSingleton(); } } public class AppBootstrap extends Bootstrap { public void init(AppContext context) { setInjector(Guice.createInjector( new GreeterModule())); } }
  32. 32. TDD with DI public class GreeterMock implements Greeter{ public String greet() { return &quot;Hello from &quot; + getClass().toString(); } } public class GreeterMockModule extends AbstractModule { @Override protected void configure() { bind(Greeter. class ).to(GreeterMock. class ).asEagerSingleton(); } } public class HelloControllerSpec extends ControllerSpec { @Before public void before(){ setInjector(Guice.createInjector( new GreeterMockModule())); } @Test public void shouldTestWithMockService(){ request().get( &quot;index&quot; ); a(assigns().get( &quot;message&quot; )).shouldBeEqual( &quot;Hello from class app.services.GreeterMock&quot; ); } } Can use any mocking framework.
  33. 33. Making tests web specific “ Send” parameters to controller: public class HelloControllerSpec extends ControllerSpec{ @Test public void shouldSendParamsToIndex(){ request() .param( &quot;first_name&quot; , &quot;John&quot; ).param( &quot;last_name&quot; , &quot;Deere&quot; ) .get( &quot;index&quot; ); a(assigns().get( &quot;message&quot; )) .shouldBeEqual( &quot;Hello, John Deere, welcome back!&quot; ); } } Seeing HTML in test! public class HelloControllerSpec extends ControllerSpec @Test public void shouldPrintGeneratedHTML(){ request(). integrateViews ().get( &quot;index&quot; ); System.out.println(responseContent()); // prints entire HMTL, decorated by layout. } }
  34. 34. What else in tests? <ul><li>Transaction rolled back
  35. 35. Post binary content
  36. 36. “ Upload” files
  37. 37. “ Send” GET, POST, DELETE, PUT HTTP requests
  38. 38. Sessions
  39. 39. Cookies
  40. 40. Controller scenarios with IntegrationTests.
  41. 41. Bootstrap entire application with AppIntegrationTests </li></ul>
  42. 42. REST web services Controller: public class Books Controller extends AppController { public void index(){ List<Book> books = Book.findAll(); view( &quot; books&quot;, books); render().noLayout(); } } View: <? xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot; ?> < books > < #list books as book > < book > < isbn > ${book.isbn} </ isbn > < title > ${book.title} </ title > < author > ${book.author} </ author > </ book > </ #list > </ books > Access: http://host:port/context/books
  43. 43. What else can controllers do? //getting parameters String name = param(“name”); List<String> selectValues = param(“myselect”); List<String> params = params1st(); Map<String, String[]> allParams = params(); //passing data to view view(“name”, name); assign(“name”, name); //detecting Ajax: if (xhr()){...} else {...} //Responding directly (short hand XML service): respond( &quot;<message>hello</message>&quot; ) .contentType( &quot;text/xml&quot; ).status( 200 ); //sending view with no layout //(useful in web services when a view is used): render().noLayout();
  44. 44. Binary content in controllers //Downloading binary to client: sendFile(f).contentType( &quot;application/pdf&quot; ).status( 200 ); //Streaming large content: streamOut(in).contentType( &quot;applicaiton/pdf&quot; ); //Uploading files: Iterator<FormItem> iterator = uploadedFiles(); while (iterator.hasNext()){ FormItem item = iterator.next(); name = item.getName(); if (item.isFile()){ InputStream in = item.getInputStream()); //process data } }
  45. 45. @RESTful routing @RESTful public BooksController extends AppController{} verb path action used for ------------------------------------------------------------------------------------- GET /books index display a list of all books GET /books/new_form new_form HTML form for creating a new book POST /books create create a new book GET /books/id show display a specific book GET /books/id/edit_form edit_form return an HTML form for editing a books PUT /books/id update update a specific book DELETE /books/id destroy delete a specific book
  46. 46. Standard routing public BooksController extends AppController{ //GET by default public void index(){} @PUT public void save(); @DELETE public void delete(); @POST public void update(); } Allows only one HTTP method per action
  47. 47. GWT Support Compile GWT client.... and PRC server on the fly: GWT server: public class EchoController extends GWTAppController implements EchoService { public String echo(String text) { return &quot;Hello from server :&quot; + text + &quot;,.... and time now is: &quot; + new Date() ; } } GWT client: business as usual GWT DEMO
  48. 48. Ad hock development demo <ul><li>Start container
  49. 49. Access non-existing controller
  50. 50. See error messages
  51. 51. Add controller
  52. 52. Add action
  53. 53. Add view
  54. 54. Pass data </li></ul>
  55. 55. ActiveWeb and ActiveJDBC <ul><li>ActiveJDBC is not baked into ActiveWeb
  56. 56. ActiveJDBC is a general purpose ORM for Java
  57. 57. ActiveWeb can be used with any other ORM, or without one
  58. 58. ActiveWeb has nice features for managing a DB connection at runtime and during tests – tailored for ActiveJDBC, but in either case you can get access to java.sql.Connection and do with it as you wish:
  59. 59. java.sql.Connection con = Base.connection(); </li></ul>
  60. 60. Conclusion <ul><li>Be happier with real TDD
  61. 61. Increase productivity
  62. 62. Have access to anything Java under the sun (there is a lot) </li></ul>

×