day12-FreeMarker

General overview

FreeMarker is a template engine written in Java language, which generates and outputs various files based on templates. It can not only be used as the realization technology of the presentation layer, but also can be used to generate XML, html or Java.

1.
Two beans are set in the Freemarker implementation process configuration file
a) The path and code of the template file to be read.
b) Type of output page (HTML), read suffix (.ftl) and output code

Back-end data returns to the page
a) Back-end controller method adds Model parameter
b) Fills in data with model
c) Returns the name string of the page, in fact, it does not care what string is returned. For example, the request path is http://item.pinyougou.com/149187842867973.html, this page is filled with data, and the URL is still this address.

The overall process: the
front-end requests http://item.pinyougou.com/149187842867973.html, enters the back-end and obtains the id behind the path, and sets a model parameter. The data is queried by id and saved in the model, and the data will be filled in the page when it returns to the page.
Note that there is no specific HTML file generated to form a static state. Here is the function just fill in the data according to the template

https://item.jd.com/5089253.html Request URL with html as the suffix
Pseudo-static: SEO optimization, improve website ranking
Pseudo-static: https://item.jd.com/5089253.html --> tomcat- -> item.ftl is
really static: https://item.jd.com/5089253.html --> nginx --> 5089253.html (home page)

Integration of Freemarker in the project (pseudo-static)

Here is the formation of pseudo-static

rely

pinyougou-item-web/pom.xml configuration:

<dependencies>
    <!-- freemarker -->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
</dependencies> 

Configuration file

pinyougou-item-web/src/main/resources/springmvc.xml:

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       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.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">

    <!-- 开启MVC注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 配置静态资源用WEB容器默认的servlet来处理 -->
    <mvc:default-servlet-handler/>

    
    
    <!-- FreeMarker配置开始 -->
    <!-- 配置FreeMarkerConfigurer -->
    <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <!-- 设置模板文件加载的基础路径 -->
        <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
        <!-- 设置模板文件的默认编码 -->
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    <!-- 配置FreeMarker视图解析器 -->
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <!-- 配置内容类型,指定模板文件的后缀名 这里是输出的文件的编码-->
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <!-- 配置模板文件后缀名 -->
        <property name="suffix" value=".ftl"/>
    </bean>
    <!-- FreeMarker配置结束 -->

    
    
    
    <!-- ############## 配置dubbo服务消费者 ############## -->
    <!-- 配置当前应用的名称 -->
    <dubbo:application name="pinyougou-item-web"/>
    <!-- 配置zookeeper作为注册中心,注册服务地址 -->
    <dubbo:registry protocol="zookeeper" address="192.168.12.131:2181"/>
    <!-- 配置采用包扫描来引用服务,产生服务接口的代理对象 -->
    <dubbo:annotation package="com.pinyougou.item.controller"/>
</beans>

ftl path:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-hMXP1DsT-1610073820093)(C:\Users\MT\AppData\Local\Temp\1600101732550.png)]

controller layer

Create ItemController.java under pinyougou-item-web/src/main/java/com.pinyougou.item.controller package:

Note that here is not returning json to return the page, so instead of using @RestController annotation, use @Controller annotation

@Controller
public class ItemController {
    
    
    @Reference(timeout=10000)
    private GoodsService goodsService;
/**
     * 根据主键id获取商品信息
     * http://item.pinyougou.com/ 149187842867973.html
     * 149187842867973  -->  SPU商品表的主键id
     */
    @GetMapping("/{goodsId}")
    public String getGoods(@PathVariable("goodsId")Long goodsId, 
Model model){
    
    
// 使用视图解析器不一样,那model就不一样,这里的视图解析器是使用FreeMarker
// jsp : model中数据最后放到request
        // ftl : model中数据就是FreeMarker的数据模型
        Map<String, Object> data = goodsService.getGoods(goodsId);
        model.addAllAttributes(data);
        return "item";
    }
}

Personal development

接上面代码
// 基础使用是以下方式,这样在页面中可以直接通过${name}获取到后台数据
model.addAttribute("name","admin");
// 如果是使用Map方式,也可以通过key直接获取,这样页面也是可以使用${age}获取到数据
data.put("age", "12");
model.addAllAttributes(data);

interface layer

pinyougou-interface/src/main/java/com.pinyougou.service/GoodsService.java increase method:

/** 获取商品信息 */
Map<String,Object> getGoods(Long goodsId);

Business realization layer

pinyougou-interface/src/main/java/com.pinyougou.service/GoodsService.java increase method:

/** 获取商品信息 */
public Map<String, Object> getGoods(Long goodsId){
    
    
   try{
    
    
      /** 定义数据模型 */
      Map<String, Object> dataModel = new HashMap<>();
      /** 加载商品SPU数据 */
      Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
      dataModel.put("goods", goods);
      /** 加载商品描述数据 */
      GoodsDesc goodsDesc =goodsDescMapper.selectByPrimaryKey(goodsId);
      dataModel.put("goodsDesc", goodsDesc);
      return dataModel;
   }catch (Exception ex){
    
    
      throw new RuntimeException(ex);
   }
}

apper.selectByPrimaryKey(goodsId);
dataModel.put(“goodsDesc”, goodsDesc);
return dataModel;
}catch (Exception ex){
throw new RuntimeException(ex);
}
}


Guess you like

Origin blog.csdn.net/qq_45850872/article/details/112346966