Jfinal freemarker 使用el表达式 无法取的session中存的值

3 篇文章 0 订阅

在使用Freemarker作为View的时候,没有自动将session传送至页面使用,原因很简单,为了遵 循restful。如果在项目中使用了session,那么集群就不太方便了,需要想办法对session进行同步才可以。所以JFinal默认是不会去 生成session对象让开发者去使用的。       

1. 在JFinalConfig的继承类中添加了SessionInViewInterceptor

public void configInterceptor(Interceptors interceptor) {
        interceptor.add(new SessionInViewInterceptor());
    }

2、html页面el表达式取值方式:${session['userInfo'].username}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我提供以下代码示例: 1. 返回HTML源码 在JFinal中,我们可以使用render方法来返回HTML源码,示例代码如下: ``` public void index() { renderHtml("<h1>Hello World!</h1>"); } ``` 在上述示例中,我们使用renderHtml方法来返回HTML源码,方法中传入的参数为HTML代码。 2. JFinal Freemarker生成静态HTML完整例子 在JFinal中,我们可以使用Freemarker来生成静态HTML页面,示例代码如下: ``` public void index() { // 设置模板文件路径 String templatePath = "/WEB-INF/templates/index.ftl"; // 获数据 String data = "Hello World!"; // 设置输出文件路径 String outputPath = "/WEB-INF/output/index.html"; // 生成静态HTML页面 renderHtml(FreemarkerUtil.render(templatePath, data, outputPath)); } ``` 在上述示例中,我们使用了一个名为FreemarkerUtil的工具类,示例代码如下: ``` public class FreemarkerUtil { /** * 渲染Freemarker模板 * * @param templatePath 模板文件路径 * @param data 数据 * @param outputPath 输出文件路径 * @return 生成的HTML源码 */ public static String render(String templatePath, Object data, String outputPath) { try { // 获模板配置 Configuration config = new Configuration(Configuration.VERSION_2_3_30); config.setServletContextForTemplateLoading(JFinal.me().getServletContext(), "/"); config.setDefaultEncoding("UTF-8"); // 获模板 Template template = config.getTemplate(templatePath); // 渲染模板 StringWriter writer = new StringWriter(); template.process(data, writer); // 输出到文件 File outputFile = new File(JFinal.me().getServletContext().getRealPath(outputPath)); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } Writer fileWriter = new FileWriter(outputFile); template.process(data, fileWriter); fileWriter.flush(); fileWriter.close(); // 返回HTML源码 return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } } } ``` 在上述示例中,我们首先获了模板配置并设置了模板文件路径和默认编码,然后获了模板文件并渲染了数据。接着,我们将生成的HTML源码输出到文件,并返回HTML源码。 3. 支持调用两次render效果,返回JSON数据 在JFinal中,我们可以使用renderJson方法来返回JSON数据,示例代码如下: ``` public void index() { // 设置模板文件路径 String templatePath = "/WEB-INF/templates/index.ftl"; // 获数据 String data = "Hello World!"; // 设置输出文件路径 String outputPath = "/WEB-INF/output/index.html"; // 生成静态HTML页面 renderHtml(FreemarkerUtil.render(templatePath, data, outputPath)); // 返回JSON数据 renderJson("{\"status\":\"success\"}"); } ``` 在上述示例中,我们首先使用renderHtml方法返回HTML源码,然后使用renderJson方法返回JSON数据。 注意:在JFinal中,只能调用一次render方法来返回响应,如果需要返回多个响应,可以使用renderXXX方法来返回。另外,如果需要在返回JSON数据之前生成静态HTML页面,可以使用renderNull方法来返回空响应。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值