Spring MVC + JSF (Facelets)

Você precisa estar logado para dar um feedback. Clique aqui para efetuar o login
Para efetuar o download você precisa estar logado. Clique aqui para efetuar o login
Confirmar voto
0
 (0)  (1)

O artigo mostra como integrar as principais características de 2 frameworks populares no mercado: Spring MVC e JSF, respectivamente para a Camada de gerenciamento de requisições transações e lógicas(M) e a Camanda de visualização de artefatos Ht

Passo 1 - Inclua a classe abaixo em seu projeto:

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



org.springframework.web.jsf.DelegatingVar iableResolver

org.springframework.web.jsf.el.SpringBean FacesELResolver
......

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}


outros links sobre o assunto:

Destaque - SpringBrasil 

SpringSource
 
Você precisa estar logado para dar um feedback. Clique aqui para efetuar o login
Receba nossas novidades
Ficou com alguma dúvida?