zoukankan      html  css  js  c++  java
  • SpringBoot集成FreeMarker

    给大家简单介绍一下springboot 集成FreeMarker
    过程很简单,5分钟即可。

    首先在项目中增添依赖spring-boot-starter-freemarker
    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.dalaoyang</groupId>
    	<artifactId>springboot_freemarker</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>springboot_freemarker</name>
    	<description>springboot_freemarker</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.10.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-freemarker</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-devtools</artifactId>
    			<scope>runtime</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    
    </project>
    

    然后创建controller,代码如下:

    package com.dalaoyang.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author dalaoyang
     * @Description
     * @project springboot_learn
     * @package com.dalaoyang.controller
     * @email 397600342@qq.com
     * @date 2018/3/14
     */
    @Controller
    public class TestController {
    
        @RequestMapping("/test")
        public String testFreemarker(ModelMap modelMap){
            modelMap.addAttribute("msg", "Hello dalaoyang , this is freemarker");
            return "freemarker";
        }
    }
    

    application.properties如下

    ##端口号
    server.port=8888
    
    #设定ftl文件路径
    spring.freemarker.template-loader-path=classpath:/templates
    #设定静态文件路径,js,css等
    spring.mvc.static-path-pattern=/static/**
    

    然后简单给大家介绍一下,目录结构

    然后贴上ftl文件的代码,一定注意,是ftl!!!!!
    写html文件是无法找到页面的。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title>FreeMarker</title>
    </head>
    <body>
    <h1>${msg}</h1>
    </body>
    </html>
    

    然后启动项目,访问http://localhost:8888/ 即可看到以下页面,

    源码下载 :大老杨码云

  • 相关阅读:
    make[1]: *** [/workopenwrt/trunk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/stamp/.tools_install_nnnnn] Error 2 make[1]: Leaving directory `/work/openwrt/trunk' make: *** [world]
    安卓端与开发板通信
    openwrt串口的使用
    .OpenWrt驱动程序Makefile的分析概述 、驱动程序代码参考、以及测试程序代码参考
    openwrt系统之字符设备驱动软件包加载、测试程序加载
    cc2530串口通信流程
    cc2530操作任务系统初始化分析
    总结OpenWrt系统基本操作方法
    Zigbee协议栈OSAL层API函数【转载】
    cc2530启动流程---广播发送数据
  • 原文地址:https://www.cnblogs.com/dalaoyang/p/8567386.html
Copyright © 2011-2022 走看看