SSM小项目(会议管理系统)三整合freemarker(图解超详细)

这是上一篇博客的地址:https://blog.csdn.net/qq_41115379/article/details/114292192
下一篇博客的地址:https://blog.csdn.net/qq_41115379/article/details/114334194
开始整合freemarker
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具,用来生成HTML web页面

先加依赖:

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>

再resources目录下创建freemarker-var.properties
简单写一个这个:

root=/

并在spring-servlet.xml 进行三步,
1.引入properties文件 2.配置模板的基本属性 3.视觉解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="org.javaboy.meeting" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <mvc:annotation-driven/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:freemarker-var.properties</value>
            </list>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="freemarkerVariables">
            <map>
                <entry key="root" value="${root}"/>
            </map>
        </property>
        
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">10</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="time_format">HH:mm:ss</prop>
                <prop key="number_format">#.####</prop>
            </props>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
        <property name="suffix" value=".ftl"/>
        <property name="allowRequestOverride" value="true"/>
        <property name="allowSessionOverride" value="true"/>
        <property name="exposeSessionAttributes" value="true"/>
        <property name="exposeRequestAttributes" value="true"/>
        <property name="contentType" value="text/html;charset=utf-8"/>
    </bean>
</beans>

在WEB-INF下创建ftl
再通过重命名将he.html 改成he.ftl
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>${hello}</h1>
</body>
</html>

并修改hellocontroller
先把RestController 改成 controller
https://blog.csdn.net/lhq186/article/details/87894440 这是两者的区别

package org.javaboy.meeting.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {
    
    
    @GetMapping("/123")
    public String hello(Model model){
    
    
        model.addAttribute("hello","hellojava");
        return "he";
    }
}

我把这些都区分一下,就可以分清楚哪些是一一对应的了
运行访问:http://localhost:8080/123
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41115379/article/details/114292502