美文网首页
Freemarker页面取session

Freemarker页面取session

作者: 王兴岭 | 来源:发表于2020-07-06 19:52 被阅读0次

springmvc页面模板引擎使用的是FreeMarkerView
当渲染页面会调用FreeMarkerView.doRender方法

    protected void doRender(Map<String, Object> model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        // Expose model to JSP tags (as request attributes).
        exposeModelAsRequestAttributes(model, request);
        // Expose all standard FreeMarker hash models.
        SimpleHash fmModel = buildTemplateModel(model, request, response);

        // Grab the locale-specific version of the template.
        Locale locale = RequestContextUtils.getLocale(request);
        processTemplate(getTemplate(locale), fmModel, response);
    }

buildTemplateModel方法很关键,这个方法是导出变量,然后页面上就可以使用了

    protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request,
            HttpServletResponse response) {

        AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
        fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
        fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
        fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
        fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
        fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
        fmModel.putAll(model);
        return fmModel;
    }

页面上使用

<span class="nickname">${Session.session_user.phone}</span>

相关文章

网友评论

      本文标题:Freemarker页面取session

      本文链接:https://www.haomeiwen.com/subject/xtkgqktx.html