Recent Changes - Search:

WebFacets

HOW-TOs

Authentication



SourceForge Logo









YourKit Java Profiler

ViewResolvers

This "pattern" allows to implement profile-based navigation and rendering, by selecting appropriate views for given profiles. This is one of the most common usages of WebFacets.

Basically, you simply use facets to decide which view to be used to render a particular page (or part of it - see CompositePages). The controller, when the job is done, simply retrieves a facet and invokes it to get the view to dispatch to (if you don't need no pre-controller, look at ViewHelpers).

It goes more or less like :

  1. The controller handles the request and does his job (validation, params, back-end, etc.)
  2. It retrieves a facet for the current profile
  3. It invokes a method on the facet that returns the view to be rendered
  4. It renders the view just as if it obtained it himself (or if it was "hardcoded" into it)

NOTE
We talk a lot about profiles here, but don't forget that JFacets is also about objects : using facets to select views for particular target objects is pretty cool as well :-)

Here under is a code snippet that shows how the view can be resolved using a facet, from inside a fictious servlet (or an action in an MVC framework) :

// somewhere in a servlet, we get the jFacets bean...
WebFacets wf = WebFacetsFilter.getJFacets(request);
// we execute a facet that returns the path to a JSP
String path = wf.execFacet("view", profileId, anObject);
// and we forward to it
request.getRequestDispatcher().forward(request, response);

Of course, you could have the same inside a JSP, using the WebFacets taglib :

<wf:execFacet name="view" profileId="${profileId}" targetObject="${anObject}" var="path"/>
<jsp:forward page="${path}"/>

View abstraction

Of course, every view technology has its specificities and is manipulated differently from controllers. Some use request dispatch (Struts, Stripes, WW, etc.) , others use "component renderers" (JSF, Echo, etc.), so there's no one-size-fits-all solution for a integration of facets to all of them. But since facets are very simple and are just POJOs, you should be able to write a few lines of code to wrap everything and use facet-based view resolution. You can have a look to the Examples?, there is a very basic implementation of this pattern using plain JSPs.

In order to provide a ready-to-use solution, a base net.sourceforge.jfacets.web.view.PathBasedViewResolverFacet is included in JFacets by default. It allows to specify a path (URL) for the facet by extending this class, and return this path when the facet is executed. You simply have to extend this base class, override the getPath() method, and assign this facet in your XML facets descriptor. At run-time, you simply execute the facet and dispatch to the returned path. Of course, you can also write a simple Groovy Script that returns the path to be used !

Edit - History - Print - Recent Changes - Search
Page last modified on August 31, 2006, at 10:24 AM