Java 无法访问freemarker模板中的会话属性
我有一个action类,它设置会话属性Java 无法访问freemarker模板中的会话属性,java,email,struts2,freemarker,Java,Email,Struts2,Freemarker,我有一个action类,它设置会话属性A和B。我知道值存在,并且它们不是null 在Freemarker模板中,我试图通过使用下面的表达式获得这些值 <#if session.A?exists> ${session.A} </#if> 现在sendEmail类包含freemarker配置 Configuration cfg = new Configuration(); cfg.setClassForTemplateLoading(SendEm
A
和B
。我知道值存在,并且它们不是null
在Freemarker模板中,我试图通过使用下面的表达式获得这些值
<#if session.A?exists>
${session.A}
</#if>
现在sendEmail类包含freemarker配置
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(SendEmail.class, "");
Template template = cfg.getTemplate("SendEmail.ftl");
Map<String,String> rootMap = new HashMap<>();
Writer out = new StringWriter();
try {
template.process(rootMap, out);
} catch (TemplateException | IOException templateException) {
logger.error("Freemarker Template processing exception", templateException);
}
body.setContent(out.toString(), "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(body);
message.setContent(multipart, "text/html");
Transport.send(message);
Configuration cfg=新配置();
cfg.setClassForTemplateLoading(sendmail.class,“”);
模板模板=cfg.getTemplate(“sendmail.ftl”);
Map rootMap=newhashmap();
Writer out=新的StringWriter();
试一试{
流程模板(rootMap,out);
}捕获(TemplateException | IOException TemplateException){
logger.error(“Freemarker模板处理异常”,templateException);
}
setContent(out.toString(),“text/html”);
Multipart Multipart=新的MimeMultipart();
多部件添加车身部件(车身);
message.setContent(多部分,“text/html”);
传输。发送(消息);
会话映射声明为
private Map<String, Object> sessionMap;
私有地图sessionMap;
变量名在Freemarker中区分大小写,会话存储在会话下(大写)。
因此,您的代码应该是:
<#if Session.A?exists>
${Session.A}
</#if>
freemarker中没有会话对象,您应该执行以下操作
<#assign session = stack.findValue('#session')/>
要执行的操作代码是
FreemarkerResult fr = new FreemarkerResult("SendEmail.ftl");
ActionContext.getContext().getContainer().inject(fr);
Writer out = new StringWriter();
fr.setWriter(out);
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("A", A);
session.put("B", B);
fr.execute(ActionContext.getContext().getActionInvocation());
FreemarkerResult fr=newfreemarkerresult(“sendmail.ftl”);
ActionContext.getContext().getContainer().inject(fr);
Writer out=新的StringWriter();
fr.setWriter(out);
映射会话=ActionContext.getContext().getSession();
会议。付诸表决(“A”,A);
会议.付诸表决(“B”,B);
fr.execute(ActionContext.getContext().getActionInvocation());
由于HttpSession
没有get
方法,因此您需要使用其中提供的一种方法来操作会话。使用getAttribute
方法获取会话值
${session.getAttribute('A')}
如果您在Struts2中使用FreeMarker模板,则上述操作将有效
在您的情况下,您没有将会话映射设置为模型。将rootMap
声明更改为Map
,并将会话映射放入其中
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put("session", sessionMap);
--freemarker.core.InvalidReferenceException:Expression会话在第39行未定义让我检查并返回它不工作,我知道会话中有值,在调用freemarker模板之前,我调用jsp并输出会话值,在那里我可以看到值,${A}${B} );还有,同样的,你如何使用ftl,你返回了什么样的结果类型。Poststruts.xml
。您也可以尝试${stack.findValue('#session.A')}
。我的结果类型不是FTL,而是jsp,我发送电子邮件,电子邮件模板是FTL。我将会话值存储在值堆栈中,然后希望访问FTL模板中的值。我有触发电子邮件类的操作类,在该电子邮件类中,我加载FTL模板并进行所需的处理,然后发送电子邮件。一切正常,只是在FTL模板中我没有得到值。但是值在配置的jsp结果类型中。发布代码您在做什么,代码应该是一个自包含的示例。您可以访问那里的值堆栈吗?没有Aleksandra,它不工作。表达式会话在第38行未定义有问题的指令:--------------==>${session.getAttribute('a')}[在第38行中的第32列
<#assign session = stack.findValue('#session')/>
FreemarkerResult fr = new FreemarkerResult("SendEmail.ftl");
ActionContext.getContext().getContainer().inject(fr);
Writer out = new StringWriter();
fr.setWriter(out);
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("A", A);
session.put("B", B);
fr.execute(ActionContext.getContext().getActionInvocation());
${session.getAttribute('A')}
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put("session", sessionMap);
${session['A']}