Beyond PrettyFaces - Einführung in Rewrite

  • 650 views
Uploaded on

My Rewrite talk on JAX 2013

My Rewrite talk on JAX 2013

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
650
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Christian Kaltepoth | ingenit GmbH & Co. KGBeyond PrettyFacesEinführung in Rewrite
  • 2. URL-Rewriting
  • 3. WikipediaA rewrite engine is software located in aWeb application framework running on aWeb server that modifies a web URLsappearance.This modification is called URL rewriting.http://en.wikipedia.org/wiki/Rewrite_engine
  • 4. Beispielhttp://www.onlineshop.de/b/ref=sa_menu_desk3?ie=UTF8&node=2193272340http://www.onlineshop.de/elektronik
  • 5. Sprechende RESTful URLshttp://www.javaserverfaces.org/newshttp://jax.de/2013/sessions/https://github.com/ocpsoft/rewrite/issues/87http://stackoverflow.com/questions/tagged/jsf
  • 6. Wozu das Ganze?
  • 7. Vorteile• Adressierbare Informationen– Wo bin ich hier?– "Vertrauen"• Reload und Bookmarks• Einfache HTML Links– Lose Kopplung• Technologieneutralität
  • 8. SEO• Keywords in URL• Optimierung des Rankingshttp://www.amazon.com/JavaServer-Faces-2-0-Complete-Reference/dp/0071625097
  • 9. PrettyFaces• JSF URL-Rewriting De-facto-Standard• RESTful URLs• Page Actions• Einfache Rewrite Engine• Dynamic Views• Integration mit JSF Navigation
  • 10. Warum Rewrite?
  • 11. API AbhängigkeitenServlet-spezifischJSF-spezifischPrettyFaces
  • 12. Einschränkungen• Mapping nur via Request Path• Eingeschränkte Konfiguration via XML• Annotation API „verbesserungswürdig“• Konvertierung nur eingeschränktmöglich• Nicht besonders erweiterbar
  • 13. Der Neuanfang
  • 14. Was ist Rewrite?
  • 15. Key Features• Servlet basiertes Rewriting auf Basiseiner Rule-Engine• Framework Integration– JSF, CDI, Spring, Shiro, etc.• Konfiguration: Java DSL + Annotations• Fokus auf Erweiterbarkeit• Open Source (Apache 2.0)
  • 16. Begriffe• Configuration:– Sortierte Listevon Rules• Rule:– Conditions– Operations– Priority
  • 17. Rewriting TypesInboundOutbound
  • 18. InboundGET /faces/home.xhtml HTTP/1.1Host: www.acme.comConnection: keep-alive[....]
  • 19. Outbound<a href="/faces/home.xhtml">Getting started</a>
  • 20. Java DSL
  • 21. Warum?• Typensichere Konfiguration• Code Assist durch IDE• Erweiterbar• Geführte Konfiguration• „Plain Java“
  • 22. Java DSLpublic class RewriteConfig extends HttpConfigurationProvider {@Overridepublic Configuration getConfiguration(ServletContext ctx) {// Konfiguration}@Overridepublic int priority() {return 10;}}
  • 23. ConfigurationBuilderreturn ConfigurationBuilder.begin()// Variante 1.addRule().when( /* condition */ ).perform( /* operation */ )// Variante 2.addRule( /* rule */ );
  • 24. Initial Redirecthttp://www.acme.com/http://www.acme.com/faces/home.xhtmlRedirect
  • 25. Beispiel: Initial Redirect.addRule().when(Direction.isInbound().and(Path.matches("/"))).perform(Redirect.permanent("/faces/home.xhtml"))
  • 26. Der erste Rewritehttp://www.acme.com/faces/home.xhtmlhttp://www.acme.com/home
  • 27. Der erste Rewrite.addRule().when(Direction.isInbound().and(Path.matches("/home"))).perform(Forward.to("/faces/home.xhtml")).addRule().when(Direction.isOutbound().and(Path.matches("/faces/home.xhtml"))).perform(Substitute.with("/home"))
  • 28. Einfacher: Joins.addRule(Join.path("/home").to("/faces/home.xhtml"))
  • 29. Parameter/faces/products.xhtml?category=books/products/books
  • 30. JSF 2.0 View Parameter<f:metadata><f:viewParam name="category"value="#{productListPage.category}" /></f:metadata>@Named@RequestScopedpublic class ProductListPage {private String category;}
  • 31. Join mit Parametern.addRule(Join.path("/products/{category}").to("/faces/products.xhtml"))
  • 32. Demo
  • 33. Annotations?
  • 34. Einfacher Join@Named@RequestScoped@Join(path = "/home",to = "/faces/home.xhtml")public class HomePage {/* your code */}
  • 35. Parameter/faces/products.xhtml?category=books/products/books
  • 36. Mit View-Parametern@Named@RequestScoped@Join(path = "/products/{category}",to = "/faces/products.xhtml")public class ProductListPage {// <f:viewParam name=“category“ ...>private String category;/* ... */}
  • 37. Ohne View-Parameter@Named@RequestScoped@Join(path = "/products/{category}",to = "/faces/products.xhtml")public class ProductListPage {@Parameterprivate String category;/* ... */}
  • 38. Validierung@Named@RequestScoped@Join(path = "/products/{category}",to = "/faces/products.xhtml")public class ProductListPage {@Parameter@Matches("[a-zA-Z-]+")private String category;/* ... */}
  • 39. JSF Validators@Named@RequestScoped@Join(path = "/products/{category}",to = "/faces/products.xhtml")public class ProductListPage {@Parameter@Validate(with = CategoryValidator.class)private String category;/* ... */}
  • 40. Request Actions
  • 41. Request Actions@Named@RequestScoped@Join(path = "/home",to = "/faces/home.xhtml")public class HomePage {@RequestActionpublic void init() {/* your code */}}
  • 42. Ignore Postbacks@Named@ViewScoped@Join(path = "/home",to = "/faces/home.xhtml")public class HomePage {@RequestAction@IgnorePostbackpublic void init() {/* your code */}}
  • 43. Deferral@Named@ViewScoped@Join(path = "/home",to = "/faces/home.xhtml")public class HomePage {@RequestAction@Deferred(before = Phase.RENDER_RESPONSE)public void prepareRender() {/* your code */}}
  • 44. Navigation
  • 45. Navigation<h:link outcome="/products.xhtml"><f:param name="category" value="books"/>Bücher</h:link><a href="/products/books">Bücher</a>
  • 46. Navigationpublic class SomePage {public String actionMethod() {/* do something */return "/products.xhtml?category=books" +"&faces-redirect=true";}}
  • 47. Navigationpublic class SomePage {public Navigate actionMethod() {/* do something */return Navigate.to(ProductListPage.class).with("category", "books");}}
  • 48. Was kann Rewrite noch?
  • 49. Content Delivery Networks(CDN)
  • 50. JSF Resources<h:outputScript name="jquery.js" /><script type="text/javascript"src="/faces/javax.faces.resource/jquery.js" /><script type="text/javascript"src="http://dh8sm43.cloudfront.net/jquery.js" />ErzeugtGewünscht
  • 51. CDN URL Relocation.addRule(CDN.relocate("/faces/javax.faces.resource/jquery.js").to("http://dh8sm43.cloudfront.net/jquery.js"))
  • 52. ResourceTransformation
  • 53. HTTP ResponseRewriteTransformationPipeline
  • 54. Usecases• Minification– JavaScript, CSS• Compression– GZIP, Deflate• Rendering– SASS, SCSS, Markdown, Textile, ...• Custom Processing
  • 55. JavaScript Minify.addRule().when(Direction.isInbound().and(Path.matches("/faces/javax.faces.resource/{*}.js"))).perform(Transform.with(Minify.js()))
  • 56. Rendering
  • 57. Beispiel: Sass$blue: #3bbfce;$margin: 16px;.content-navigation {border-color: $blue;color:darken($blue, 9%);}.border {padding: $margin / 2;margin: $margin / 2;border-color: $blue;}.content-navigation {border-color: #3bbfce;color: #2b9eab;}.border {padding: 8px;margin: 8px;border-color: #3bbfce;}
  • 58. Beispiel: Sass.addRule().when(Direction.isInbound().and(Path.matches("/styles/{*}.sass"))).perform(Response.setContentType("text/css").and(Transform.with(Sass.compiler())));
  • 59. Wie migriere ich meinePrettyFaces Anwendung?
  • 60. Rewrite PrettyFaces Module<dependency><groupId>org.ocpsoft.rewrite</groupId><artifactId>rewrite-config-prettyfaces</artifactId><version>2.0.0.Final</version></dependency>• Drop-In Replacement für PrettyFaces• „Sanfte“ Migration
  • 61. Thank you!http://ocpsoft.org/rewrite/Christian Kaltepothchristian@kaltepoth.de@chkal