Freemarker(下)

简介: Freemarker(下)

4. Freemarker整合spring

4.1. 创建整合spring的配置文件


<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="freemarkerSettings">
            <!-- 设置默认的编码方式,原先是GBK,需要设置成utf-8 -->
            <props>
                    <!--用于解决前端报空指针问题 不用再空值后面+ !号-->
                <prop key="classic_compatible">true</prop>
               <!--  <prop key="defaultEncoding">utf-8</prop>
                <prop key="template_exception_handler">rethrow</prop> -->
            </props>
        </property>
    </bean>
</beans>


需要编写一Controller进行测试


4.2 Controller

请求的url:/genhtml

参数:无

返回值:ok (String, 需要使用@ResponseBody)

业务逻辑:

1、从spring容器中获得FreeMarkerConfigurer对象。

2、从FreeMarkerConfigurer对象中获得Configuration对象。

3、使用Configuration对象获得Template对象。

4、创建数据集

5、创建输出文件的Writer对象。

6、调用模板对象的process方法,生成文件。

7、关闭流。


加载配置文件:


image.png


@Controller
public class HtmlGenController {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @RequestMapping("/genhtml")
    @ResponseBody
    public String genHtml()throws Exception {
        // 1、从spring容器中获得FreeMarkerConfigurer对象。
        // 2、从FreeMarkerConfigurer对象中获得Configuration对象。
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        // 3、使用Configuration对象获得Template对象。
        Template template = configuration.getTemplate("hello.ftl");
        // 4、创建数据集
        Map dataModel = new HashMap<>();
        dataModel.put("hello", "1000");
        // 5、创建输出文件的Writer对象。
        Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));
        // 6、调用模板对象的process方法,生成文件。
        template.process(dataModel, out);
        // 7、关闭流。
        out.close();
        return "OK";
    }
}


5. 商品详情页面静态化

5.1 网页的静态化方案

输出文件的名称:商品id+“.html”

输出文件的路径:工程外部的任意目录。

网页访问:使用nginx访问网页。在此方案下tomcat只有一个作用就是生成静态页面。

工程部署:可以把taotao-item-web部署到多个服务器上。

生成静态页面的时机:商品添加后,生成静态页面。可以使用Activemq,订阅topic(商品添加)



微信图片_20220127194014.png

微信图片_20220127194014.png


3.5.2. MessageListener

需要实现MessageListener,把Active的客户端jar包的依赖添加到工程中。

业务逻辑:

1、创建一个MessageListener接口的实现类

2、从message中取商品id

3、查询商品基本消息、商品描述。

4、创建商品详情页面的模板。

5、指定文件输出目录

6、生成静态文件。

安装http服务器。


public class HtmlGenListener implements MessageListener {
    @Autowired
    private ItemService itemService;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${HTML_OUT_PATH}")
    private String HTML_OUT_PATH;
    @Override
    public void onMessage(Message message) {
        try {
            // 1、创建一个MessageListener接口的实现类
            // 2、从message中取商品id
            TextMessage textMessage = (TextMessage) message;
            String strItemId = textMessage.getText();
            Long itemId = new Long(strItemId);
            // 3、查询商品基本消息、商品描述。
            TbItem tbItem = itemService.getItemById(itemId);
            Item item = new Item(tbItem);
            TbItemDesc tbItemDesc = itemService.getItemDescById(itemId);
            //创建数据集
            Map data = new HashMap<>();
            data.put("item", item);
            data.put("itemDesc", tbItemDesc);
            // 4、创建商品详情页面的模板。
            // 5、指定文件输出目录
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            Template template = configuration.getTemplate("item.htm");
            FileWriter out = new FileWriter(new File(HTML_OUT_PATH + itemId + ".html"));
            // 6、生成静态文件。
            template.process(data, out);
            //关闭流
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


3.5.3. Spring配置文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.25.168:61616" />
    </bean>
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.SingleConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
        <property name="targetConnectionFactory" ref="targetConnectionFactory" />
    </bean>
    <!--这个是主题目的地,一对多的 -->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="item-add-topic" />
    </bean>
    <!-- 配置消息监听器 -->
    <bean id="htmlGenListener" class="com.taotao.item.listener.HtmlGenListener"/>
    <!-- 配置监听容器 -->
    <!-- 消息监听容器 -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicDestination" />
        <property name="messageListener" ref="htmlGenListener" />
    </bean>
</beans>
目录
相关文章
|
1月前
|
XML Java 数据格式
freemarker使用总结
freemarker使用总结
|
1月前
|
XML Java 数据格式
freemarker
freemarker
|
1月前
|
XML 数据格式
SpringMVC中使用FreeMarker实践
SpringMVC中使用FreeMarker实践
36 0
freemarker+yml介绍 以及freemarker与JSP的区别
1. freemarker介绍 2. freemarker使用步骤   2.1 在pom.xml引入freeMarker的依赖包   2.2 在springboot中添加freemarker配置   2.3 编写模板文件*.ftl(当做jsp使用即可)​编辑   2.4 访问控制器后进行页面跳转       3. freemarker常用语法   3.1 取值    3.2 条件    3.3 循环    3.4 include    3.5 局部变量(assign)/全局变量(global) 4. yml的介绍 5."freemarker"和"jsp"的区区别和联系有
|
前端开发
Freemarker - 基础篇(下)
Freemarker - 基础篇(下)
171 0
Freemarker - 基础篇(下)
|
前端开发 Java
Freemarker - 基础篇(上)
Freemarker - 基础篇(上)
317 0
Freemarker - 基础篇(上)
|
Web App开发 安全 Java
一篇很全面的freemarker教程
FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:格式部分,不会输出 3,插值:即${...}或#{..
1777 0
|
XML Java Maven
Freemarker(上)
Freemarker(上)
146 0
Freemarker(上)
|
Java Maven