protected TemplateModel createModel(ObjectWrapper wrapper) throws Throwable {
   HttpServletRequest request = super.getRequestContext().getRequest();
   HttpServletResponse response = super.getRequestContext().getResponse();
   ServletContext servletContext = request.getSession().getServletContext();
   AllHttpScopesHashModel hashModel =
       new AllHttpScopesHashModel(
           wrapper,
           servletContext,
           request); // super.createModel(wrapper, servletContext, request, response);
   ControllerServlet servlet = new ControllerServlet();
   MockServletConfig config = new MockServletConfig(servletContext);
   servlet.init(config);
   ServletContextHashModel newServletContextModel = new ServletContextHashModel(servlet, wrapper);
   ServletContextHashModel servletContextModel = new ServletContextHashModel(servlet, wrapper);
   servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
   TaglibFactory taglibs = new TaglibFactory(servletContext);
   servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
   hashModel.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION, newServletContextModel);
   hashModel.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION_PRIVATE, newServletContextModel);
   hashModel.putUnlistedModel(FreemarkerServlet.KEY_JSP_TAGLIBS, taglibs);
   HttpRequestHashModel requestModel = new HttpRequestHashModel(request, response, wrapper);
   request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
   hashModel.putUnlistedModel(FreemarkerServlet.KEY_REQUEST_PRIVATE, requestModel);
   return hashModel;
 }
예제 #2
0
  private static TemplateModel createModel(PageContext pageContext, Map<String, Object> map) {
    ObjectWrapper wrapper = ObjectWrapper.DEFAULT_WRAPPER;
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
    HttpServlet servlete =
        new ProxyServlet(pageContext.getServletConfig(), pageContext.getServletContext());
    ServletContextHashModel servletContextModel = new ServletContextHashModel(servlete, wrapper);
    ServletActionContext.getServletContext()
        .setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
    TaglibFactory taglibs = new TaglibFactory(ServletActionContext.getServletContext());
    ServletActionContext.getServletContext().setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
    AllHttpScopesHashModel params =
        new AllHttpScopesHashModel(wrapper, pageContext.getServletContext(), request);
    params.putUnlistedModel(KEY_APPLICATION, servletContextModel);
    params.putUnlistedModel(KEY_APPLICATION_PRIVATE, servletContextModel);
    params.putUnlistedModel(
        KEY_JSP_TAGLIBS,
        (TemplateModel)
            ServletActionContext.getServletContext().getAttribute(ATTR_JSP_TAGLIBS_MODEL));

    // Create hash model wrapper for session
    HttpSessionHashModel sessionModel;
    HttpSession session = request.getSession(false);
    sessionModel = (HttpSessionHashModel) session.getAttribute(ATTR_SESSION_MODEL);
    if (sessionModel == null) {
      sessionModel = new HttpSessionHashModel(session, wrapper);
      session.setAttribute(ATTR_SESSION_MODEL, sessionModel);
    }
    params.putUnlistedModel(KEY_SESSION, sessionModel);

    // Create hash model wrapper for request
    HttpRequestHashModel requestModel =
        (HttpRequestHashModel) request.getAttribute(ATTR_REQUEST_MODEL);
    if (requestModel == null || requestModel.getRequest() != request) {
      requestModel = new HttpRequestHashModel(request, response, wrapper);
      request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
      request.setAttribute(
          ATTR_REQUEST_PARAMETERS_MODEL, new HttpRequestParametersHashModel(request));
    }
    params.putUnlistedModel(KEY_REQUEST, requestModel);
    params.putUnlistedModel(KEY_INCLUDE, new IncludePage(request, response));
    params.putUnlistedModel(KEY_REQUEST_PRIVATE, requestModel);

    // Create hash model wrapper for request parameters
    HttpRequestParametersHashModel requestParametersModel =
        (HttpRequestParametersHashModel) request.getAttribute(ATTR_REQUEST_PARAMETERS_MODEL);
    params.putUnlistedModel(KEY_REQUEST_PARAMETERS, requestParametersModel);
    // 将用户数据放入model中
    params.putAll(map);
    return params;
  }