package javax.faces.mvc.view;
imports ...
public class FaceletView extends AbstractUrlBasedView {
private Lifecycle facesLifecycle;
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
facesLifecycle = createFacesLifecycle();
}
@Override
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
FacesContext facesContext = createFacesContext(request, response);
populateRequestMap(facesContext, model);
FaceletView.notifyBeforeListeners(PhaseId.RESTORE_ VIEW, facesLifecycle, facesContext);
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
viewHandler.initView(facesContext);
UIViewRoot viewRoot = viewHandler.createView(facesContext, getUrl());
Assert.notNull(viewRoot, "A JSF view could not be created for " + getUrl());
viewRoot.setLocale(RequestContextUtils.getLocale(r equest));
viewRoot.setTransient(true);
facesContext.setViewRoot(viewRoot);
FaceletView.notifyAfterListeners(PhaseId.RESTORE_V IEW, facesLifecycle, facesContext);
facesContext.setViewRoot(viewRoot);
facesContext.renderResponse();
try {
FaceletView.notifyBeforeListeners(PhaseId.RENDER_R ESPONSE, facesLifecycle, facesContext);
logger.debug("Asking view handler to render view");
facesContext.getApplication().getViewHandler().ren derView(facesContext, viewRoot);
FaceletView.notifyAfterListeners(PhaseId.RENDER_RE SPONSE, facesLifecycle, facesContext);
} catch (IOException e) {
throw new FacesException("An I/O error occurred during view rendering", e);
} finally {
logger.debug("View rendering complete");
facesContext.responseComplete();
facesContext.release();
}
}
private void populateRequestMap(FacesContext facesContext, Map model) {
Iterator i = model.keySet().iterator();
while (i.hasNext()) {
String key = i.next().toString();
facesContext.getExternalContext().getRequestMap(). put(key, model.get(key));
}
}
private FacesContext createFacesContext(HttpServletRequest request, HttpServletResponse response) {
FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTE XT_FACTORY);
return facesContextFactory.getFacesContext(getServletCont ext(), request, response, facesLifecycle);
}
private Lifecycle createFacesLifecycle() {
LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_F ACTORY);
return lifecycleFactory.getLifecycle(LifecycleFactory.DEF AULT_LIFECYCLE);
}
public static void notifyAfterListeners(PhaseId phaseId, Lifecycle lifecycle, FacesContext context) {
PhaseEvent afterPhaseEvent = new PhaseEvent(context, phaseId, lifecycle);
for (int i = 0; i < lifecycle.getPhaseListeners().length; i++) {
PhaseListener listener = lifecycle.getPhaseListeners()[i];
if (listener.getPhaseId() == phaseId || listener.getPhaseId() == PhaseId.ANY_PHASE) {
listener.afterPhase(afterPhaseEvent);
}
}
}
public static void notifyBeforeListeners(PhaseId phaseId, Lifecycle lifecycle, FacesContext context) {
PhaseEvent beforePhaseEvent = new PhaseEvent(context, phaseId, lifecycle);
for (int i = 0; i < lifecycle.getPhaseListeners().length; i++) {
PhaseListener listener = lifecycle.getPhaseListeners()[i];
if (listener.getPhaseId() == phaseId || listener.getPhaseId() == PhaseId.ANY_PHASE) {
listener.beforePhase(beforePhaseEvent);
}
}
}
}
Passo 2 - Configure o Dispacher Servlet ViewResolver conforme abaixo:
Passo 3 - Configure o faces-config
<variable-resolver>org.springframework.web.jsf.DelegatingVar iableResolver</variable-resolver>
<el-resolver>org.springframework.web.jsf.el.SpringBean FacesELResolver</el-resolver>
......
Passo 4 - Criando o HelloController
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(@RequestParam(value = "name", defaultValue = "Visitor") String name) {
ModelAndView view = new ModelAndView("helloView");
view.addObject("helloMessage", "Hello, " + name + "!");
return view;
}
Passo 5 - Criando a Facelet-View (/WEB-INF/views/helloView.xhtml)
....
Hello ${helloMessage}