zoukankan      html  css  js  c++  java
  • springboot freemarker

    springboot可以通过简单的配置集成freemarker

    添加maven依赖

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.5.RELEASE</version>
        </parent>
        <artifactId>spring-freemarker</artifactId>
        <name>spring-freemarker</name>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
        </dependencies>
    </project>

    添加application.properties并添加freemarker相关配置

    spring.freemarker.enabled=true
    spring.freemarker.suffix=.ftl
    spring.mvc.static-path-pattern=/static/**

    查看其他的模板配置可以访问https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#templating-properties

    添加测试类

    @SpringBootApplication
    @Controller
    public class SpringFreemarkerApplication {
    
        @RequestMapping("/index")
        public String index() {
            return "index";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SpringFreemarkerApplication.class, args);
        }
    
    }

    创建src/main/java/resources/templates目录,添加index.ftl文件

    <head>
        <script type="text/javascript" src="/static/index.js"></script>
    </head>
    <body onload="windowOnLoad();">
        <h1>Hello World</h1>
        <div id="div"></div>
    </body>
    创建src/main/java/resources/static目录,添加index.js文件
    function windowOnLoad() {
        document.getElementById("div").innerHTML = "Hello From JS";
    }

    启动应用,访问http://localhost:8080/index可以看到如下内容:

     说明配置成功

  • 相关阅读:
    IE安全级别没法定制
    将应用部署到Websphere的context root根/
    SqlServer插入慢的问题解决
    SqlServer中用@@IDENTITY取最新ID不准的问题
    分享我的戒烟经验
    Telnet发邮件过程
    .net垃圾回收和CLR 4.0对垃圾回收所做的改进之三
    Please pay more attention to the character set of your database
    翻旧贴: 什么是对象?
    C# Code in ASPX Page
  • 原文地址:https://www.cnblogs.com/yytxdy/p/12817533.html
Copyright © 2011-2022 走看看