springMVC与freemarker整合


准备好的环境:Maven工程整合好了ssm,即spring+springMVC+mybatis。接下来准备将springMVC与freemarker整合,以html文件为模板。

一,加入freemarker依赖

        
        
            org.freemarker
            freemarker
            2.3.23
        

二,在web.xml中的前端控制器选择加载mvc-context-freemarker.xml文件

    
    
        springServlet
        class>org.springframework.web.servlet.DispatcherServletclass>
        
            contextConfigLocation
            
          classpath*:/spring/mvc-context-freemarker.xml,
          classpath*:/spring/controller/*  
      
1

三,编辑mvc-context-freemarker.xm文件,注意标红部分。

<?xml version="1.0" encoding="UTF-8"?>

    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                    ">
    
    
    
    
        
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
     
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
     
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      0 zh_CN yyyy-MM-dd HH:mm:ss yyyy-MM-dd #.##
class="freemarker.template.utility.XmlEscape"/> class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

四,编辑Controller

    @RequestMapping("/doLogin")
    public String doLogin(HttpServletRequest req, HttpServletResponse res, Model model) {
         model.addAttribute("name", "czx"); 
         return "hello";
    }    

五,编辑Hello.html模板文件,注意存放路径问题

  
     
        freemarker Test  
    
      
        

Hello,${name}

结果: