1 25 package org.ofbiz.webapp.view; 26 27 import java.io.IOException ; 28 import javax.servlet.RequestDispatcher ; 29 import javax.servlet.ServletContext ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.jsp.JspException ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.webapp.control.ContextFilter; 37 38 45 public class JspViewHandler implements ViewHandler { 46 47 public static final String module = JspViewHandler.class.getName(); 48 49 protected ServletContext context; 50 51 public void init(ServletContext context) throws ViewHandlerException { 52 this.context = context; 53 } 54 55 public void render(String name, String page, String contentType, String encoding, String info, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { 56 59 if (request == null) { 60 throw new ViewHandlerException("Null HttpServletRequest object"); 61 } 62 if (page == null || page.length() == 0) { 63 throw new ViewHandlerException("Null or empty source"); 64 } 65 66 69 request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean (true)); 71 RequestDispatcher rd = request.getRequestDispatcher(page); 72 73 if (rd == null) { 74 Debug.logInfo("HttpServletRequest.getRequestDispatcher() failed; trying ServletContext", module); 75 rd = context.getRequestDispatcher(page); 76 if (rd == null) { 77 Debug.logInfo("ServletContext.getRequestDispatcher() failed; trying ServletContext.getNamedDispatcher(\"jsp\")", module); 78 rd = context.getNamedDispatcher("jsp"); 79 if (rd == null) { 80 throw new ViewHandlerException("Source returned a null dispatcher (" + page + ")"); 81 } 82 } 83 } 84 85 try { 86 rd.include(request, response); 87 } catch (IOException ie) { 88 throw new ViewHandlerException("IO Error in view", ie); 89 } catch (ServletException e) { 90 Throwable throwable = e.getRootCause() != null ? e.getRootCause() : e; 91 92 if (throwable instanceof JspException ) { 93 JspException jspe = (JspException ) throwable; 94 95 throwable = jspe.getRootCause() != null ? jspe.getRootCause() : jspe; 96 } 97 Debug.logError(throwable, "ServletException rendering JSP view", module); 98 throw new ViewHandlerException(e.getMessage(), throwable); 99 } 100 } 101 } 102 | Popular Tags |