1

Consider this JSP website example. It allows to browse customers in a master/detail fashion. Namely when user supplies link "/customers" then master view is displayed. When user supplies link "/customers?id=4" then details view for the particular is displayed. Technically this is achieved by registering the link "/customers" in web.xml and mapping it to ClientsController.java servlet which acts as a MVC controller. It basically 1) checks whether an "id" attribute is set or not 2) correspondingly sets HttpServletRequest attribute (full list of customer oor details of a particular customer and 3) forwards to master.jsp or details.jsp pages appropriately using getRequestDispatcher() mechanism. Now how to convert this thing to JSF2 by keeping the same fashion of urls and separate master and details Facelets pages?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
mmierins
  • 3,674
  • 4
  • 21
  • 25

1 Answers1

1

As to eliminating the extension in URL, head to this Q&A: Customize FacesServlet <url-pattern> to get rid of .xhtml extension.

As to the master & detail view depending on a request parameter, just make use of <f:viewParam> to set (and convert) the request parameter as a bean property and then make use of rendered attribute to conditionally render the desired content depending on whether the bean property is empty.

<f:metadata>
    <f:viewParam name="id" value="#{bean.customer}" />
<f:metadata>
<ui:fragment rendered="#{empty bean.customer}">
    <ui:include src="/WEB-INF/includes/customer-master.xhtml" /> 
</ui:fragment>
<ui:fragment rendered="#{not empty bean.customer}">
    <ui:include src="/WEB-INF/includes/customer-detail.xhtml" /> 
</ui:fragment>

To survive postbacks, make sure that the #{bean} is @ViewScoped.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555