This document discusses using JavaServer Faces (JSF) for public websites. It presents lightweight Java EE 6 architectures with and without an EE container. It covers using CDI and CODI to manage transactions and persistence outside an EE container. It also discusses using JSF 2 features like h:link and h:button to support HTTP GET requests, view parameters, and POST-redirect-GET. Finally, it introduces PrettyFaces for creating RESTful URLs with JSF and demonstrates examples.
2. Agenda
• Motivation
• Architecture with/without EE 6 container
• Putting the pieces together (CDI and CODI)
• JSF and GET-Requests
• RESTful URLs with JSF and PrettyFaces
• Demonstrations and examples
4. Architecture and Technology Stack
Architecture With EE 6 container No EE 6 container
Presentation JSF, CDI, CODI JSF, CDI, CODI
Service EJB, CDI CDI, CODI
Data access EJB, JPA CDI, CODI, JPA
5. MyFaces Extensions CDI
• Portable extension of CDI
• Simplifies development with CDI and JSF
• Several modules:
• JSF 1.2 and 2.0
• JPA, bean validation...
• Works with:
• CDI: OpenWebBeans, Weld
• JSF: MyFaces, Mojarra (1.2 und 2.0)
• Watch out Apache DeltaSpike
6. Management of Persistence Context
• With Java EE container (EJB)
@javax.ejb.Stateless
public class CustomerRepository {
@PersistenceContext private EntityManager em;
}
• Without Java EE container (CDI + CODI)
@ApplicationScoped
public class CustomerRepository {
@Inject private EntityManager em;
}
7. Transaction Management
• With Java EE container
• Transactions managed by container (@TransactionAttribute)
• Without Java EE container (CDI only)
• Transactions managed by CODI
@ApplicationScoped
public class CustomerService {
@Inject
private CustomerRepository customerRepository;
@Transactional
public void saveUser(User user) {...}
}
10. JSF requests
• HTTP requests in classic JSF
page1.xhtml page2.xhtml page3.xhtml
GET POST POST
/faces/page1.jsf /faces/page1.jsf /faces/page2.jsf
• Problems with HTTP POST:
• Page reload problematic
• URL in browser one step behind
• Lack of bookmarkability/direct linking
• JSF 2: h:link, h:button and view parameters
11. JSF 2: Navigation with h:link and h:button
• Navigation target in attribut outcome
<h:link outcome="/page1.xhtml" value="Page 1"/>
<h:link outcome="/page2.xhtml" value="Page 2">
<f:param name="para" value="1"/>
</h:link>
<h:button outcome="/page3.xhtml" value="Page 3"/>
• Renders link oder button (h:form not necessary)
<a href="/page1.jsf">Page 1</a>
<a href="/page2.jsf?para=1">Page 2</a>
<input type="button" value="Page 3"
onclick="window.location.href = '/pag3.jsf'"/>
12. JSF 2: View Parameters
• Bind request parameters to bean properties
• Tag f:viewParam in f:metadata
<f:metadata>
<f:viewParam name="id" value="#{myBean.id}"/>
</f:metadata>
Converted and
@Named @RequestScoped validated
public class MyBean {
private long id;
public long getId() {return id;}
public void setId(long id) {this.id = id;}
}
13. What about view actions?
• JSF 2.0 "forgot" about them
• Alternative: PreRenderViewEvent
<f:event type="preRenderView" listener="#{bean.preRender}"/>
• Alternative: CODI @PreRenderView
• Real view actions part of JSF 2.2
<f:metadata>
<f:viewParam name="id" value="#{bean.id}"/>
<f:viewAction action="#{bean.load}"/>
</f:metadata>
14. Post-Redirect-Get with JSF 2
• Request /faces/myPage.xhtml?id=1
<h:form>
<h:commandButton action="#{myBean.save}" value="Save"/>
<h:commandButton value="Cancel" immediate="true"
action="myPage?faces-redirect=true&
faces-include-view-params=true"/>
</h:form>
@Named @RequestScoped
public class MyBean {
public String save() {
return "myPage?faces-redirect=true"
+ "&faces-include-view-params=true";
}
}
15. Demonstration
• GET requests with h:link and h:button
• View Parameters
• Post-Redirect-Get with JSF
Beispiel: https://github.com/jsflive/jsf-get02