Beyond PrettyFaces - Einführung in Rewrite

Christian Kaltepoth
Christian KaltepothSenior Software Developer um ingenit GmbH & Co. KG
Christian Kaltepoth | ingenit GmbH & Co. KG
Beyond PrettyFaces
Einführung in Rewrite
URL-Rewriting
Wikipedia
A rewrite engine is software located in a
Web application framework running on a
Web server that modifies a web URL's
appearance.
This modification is called URL rewriting.
http://en.wikipedia.org/wiki/Rewrite_engine
Beispiel
http://www.onlineshop.de/b/ref=sa_m
enu_desk3?ie=UTF8&node=2193272340
http://www.onlineshop.de/elektronik
Sprechende RESTful URLs
http://www.javaserverfaces.org/news
http://jax.de/2013/sessions/
https://github.com/ocpsoft/rewrite/issues/87
http://stackoverflow.com/questions/tagged/jsf
Wozu das Ganze?
Vorteile
• Adressierbare Informationen
– Wo bin ich hier?
– "Vertrauen"
• Reload und Bookmarks
• Einfache HTML Links
– Lose Kopplung
• Technologieneutralität
SEO
• Keywords in URL
• Optimierung des Rankings
http://www.amazon.com/JavaServer-
Faces-2-0-Complete-
Reference/dp/0071625097
PrettyFaces
• JSF URL-Rewriting De-facto-Standard
• RESTful URLs
• Page Actions
• Einfache Rewrite Engine
• Dynamic Views
• Integration mit JSF Navigation
Warum Rewrite?
API Abhängigkeiten
Servlet-spezifisch
JSF-spezifisch
PrettyFaces
Beyond PrettyFaces - Einführung in Rewrite
Einschränkungen
• Mapping nur via Request Path
• Eingeschränkte Konfiguration via XML
• Annotation API „verbesserungswürdig“
• Konvertierung nur eingeschränkt
möglich
• Nicht besonders erweiterbar
Der Neuanfang
Was ist Rewrite?
Key Features
• Servlet basiertes Rewriting auf Basis
einer Rule-Engine
• Framework Integration
– JSF, CDI, Spring, Shiro, etc.
• Konfiguration: Java DSL + Annotations
• Fokus auf Erweiterbarkeit
• Open Source (Apache 2.0)
Begriffe
• Configuration:
– Sortierte Liste
von Rules
• Rule:
– Conditions
– Operations
– Priority
Rewriting Types
Inbound
Outbound
Inbound
GET /faces/home.xhtml HTTP/1.1
Host: www.acme.com
Connection: keep-alive
[....]
Outbound
<a href="/faces/home.xhtml">
Getting started
</a>
Java DSL
Warum?
• Typensichere Konfiguration
• Code Assist durch IDE
• Erweiterbar
• Geführte Konfiguration
• „Plain Java“
Java DSL
public class RewriteConfig extends HttpConfigurationProvider {
@Override
public Configuration getConfiguration(ServletContext ctx) {
// Konfiguration
}
@Override
public int priority() {
return 10;
}
}
ConfigurationBuilder
return ConfigurationBuilder.begin()
// Variante 1
.addRule()
.when( /* condition */ )
.perform( /* operation */ )
// Variante 2
.addRule( /* rule */ )
;
Initial Redirect
http://www.acme.com/
http://www.acme.com/faces/home.xhtml
Redirect
Beispiel: Initial Redirect
.addRule()
.when(
Direction.isInbound().and(Path.matches("/"))
)
.perform(
Redirect.permanent("/faces/home.xhtml")
)
Der erste Rewrite
http://www.acme.com/faces/home.xhtml
http://www.acme.com/home
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"))
Einfacher: Joins
.addRule(
Join.path("/home")
.to("/faces/home.xhtml")
)
Parameter
/faces/products.xhtml?category=books
/products/books
JSF 2.0 View Parameter
<f:metadata>
<f:viewParam name="category"
value="#{productListPage.category}" />
</f:metadata>
@Named
@RequestScoped
public class ProductListPage {
private String category;
}
Join mit Parametern
.addRule(
Join.path("/products/{category}")
.to("/faces/products.xhtml")
)
Demo
Annotations?
Einfacher Join
@Named
@RequestScoped
@Join(path = "/home",
to = "/faces/home.xhtml")
public class HomePage {
/* your code */
}
Parameter
/faces/products.xhtml?category=books
/products/books
Mit View-Parametern
@Named
@RequestScoped
@Join(path = "/products/{category}",
to = "/faces/products.xhtml")
public class ProductListPage {
// <f:viewParam name=“category“ ...>
private String category;
/* ... */
}
Ohne View-Parameter
@Named
@RequestScoped
@Join(path = "/products/{category}",
to = "/faces/products.xhtml")
public class ProductListPage {
@Parameter
private String category;
/* ... */
}
Validierung
@Named
@RequestScoped
@Join(path = "/products/{category}",
to = "/faces/products.xhtml")
public class ProductListPage {
@Parameter
@Matches("[a-zA-Z-]+")
private String category;
/* ... */
}
JSF Validators
@Named
@RequestScoped
@Join(path = "/products/{category}",
to = "/faces/products.xhtml")
public class ProductListPage {
@Parameter
@Validate(with = CategoryValidator.class)
private String category;
/* ... */
}
Request Actions
Request Actions
@Named
@RequestScoped
@Join(path = "/home",
to = "/faces/home.xhtml")
public class HomePage {
@RequestAction
public void init() {
/* your code */
}
}
Ignore Postbacks
@Named
@ViewScoped
@Join(path = "/home",
to = "/faces/home.xhtml")
public class HomePage {
@RequestAction
@IgnorePostback
public void init() {
/* your code */
}
}
Deferral
@Named
@ViewScoped
@Join(path = "/home",
to = "/faces/home.xhtml")
public class HomePage {
@RequestAction
@Deferred(before = Phase.RENDER_RESPONSE)
public void prepareRender() {
/* your code */
}
}
Navigation
Navigation
<h:link outcome="/products.xhtml">
<f:param name="category" value="books"/>
Bücher
</h:link>
<a href="/products/books">
Bücher
</a>
Navigation
public class SomePage {
public String actionMethod() {
/* do something */
return "/products.xhtml?category=books" +
"&faces-redirect=true";
}
}
Navigation
public class SomePage {
public Navigate actionMethod() {
/* do something */
return Navigate.to(ProductListPage.class)
.with("category", "books");
}
}
Was kann Rewrite noch?
Content Delivery Networks
(CDN)
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" />
Erzeugt
Gewünscht
CDN URL Relocation
.addRule(
CDN.relocate("/faces/javax.faces.resource/jquery.js")
.to("http://dh8sm43.cloudfront.net/jquery.js")
)
Resource
Transformation
HTTP Response
Rewrite
Transformation
Pipeline
Usecases
• Minification
– JavaScript, CSS
• Compression
– GZIP, Deflate
• Rendering
– SASS, SCSS, Markdown, Textile, ...
• Custom Processing
JavaScript Minify
.addRule()
.when(
Direction.isInbound().and(Path.matches(
"/faces/javax.faces.resource/{*}.js"))
)
.perform(
Transform.with(Minify.js())
)
Rendering
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;
}
Beispiel: Sass
.addRule()
.when(
Direction.isInbound().and(
Path.matches("/styles/{*}.sass"))
)
.perform(
Response.setContentType("text/css").and(
Transform.with(Sass.compiler()))
);
Wie migriere ich meine
PrettyFaces Anwendung?
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
Thank you!
http://ocpsoft.org/rewrite/
Christian Kaltepoth
christian@kaltepoth.de
@chkal
1 von 62

Más contenido relacionado

Similar a Beyond PrettyFaces - Einführung in Rewrite

Spring Surf 101Spring Surf 101
Spring Surf 101Alfresco Software
2.8K views30 Folien

Similar a Beyond PrettyFaces - Einführung in Rewrite(20)

Spring Surf 101Spring Surf 101
Spring Surf 101
Alfresco Software2.8K views
Microdata semantic-extendMicrodata semantic-extend
Microdata semantic-extend
Seek Tan507 views
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron13.3K views
Advanced SEO for Web DevelopersAdvanced SEO for Web Developers
Advanced SEO for Web Developers
Nathan Buggia5.3K views
Code Generation in Magento 2Code Generation in Magento 2
Code Generation in Magento 2
Sergii Shymko446 views
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
Gabriel Walt4.1K views
Supercharging your Organic CTRSupercharging your Organic CTR
Supercharging your Organic CTR
Phil Pearce2.9K views
URL DesignURL Design
URL Design
Walter Ebert2.6K views
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
Gavin Roy9.3K views
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
Maarten Balliauw2.8K views

Más de Christian Kaltepoth(6)

TypeScript - das bessere JavaScript!?TypeScript - das bessere JavaScript!?
TypeScript - das bessere JavaScript!?
Christian Kaltepoth211 views
JavaScript im Jahr 2016JavaScript im Jahr 2016
JavaScript im Jahr 2016
Christian Kaltepoth266 views
MVC 1.0 - Das neue Webframework in Java EE 8MVC 1.0 - Das neue Webframework in Java EE 8
MVC 1.0 - Das neue Webframework in Java EE 8
Christian Kaltepoth674 views
JSF vs. GWT? JSF und GWT!JSF vs. GWT? JSF und GWT!
JSF vs. GWT? JSF und GWT!
Christian Kaltepoth2.3K views
Feature Flags mit TogglzFeature Flags mit Togglz
Feature Flags mit Togglz
Christian Kaltepoth1.2K views
PrettyFaces: RESTful URLs für JSFPrettyFaces: RESTful URLs für JSF
PrettyFaces: RESTful URLs für JSF
Christian Kaltepoth2.1K views

Beyond PrettyFaces - Einführung in Rewrite