Spring MVC整合FreeMarker的示例

什么是Freemarker?

    FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。
    目前企业中:主要用Freemarker做静态页面或是页面展示

一.工程结构

Spring MVC整合FreeMarker的示例_第1张图片

二.web.xml




  SpringMVC

  
    contextConfigLocation
    /WEB-INF/springMVC-servlet.xml
  

  
    org.springframework.web.context.ContextLoaderListener
  

  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    encodingFilter
    /*
  

  
    springMVC
    org.springframework.web.servlet.DispatcherServlet
    1
  
  
    springMVC
    /
  

三.springMVC-servlet.xml



  
  

  
  

  
  
    
    
    
    
  

  
  
    
    
    
    
    
    
    
    
  

  
  
    
    
      
        
      
    
    
    
      
        3600
        zh_CN
        yyyy-MM-dd HH:mm:ss
        yyyy-MM-dd
        #.##
      
    
  

  

      在JSP和Freemarker的配置项中都有一个order property,上面例子是把freemarker的order设置为0,jsp为1,意思是找view时,先找ftl文件,再找jsp文件做为视图。这样Freemarker视图解析器就能与JSP视图解析器并存。

四.FreeMarkerController.java

package com.bijian.study.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.bijian.study.utils.JsonUtil;
import com.bijian.study.vo.User;

@Controller
public class FreeMarkerController {

  @RequestMapping("/get/usersInfo")
  public ModelAndView Add(HttpServletRequest request, HttpServletResponse response) {

    User user = new User();
    user.setUsername("zhangsan");
    user.setPassword("1234");

    User user2 = new User();
    user2.setUsername("lisi");
    user2.setPassword("123");

    List users = new ArrayList();
    users.add(user);
    users.add(user2);
    return new ModelAndView("usersInfo", "users", users);
  }

  @RequestMapping("/get/allUsers")
  public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {

    List users = new ArrayList();
    User u1 = new User();
    u1.setUsername("王五");
    u1.setPassword("123");
    users.add(u1);

    User u2 = new User();
    u2.setUsername("张三");
    u2.setPassword("2345");
    users.add(u2);

    User u3 = new User();
    u3.setPassword("fgh");
    u3.setUsername("李四");
    users.add(u3);

    Map rootMap = new HashMap();
    rootMap.put("userList", users);
    Map product = new HashMap();
    rootMap.put("lastProduct", product);
    product.put("url", "http://www.baidu.com");
    product.put("name", "green hose");

    String result = JSON.toJSONString(rootMap);

    Map resultMap = new HashMap();
    resultMap = JsonUtil.getMapFromJson(result);

    return new ModelAndView("allUsers", "resultMap", resultMap);
  }
}

五.JsonUtil.java

package com.bijian.study.utils;

import java.util.Map;

import com.alibaba.fastjson.JSON;

public class JsonUtil {

  public static Map getMapFromJson(String jsonString) {
    if (checkStringIsEmpty(jsonString)) {
      return null;
    }
    return JSON.parseObject(jsonString);
  }

  /**
   * 检查字符串是否为空
   * @param str
   * @return
   */
  private static boolean checkStringIsEmpty(String str) {
    if (str == null || str.trim().equals("") || str.equalsIgnoreCase("null")) {
      return true;
    }
    return false;
  }
}

六.User.java

ackage com.bijian.study.vo;

public class User {

  private String username;
  private String password;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}

七.usersInfo.ftl





usersInfo


<#list users as user>
  
username : ${user.username}, password : ${user.password}

八.allUsers.ftl


 
  allUsers
 
 
  <#list resultMap.userList as user>
    Welcome ${user.username}!  id:${user.password}

Our latest product: ${resultMap.lastProduct.name} !

九.运行效果

Spring MVC整合FreeMarker的示例_第2张图片

Spring MVC整合FreeMarker的示例_第3张图片

再输入http://localhost:8088/SpringMVC/greeting?name=zhangshan,JSP视图解析器运行依然正常。

Spring MVC整合FreeMarker的示例_第4张图片

至此,就结束完成整合了!

以上就是Spring MVC整合FreeMarker的示例的详细内容,更多关于Spring MVC整合FreeMarker的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(Spring MVC整合FreeMarker的示例)