FreeMarker 获取页面request、session

使用Request里的Attribute值最简单的方法就是直接${AttributeName}或者安全一点:${AttributeName!"default Value"}

1.取Application范围的对象

xml 代码

   1. <#if Application.myApplicationAttribute?exists>  

   2.      ${Application.myApplicationAttribute}   

   3. </#if>  

或者 :  ${Application.myApplicationAttribute!"default value"}   

2.取session范围的对象

xml 代码

   1. <#if Session.mySessionAttribute?exists>  

   2.      ${Session.mySessionAttribute}   

   3. </#if>  

或者 :   ${Session.mySessionAttribute!"default value"}   

3.取request范围的对象

xml 代码

   1. <#if Request.myRequestAttribute?exists>  

   2.       ${Request.myRequestAttribute}   

   3. </#if>  

或者 :   ${Request.myRequestAttribute!"default value"}   

4.取request parameter范围的对象

xml 代码

   1. <#if Parameters.myParameter?exists>  

   2.      ${Parameters.myParameter}   

   3. </#if>  

或者 : ${Parameters.myParameter!"default value"}   

5.取context parameter范围的对象

xml 代码

   1. ${stack.findValue('#myContextParam')}  

 

 request 对象直接对应 HttpServletResponse

例如 获取 当前网页地址:  ${request.requestURL}

客户端IP地址:  ${request.getRemoteAddr()} 或者  ${request.remoteAddr}

提交方式:  ${request.method}

等等

 

 

 Request: 用于获取Request对象中的attribute对象。

例如:${Request["myRequestAttribute"]} 这样是直接在页面输出属性值。相当于request.getAtrribute("myRequestAttribute");

         如果要对这个值进行判断就必须使用如下格式:<#if Request["myRequestAttribute"]="edit">

或者 : ${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"]}

 

  1. ${Request["a"]}   
  2. ${RequestParameters["a"]}   
  3. ${Session ["a"]}   
  4. ${Application ["a"]}   
  5. ${JspTaglibs ["a"]}   
  6. 注意RequestParameters等内部对象时常不能获得所以要:   
  7. ${RequestParameters?default({})["_errors"]?default("params")}   
  8. ${Request["request"]?default("request")}   
  9. ${Session["session"]?default("session")}   
  10. ${Application["ctx"]?default("ctx")}   
  11. ${RequestParameters?default({})?keys?size}   
  12. ${Request?keys?first}   
  13. Freemarker可以直接取pageContext,requestAttribute,session,application中的数据,就是不能取requestParameter;  
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中生成静态页面使用Freemarker模板引擎可以按照以下步骤进行操作: 1. 首先,在pom.xml文件中添加Freemarker的依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 创建一个Controller类,用于处理请求并生成静态页面。例如,创建一个名为`StaticPageController`的类: ```java @Controller public class StaticPageController { @Autowired private Configuration freemarkerConfig; @GetMapping("/generate") public void generateStaticPage(HttpServletResponse response) throws IOException, TemplateException { Map<String, Object> data = new HashMap<>(); // 将需要的数据放入data中,供Freemarker模板使用 Template template = freemarkerConfig.getTemplate("static-page-template.ftl"); String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, data); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(html); } } ``` 3. 创建一个Freemarker的模板文件,例如,创建一个名为`static-page-template.ftl`的文件,并在该文件中编写静态页面的HTML代码。可以在模板中使用Freemarker的语法和表达式来动态生成页面内容。 4. 运行Spring Boot应用程序,并访问`/generate`路径,即可生成静态页面。 请注意,上述代码只是一个简单示例,你可以根据实际需求进行修改和优化。另外,还可以使用Freemarker的一些高级特性,如布局模板、片段等来提高开发效率和代码复用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值