Freemarker获取Session,Application,Request等对象

使用Request里的Attribute值最简单的方法就是直接 A t t r i b u t e N a m e {AttributeName}或者安全一点: {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地址:   r e q u e s t . g e t R e m o t e A d d r ( )   {request.getRemoteAddr()} 或者   {request.remoteAddr}
提交方式:   r e q u e s t . m e t h o d       R e q u e s t R e q u e s t a t t r i b u t e {request.method} 等等      Request: 用于获取Request对象中的attribute对象。 例如: {Request[“myRequestAttribute”]} 这样是直接在页面输出属性值。相当于request.getAtrribute(“myRequestAttribute”);
         如果要对这个值进行判断就必须使用如下格式:<#if Request[“myRequestAttribute”]=“edit”>
或者 :  R e q u e s t [ " m y R e q u e s t A t t r i b u t e " ] ! " d e f a u l t v a l u e "       S e s s i o n S e s s i o n a t t r i b u t e R e q u e s t   A p p l i c a t i o n A p p l i c a t i o n ( S e r v l e t C o n t e x t ) a t t r i b u t e R e q u e s t   R e q u e s t P a r a m e t e r s R e q u e s t p a r a m e t e r {Request["myRequestAttribute"]!"default value"}      Session:用于获取Session 对象中的attribute对象。 用法参照Request的用法。   Application:用于获取 Application(ServletContext)对象中的attribute对象。 用法参照Request的用法。   RequestParameters:用 于获取Request对象的parameter参数(浏览器端发送的请求数据) 例如: {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;


  1. 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>
    
  2. 在ftl文件中使用request对象,类型是HttpServletRequest:
    <#assign ctx=request.getContextPath()>

发布了32 篇原创文章 · 获赞 13 · 访问量 1519

猜你喜欢

转载自blog.csdn.net/weixin_38068605/article/details/103818473