如何获取FTL中的会话ID

问题描述:

我想要ftl页面中的当前会话ID。如何获取FTL中的会话ID

我试了很多东西与Session(HttpSessionHashModel)可用,但没有能够得到的ID?

获取请求标题也是我的工作。

有没有办法让我的FTL中的任何项目?

你可以把这个参数放到你的FTL文件中用java servlet。

我认为你的项目的工作是这样的:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 

     try 
     { 
      Map dataModel = new HashMap(); 

      dataModel.put("session_id", req.getSession().getId());     

      Configuration cfg = new Configuration(); 

      Template tmpl = cfg.getTemplate("test.ftl"); 

      tmpl.process(dataModel, resp.getWriter()); 
     } 
     catch (TemplateException ex) {    
      throw new IOException(ex); 
     } 

    } 

文件test.ftl为

<html> 
<head></head> 
<body> 
    <p> ${session_id!""} </p> 
</body> 
</html> 
+0

我知道这仅仅是一个例子,但是你仍然可以指出帽子的“配置”应该是单身,因此对于每个请求都不会被重新实例化。因为,人们喜欢复制解析... – ddekany

FreeMarker的不公开默认情况下任何东西。它甚至不知道什么是servlet。所以它取决于您正在使用的Web应用程序框架。 (除非,您正在使用freemarker.ext.servlet.FreeMarkerServlet。)或者,您可以直接将它们放入数据模型中。

+0

但我是用户freemarker servlet和HttpRequestHashModel可作为请求。我可以从中获取会话或请求标头吗? –

+0

不,“FreemarkerServlet”只公开会话属性。您可以扩展'FreemarkerServlet'并覆盖'createModel'以通过'AllHttpScopesHashModel.putUnlistedModel'将该会话ID添加到'super.createModel'的返回值中。 – ddekany