Administrator
2023-02-17 d3d8ceb38e114db791a7c9eed710065465c3d1da
提交 | 用户 | 时间
58d006 1 package com.mandi.springmvc;
A 2
3 import java.io.IOException;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import javax.faces.FacesException;
8 import javax.faces.FactoryFinder;
9 import javax.faces.application.ViewHandler;
10 import javax.faces.component.UIViewRoot;
11 import javax.faces.context.FacesContext;
12 import javax.faces.context.FacesContextFactory;
13 import javax.faces.event.PhaseEvent;
14 import javax.faces.event.PhaseId;
15 import javax.faces.event.PhaseListener;
16 import javax.faces.lifecycle.Lifecycle;
17 import javax.faces.lifecycle.LifecycleFactory;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 import org.springframework.web.servlet.support.RequestContextUtils;
22 import org.springframework.web.servlet.view.AbstractUrlBasedView;
23
24
25
26 public class FaceletView extends AbstractUrlBasedView {
27     private Lifecycle facesLifecycle;
28
29     @Override
30     public void afterPropertiesSet() throws Exception {
31         super.afterPropertiesSet();
32         facesLifecycle = createFacesLifecycle();
33     }
34     
35     @Override
36     protected void renderMergedOutputModel(Map<String, Object> model,
37             HttpServletRequest request, HttpServletResponse response) throws Exception {
38         logger.info("View rendering start");
39         FacesContext facesContext = createFacesContext(request, response);
40         populateRequestMap(facesContext, model);
41     
42         FaceletView.notifyBeforeListeners(PhaseId.RESTORE_VIEW, facesLifecycle, facesContext);
43     
44         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
45     
46         viewHandler.initView(facesContext);
47     
48         UIViewRoot viewRoot = viewHandler.createView(facesContext, getUrl());
49         viewRoot.setLocale(RequestContextUtils.getLocale(request));
50         viewRoot.setTransient(true);
51     
52         facesContext.setViewRoot(viewRoot);
53     
54         FaceletView.notifyAfterListeners(PhaseId.RESTORE_VIEW, facesLifecycle, facesContext);
55     
56         facesContext.setViewRoot(viewRoot);
57         facesContext.renderResponse();
58         try {
59             FaceletView.notifyBeforeListeners(PhaseId.RENDER_RESPONSE, facesLifecycle, facesContext);
60             logger.debug("Asking view handler to render view");
61             facesContext.getApplication().getViewHandler().renderView(facesContext, viewRoot);
62             FaceletView.notifyAfterListeners(PhaseId.RENDER_RESPONSE, facesLifecycle, facesContext);
63         } 
64         catch (IOException e) {
65             logger.error("An I/O error occurred during view rendering");
66             throw new FacesException("An I/O error occurred during view rendering", e);
67         } finally {
68             logger.info("View rendering complete");
69             facesContext.responseComplete();
70             facesContext.release();
71         }
72     }
73     @SuppressWarnings("rawtypes")
74     private void populateRequestMap(FacesContext facesContext, Map model) {
75         Iterator i = model.keySet().iterator();
76         while (i.hasNext()) {
77             String key = i.next().toString();
78             facesContext.getExternalContext().getRequestMap().put(key, model.get(key));
79         }
80     }
81     private FacesContext createFacesContext(HttpServletRequest request, HttpServletResponse response) {
82         FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
83         return facesContextFactory.getFacesContext(getServletContext(), request, response, facesLifecycle);
84     }
85
86     private Lifecycle createFacesLifecycle() {
87         LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
88         return lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
89     }
90
91     public static void notifyAfterListeners(PhaseId phaseId,
92             Lifecycle lifecycle, FacesContext context) {
93         PhaseEvent afterPhaseEvent = new PhaseEvent(context, phaseId, lifecycle);
94         for (int i = 0; i < lifecycle.getPhaseListeners().length; i++) {
95             PhaseListener listener = lifecycle.getPhaseListeners()[i];
96             if (listener.getPhaseId() == phaseId
97                     || listener.getPhaseId() == PhaseId.ANY_PHASE) {
98                 listener.afterPhase(afterPhaseEvent);
99             }
100         }
101     }
102
103     public static void notifyBeforeListeners(PhaseId phaseId,
104             Lifecycle lifecycle, FacesContext context) {
105         PhaseEvent beforePhaseEvent = new PhaseEvent(context, phaseId,
106                 lifecycle);
107         for (int i = 0; i < lifecycle.getPhaseListeners().length; i++) {
108             PhaseListener listener = lifecycle.getPhaseListeners()[i];
109             if (listener.getPhaseId() == phaseId
110                     || listener.getPhaseId() == PhaseId.ANY_PHASE) {
111                 listener.beforePhase(beforePhaseEvent);
112             }
113         }
114     }
115 }