【笔记】freemarker模板

freemarker依赖引入

    <dependency>
	    <groupId>org.freemarker</groupId>
	    <artifactId>freemarker</artifactId>
	    <version>2.3.21</version>
	</dependency>
2.3.19不会报错

首先,在spring mvc框架中配置

<!-- FreeMarker -->
    <bean
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"
        p:templateLoaderPath="/WEB-INF/flt"
        p:defaultEncoding="UTF-8">
        <property name="freemarkerSettings">
            <props>
                <prop key="classic_compatible">true</prop>
            </props>
        </property>
    </bean>
    <bean
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
        p:order="5"
        p:suffix=".ftl"
        p:contentType="text/html; charset=utf-8"/>

在/WEB-INF/下新建ftl文件. product.ftl

<#import "spring.ftl" as spring />
<html>
<head>
    <title>产品列表</title>
</head>

<body>
    产品列表
    <table>
        <#list productList as product>
            <tr>
                <td>
                    <a href="<@spring.url'/product/${product.logicId}.html'/>">${product.productName}</a>
                </td>
                <td>
                    ${product.productValue}
                </td>
            </tr>
        </#list>
    </table>
</body>

</html>

接下来Controller中跳转到这个模板页

@RequestMapping("/showProductListByFlt.html")
	public String showProductListByFlt(Product product,ModelMap model) throws Exception {
		// 查询产品列表逻辑略
		List<Product> productList = productService.findProList(product);
		model.addAttribute("productList",productList);
		return ("product");
	}
由于xml配置了freemarker视图解析器的优先级, 所以会先跳转到product的ftl文件
接着条产品信息页面模板

@RequestMapping(value="/{logicId}")
	public String showProduct(@PathVariable("logicId") String logicId,ModelMap model) throws Exception {
		Product obj = new Product();
		obj.setLogicId(logicId);
		Product product = productService.getProductByLogicId(obj);
		model.addAttribute("product",product);
		return ("productDetail");
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值