freemarker的应用

来源:互联网 发布:rpc java 一般 编辑:程序博客网 时间:2024/06/06 08:25

freemarker是一款用来生成输出文本的模版引擎。

下面介绍freemarker融入java项目的案例,是一个电商项目,生成的是商品单品页。

首先,创建商品单品页的实现类及其接口:

public interface StaticPageService {
public void productIndex(Map<String,Object> root,Integer id);
}

<bean id="staticPageService" class="cn.itcast.core.service.staticpage.StaticPageServiceImpl">
      <property name="freeMarkerConfigurer">
      <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

//设置模版目录
      <property name="templateLoaderPath" value="/WEB-INF/ftl/"></property>

//设置读取模版的编码
      <property name="defaultEncoding" value="UTF-8"></property>
      </bean>
      </property>
      </bean>

//继承ServletContextAware ,用来获取项目路径

public class StaticPageServiceImpl implements StaticPageService,ServletContextAware {
//springmvc提供了对freemarker的支持
private Configuration conf;

//在配置文件中注入freeMarkerConfigurer 对应的类org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {

//直接在set方法中获得conf

this.conf = freeMarkerConfigurer.getConfiguration();
}

public void productIndex(Map<String,Object> root,Integer id) {
Writer out = null;
try {

//获取模版对象
Template template = conf.getTemplate("productDetail.html");

//设置写入的文件路径
String path = getPath("/html/product/"+id+".html");
File f = new File(path);
File parentFile = f.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}

//写入的流及编码
out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");

template.process(root, out);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
public String getPath(String name){

//获取项目路径
return servletContext.getRealPath(name);
}
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}

springmvc对freemarker提供了支持,web.xml对过滤器编码的设置只对request和response请求起作用,freemarker的读写不属于request和response,所以freemarker的读写都必须设置编码UTF-8,模版也要设置编码为UTF-8,如果模版不是jsp文件,必须显式写上编码格式,比如HTML文件:<meta http-equiv="content-type" content="text/html; charset=UTF-8">


附controller代码

public String isShow(Integer[] ids,Integer pageNo,String name,Integer brandId,Integer changeShow,HttpServletRequest request,ModelMap model){
Product product = new Product();
String basePath = request.getContextPath();
product.setIsShow(1);
if(null != ids && ids.length > 0){
for(Integer id : ids){
Map<String,Object> root = new HashMap<String, Object>();
product.setId(id);
productService.updateProductByKey(product);

Product p = productService.getProductByKey(id);
root.put("product", p);

List<Sku> skus = skuService.getStock(id);
root.put("skus", skus);

List<Color> colors = new ArrayList<Color>();
for(Sku sku:skus){
if(!colors.contains(sku.getColor())){
colors.add(sku.getColor());
}
}
root.put("colors", colors);
root.put("basePath", basePath);
//TODO  静态化
staticPageService.productIndex(root, id);
}
}
if(null != pageNo){
model.addAttribute("pageNo", pageNo);
}
if(StringUtils.isNotBlank(name)){
model.addAttribute("name", name);
}
if(null != brandId){
model.addAttribute("brandId", brandId);
}
if(null != changeShow){
model.addAttribute("isShow", changeShow);
}

return "redirect:/product/list.do";
}

0 0
原创粉丝点击