jfinal engine 在启动时从数据库查询并添加全局变量

思路:在JFinalConfig的实现类中配置插件的启动时机。

另一种:在 onStart 方法(3.6) 或者 afterJFinalStart 方法中获取当前web应用的模板引擎。

image.png

页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 #(globalName)
</body>
</html>

java代码

package com.jfinal.engine.demo;

import com.jfinal.core.Controller;

public class IndexController extends Controller {

    public void index(){
        render("test.html");
    }
}
package com.jfinal.engine.demo;

import com.jfinal.config.*;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

public class EngineDemoConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants me) {
        me.setConfigPluginOrder(1);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", IndexController.class, "/");
    }

    @Override
    public void configEngine(Engine me) {
        me.setBaseTemplatePath(null);
        me.setToClassPathSourceFactory();

        String globalName = Db.queryStr(" select name from p_test limit 1 ");

        me.addSharedObject("globalName", globalName);
    }

    @Override
    public void configPlugin(Plugins me) {
        DruidPlugin dp = new DruidPlugin("jdbc:mysql://localhost:3306/db0", "root", "123456");
        me.add(dp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        me.add(arp);
    }

    @Override
    public void configInterceptor(Interceptors me) {

    }

    @Override
    public void configHandler(Handlers me) {

    }
}
package com.jfinal.engine.demo;

import com.jfinal.server.undertow.UndertowServer;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        UndertowServer.start(EngineDemoConfig.class);
    }
}

maven pom

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jfinal.engine.demo</groupId>
    <artifactId>engine_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>engine_demo</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal</artifactId>
            <version>3.6</version>
        </dependency>

        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal-undertow</artifactId>
            <version>1.5</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
    </dependencies>

    <build>

    </build>
</project>


评论区

JFinal

2019-02-19 16:27

炉火纯清了,连 me.setConfigPluginOrder(1) 这么隐密的功能都被找到了,赞

zhangshiqiang

2019-02-19 20:24

@JFinal 关注每一版更新

JFinal

2019-02-19 21:45

@zhangshiqiang 回头策划将 jfinal 所有 change log 汇总分享出来,开源七年多,上千项增强与改进吓死人

絮随风

2019-02-20 10:43

热门分享

扫码入社