集成spring mvc freemarker整合

阅读更多
spring mvc freemarker整合现在已经是很流行的一种架构模式了,写这篇章目的是为了方便以后自己的配置,也希望能为刚学习freemarker的同学一点帮助,废话不多说直接贴上代码:
使用工具:eclipse+maven
整体结构
集成spring mvc freemarker整合_第1张图片

1、先导入jar包

 
    org.freemarker
    freemarker
    2.3.23



    org.springframework
    spring-context-support
    4.3.6.RELEASE


    org.springframework
    spring-beans
    4.3.6.RELEASE


    org.springframework
    spring-core
    4.3.6.RELEASE


    org.springframework
    spring-webmvc
    4.3.6.RELEASE


    commons-logging
    commons-logging
    1.2

其中我遇到一个错误java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS
原因就是freemarker版本太低了,Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory原因是少了spring-context-support,如果同样遇到的朋友需要注意一下。

2、配置applicationContext.xml
   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="   
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 
 
 
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
     
     
         
            0 
            UTF-8 
            0.########## 
            yyyy-MM-dd HH:mm:ss 
            true 
            ignore 
       
 
   
 
 



3、在WEB-INF配置springmvc-servlet.xml
   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="   
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
       



class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">


 
   

4、配置web.xml


  Freemaker


contextConfigLocation
classpath:applicationContext.xml


 
  org.springframework.web.context.ContextLoaderListener
 

 
  springmvc
  org.springframework.web.servlet.DispatcherServlet
  1
 

 
  springmvc
  /*
 



5、在view创建hello.ftl模板

"http://www.w3.org/TR/html4/loose.dtd"> 
 
     
         
        ${title} 
     
     
        ${content}
     


6、写一个控制器测试
package com.yejiu.spring.freemarker.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
@RequestMapping("/view")
public class HelloWorkController {

@RequestMapping("/hello")
public String hellowork(Model model){

model.addAttribute("title", "freemarker");
model.addAttribute("content", "welcome to freemarker");
return "hello";
}

}
到此一个freemarker的整合就完成了,就是那么容易
  • 集成spring mvc freemarker整合_第2张图片
  • 大小: 25.3 KB
  • 查看图片附件

你可能感兴趣的:(freemarker,spring,mvc)