使用Request里的Attribute值最简单的方法就是直接 {AttributeName!“default Value”}
1.取Application范围的对象
xml 代码
1. <#if Application.myApplicationAttribute?exists>
2. KaTeX parse error: Expected 'EOF', got '#' at position 49: …te} 3. </#̲if> 或者 : {Application.myApplicationAttribute!“default value”}
2.取session范围的对象
xml 代码
1. <#if Session.mySessionAttribute?exists>
2. KaTeX parse error: Expected 'EOF', got '#' at position 41: …te} 3. </#̲if> 或者 : {Session.mySessionAttribute!“default value”}
3.取request范围的对象
xml 代码
1. <#if Request.myRequestAttribute?exists>
2. KaTeX parse error: Expected 'EOF', got '#' at position 41: …te} 3. </#̲if> 或者 : {Request.myRequestAttribute!“default value”}
4.取request parameter范围的对象
xml 代码
1. <#if Parameters.myParameter?exists>
2. KaTeX parse error: Expected 'EOF', got '#' at position 37: …er} 3. </#̲if> 或者 : {Parameters.myParameter!“default value”}
5.取context parameter范围的对象
xml 代码
1. KaTeX parse error: Expected '}', got '#' at position 19: …ack.findValue('#̲myContextParam'…{request.requestURL}
客户端IP地址:
{request.remoteAddr}
提交方式:
{Request[“myRequestAttribute”]} 这样是直接在页面输出属性值。相当于request.getAtrribute(“myRequestAttribute”);
如果要对这个值进行判断就必须使用如下格式:<#if Request[“myRequestAttribute”]=“edit”>
或者 :
{RequestParameters[“myRequestAttribute”]}等同于 request.getParameter(“myRequestAttribute”);
Parameters:属性获取,依次从 RequestParameters、Request、Session、Application对象中获取对应属性/参数,一旦获取,则不再向下查找。
例如:${Parameters[“myRequestAttribute”]}
${Request[“a”]}
${RequestParameters[“a”]}
${Session [“a”]}
${Application [“a”]}
${JspTaglibs [“a”]}
注意RequestParameters等内部对象时常不能获得所以要:
${RequestParameters?default({})["_errors"]?default(“params”)}
${Request[“request”]?default(“request”)}
${Session[“session”]?default(“session”)}
${Application[“ctx”]?default(“ctx”)}
${RequestParameters?default({})?keys?size}
${Request?keys?first}
Freemarker可以直接取pageContext,requestAttribute,session,application中的数据,就是不能取requestParameter;
-
FreemarkerViewResolver中设定requestContextAttribute属性
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html;charset=utf-8"/> <property name="cache" value="true"/> <property name="suffix" value=".ftl"/> <property name="order" value="0"/> <property name="requestContextAttribute" value="request"/> </bean>
-
在ftl文件中使用request对象,类型是HttpServletRequest:
<#assign ctx=request.getContextPath()>