Freemarker使用Cookie,Request,Session

Freemarker使用Cookie,Request,Session

1. 获取cookie.
<#assign cookies = request.getCookies()>
<#list cookies as cookie>
<#if cookie.name = "userName">
${cookie.name}
${cookie.value}
</#if>
</#list>

2. 获取request.
<#if Request["shoppingCart"]?exists>
<#assign shoppingCart = Request["shoppingCart"]>

3.session.
<#if Session["shoppingCart"]?exists>
<#assign shoppingCart = Session["shoppingCart"]>
action中:
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession().setAttribute("pathtu", filepath);
ftl页面:
${Session["pathtu"]?default("Session")}
读取堆栈:
<#assign code=((stack.findString("#parameters['code']"))?default('001'))/>

在后台给request,session设置值即可在ftl页面使用的,如:

public ModelAndView employeeItemKpi(HttpServletRequest request,String employeeName) {

request.setAttribute("name", "your value");

HttpSession session = request.getSession();
session.setAttribute("isAll", isAll);

}

下面介绍访问应用程序各范围属性的语法示例:
Application范围假定Application范围有一个属性 myApplicationAttribute :
java 代码
<#if Application.myApplicationAttribute?exists>

${Application.myApplicationAttribute}

或java 代码
<@s.property value="%{#application.myApplicationAttribute}" />

Session范围假定会话范围内有一个属性mySessionAttribute:
java 代码

<#if Session.mySessionAttribute?exists>

${Session.mySessionAttribute}

或java 代码

<@s.property value="%{#session.mySessionAttribute}" />

Request范围假定请求范围有一个属性myRequestAttribute
java 代码

<#if Request.myRequestAttribute?exists>

${Request.myRequestAttribute}


java 代码
<@s.property value="%{#request.myRequestAttribute}" />
Request参数假定请求参数myParameter
java 代码
<#if Parameters.myParameter?exists>

${Parameters.myParameter}


or
java 代码
<@s.property value="%{#parameters.myParameter}" />
Context参数假定框架上下文有一参数myContextParam
java 代码
${stack.findValue('#myContextParam')}

java 代码

<@s.property value="%{#myContextParam}" />

你可能感兴趣的:(Freemarker使用Cookie,Request,Session)