freemarker中判断空可以用??或者是!,感叹号后面最好跟一个变量,当值为null时会输出我们!后面的值例如
<!--问号用法-->
<#if(name??)>
name值不为空时会走这里,为空时直接不输出
</#if>
<!--感叹号用法,为了避免null报错,!后面要跟一个引号内容,该内容可以为空字符串也可以是一个值-->
<#if(name!'')>
name值不为空时会走这里
</#if>
为空时会输出引号里的值
项目搭建
配置文件applicationContext-freemarker.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: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-4.2.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-4.2.xsd">
<bean id="freeMarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="/WEB-INF/ftl"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>
后台代码
注意:我们在properties中定义的文件路径,在代码中用@Value("${pageSource}")注解来注入
package com.offcn.service.impl;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import com.offcn.mapper.TbGoodsDescMapper;
import com.offcn.mapper.TbGoodsMapper;
import com.offcn.mapper.TbItemCatMapper;
import com.offcn.mapper.TbItemMapper;
import com.offcn.pojo.TbGoods;
import com.offcn.pojo.TbGoodsDesc;
import com.offcn.pojo.TbItem;
import com.offcn.pojo.TbItemCat;
import com.offcn.pojo.TbItemExample;
import com.offcn.pojo.TbItemExample.Criteria;
import com.offcn.service.ItemCatService;
import com.offcn.service.ItemPageService;
import freemarker.template.Configuration;
import freemarker.template.Template;
@Service
public class ItemPageServiceImpl implements ItemPageService{
@Autowired
private TbGoodsMapper goodsMapper;
@Autowired
private TbGoodsDescMapper goodsDescMapper;
@Autowired
private FreeMarkerConfig freeMarkerConfig;
@Value("${pageSource}")
private String pageSource;
@Autowired
private TbItemCatMapper itemCatMapper;
@Autowired
private TbItemMapper itemMapper;
@Override
public boolean getItemPageHtml(long id) {
try {
// 根据id获取goods数据
TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
// 获取freemarker模板
Configuration configuration = freeMarkerConfig.getConfiguration();
Template template = configuration.getTemplate("item.ftl");
Map map = new HashMap();
map.put("goods",tbGoods);
// 根据id获取goodsdesc的数据
TbGoodsDesc goodsDesc = goodsDescMapper.selectByPrimaryKey(id);
map.put("goodsDesc", goodsDesc);
// 存放category1Id
String catName1 = itemCatMapper.selectByPrimaryKey(tbGoods.getCategory1Id()).getName();
String catName2 = itemCatMapper.selectByPrimaryKey(tbGoods.getCategory2Id()).getName();
String catName3 = itemCatMapper.selectByPrimaryKey(tbGoods.getCategory3Id()).getName();
map.put("category1", catName1);
map.put("category2", catName2);
map.put("category3", catName3);
// 保存价格
TbItemExample example = new TbItemExample();
Criteria criteria = example.createCriteria();
criteria.andGoodsIdEqualTo(id);
List<TbItem> items = itemMapper.selectByExample(example);
map.put("items", items);
// 生成html文件
File file = new File(pageSource+id+".html");
FileWriter fw = new FileWriter(file);
// 把数据添加到数据源
template.process(map, fw);
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
// 封装数据到FM的map中
return false;
}
}
在显示价格的时候,我们需要根据规格不同显示不同的价格,就需要拿到商品列表的信息到前台controller,但是数据源是和模板直接关联的,无法送到前端的controller中,这时候我们可以在模板中定义脚本初始化一个数据源,这时候前端的controller就可以通过$scope域拿到数据源了