`
wx1569110409
  • 浏览: 20068 次
文章分类
社区版块
存档分类
最新评论

SpringMVC整合freemaker

 
阅读更多

首先需要导入相关jar包

其中: spring-context-support 包一定要导入 如果没有导入将出现以下错误

?

1
2
3
4
严重: Exception sending context initialized event to listener instance of  class  org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading  class  [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer]  for  bean with name  'freemarkerConfig'  defined in file [你的spring配置文件.xml]: problem with  class  file or dependent  class ; nested exception is java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory
 
Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory


添加freemaker需要的jar包有

spring-context-support.jar

freemarker.jar

其他包则在新建SpringMVC项目的时候就加载进来了


完成jar包的添加,下面则需要在web.xml中添加相关配置

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< web-app  version = "2.5" 
     xmlns = "http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     
   < welcome-file-list >
     < welcome-file >/index</ welcome-file >
   </ welcome-file-list >
   
   <!--  Spring 服务层的配置文件 --> 
   < context-param >
     < param-name >contextConfigLocation</ param-name >
     < param-value >classpath:config/spring*.xml</ param-value >
   </ context-param >
   
   <!--  Spring 容器启动监听器 -->  
   < listener >
     < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
   </ listener >
   
   
   < servlet >
     < servlet-name >springMVC</ servlet-name >
     < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
     < load-on-startup >1</ load-on-startup >
   </ servlet >
   
   <!--为DispatcherServlet建立映射 --> 
   < servlet-mapping >
     < servlet-name >springMVC</ servlet-name >
     < url-pattern >/</ url-pattern >
   </ servlet-mapping >
 
   
   < distributable />
</ web-app >

完成了web.xml的配置,下面则需要配置我们的 SpringMVC-servlet.xml

DispatcherServlet会根绝web.xml中配置的<servlet-name>去找<servlet-name>-servlet.xml的文件来加载spring的一些配置信息。我这里就应该取名叫springmvc-servlet.xml 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<? 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:aop = "http://www.springframework.org/schema/aop"
     xmlns:context = "http://www.springframework.org/schema/context"  xmlns:p = "http://www.springframework.org/schema/p"
     xmlns:tx = "http://www.springframework.org/schema/tx"
     xmlns:mvc = "http://www.springframework.org/schema/mvc"
     xmlns:ehcache = "http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring   
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
     
         <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射   请求映射-->  
     < bean  class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  >  
      
     < property  name = "messageConverters" >   
              < list >   
                  < bean  class = "org.springframework.http.converter.StringHttpMessageConverter">   
                     < property  name = "supportedMediaTypes">
                           < list >
                               < value >text/html;charset=UTF-8</ value >   
                          </ list >   
                     </ property >   
                  </ bean >   
              </ list >   
        </ property >  
     </ bean >  
         
         <!--对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> 
       < context:component-scan  base-package = "com.youto.controller" />
       
       < mvc:annotation-driven  />
       
       <!-- 静态文件目录 -->
       < mvc:resources  location = "/assets/"  mapping = "/assets/**"  />
       < mvc:interceptors >
           < mvc:interceptor >
             < mvc:mapping  path = "/manager/**" />              
             < bean  class = "com.youto.util.ManagerInterceptor"  />
           </ mvc:interceptor >
       </ mvc:interceptors >
       
       <!--以下三种视图配置根据需要任选一种即可 --> 
       
      <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
      <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
         <property name="suffix" value=".jsp"/>
      </bean>-->
      
      <!-- 针对freemarker的视图配置 -->  
     < bean  id = "viewResolver"  
         class = "org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" >  
         < property  name = "cache"  value = "true"  />  
         < property  name = "prefix"  value = ""  />  
         < property  name = "suffix"  value = ".ftl"  />  
         < property  name = "contentType"  value = "text/html;charset=UTF-8" ></ property >  
         < property  name = "requestContextAttribute"  value = "request"  />  
         < property  name = "exposeSpringMacroHelpers"  value = "true"  />  
         < property  name = "exposeRequestAttributes"  value = "true"  />  
         < property  name = "exposeSessionAttributes"  value = "true"  />  
     </ bean >
     
     <!-- View resolvers can also be configured with ResourceBundles or XML files.   
         If you need different view resolving based on Locale, you have to use the   
         resource bundle resolver. -->  
     <!-- 这个是针对返回视图还是json值的视图配置   来分别处理同步和异步请求 -->  
     <!--<bean  
             class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
             <property name="mediaTypes">  
                 <map>  
                     <entry key="html" value="text/html" />  
                     <entry key="json" value="application/json" />  
                 </map>  
             </property>  
             <property name="favorParameter" value="true" />  
             <property name="viewResolvers">  
                 <list>  
                     <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />  
                     <bean id="viewResolver"  
                         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
                         <property name="cache" value="true" />  
                         <property name="prefix" value="" />  
                         <property name="suffix" value=".ftl" />  
                         <property name="contentType" value="text/html;charset=UTF-8"></property>  
                         <property name="requestContextAttribute" value="request" />  
                         <property name="exposeSpringMacroHelpers" value="true" />  
                         <property name="exposeRequestAttributes" value="true" />  
                         <property name="exposeSessionAttributes" value="true" />  
                     </bean>  
                 </list>  
             </property>  
             <property name="defaultContentType" value="text/html" />  
         </bean>  
         -->  
      
</ beans >

如果是使用freemarker最为视图模板需要再spring的配置文件applicationContext.xml中加入以下配置  由于本人项目中Spring的配置文件叫 spring.xml 故我需要修改的是spring.xml

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<? 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:jee = "http://www.springframework.org/schema/jee"
     xmlns:tx = "http://www.springframework.org/schema/tx"
     xmlns:context = "http://www.springframework.org/schema/context"
     xmlns:aop = "http://www.springframework.org/schema/aop"
     xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
 
     < tx:annotation-driven  transaction-manager = "transactionManager" />
     < context:annotation-config ></ context:annotation-config >
     < context:component-scan  base-package = "com.youto" />
     
     <!-- 加载配置文件 -->
     < bean  id = "propertyConfigurer"
         class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
         < property  name = "locations" >
             < list >
                 < value >classpath:config/db.properties</ value >
             </ list >
         </ property >
     </ bean >
     
     <!-- freemaker配置 -->
     < bean  id = "freemarkerConfig"  
         class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >  
         < property  name = "templateLoaderPath"  value = "/WEB-INF/views/"  />  
         < property  name = "freemarkerSettings" >  
             < props >  
                 < prop  key = "template_update_delay" >0</ prop >  
                 < prop  key = "default_encoding" >UTF-8</ prop >  
                 < prop  key = "number_format" >0.##########</ prop >  
                 < prop  key = "datetime_format" >yyyy-MM-dd HH:mm:ss</ prop >  
                 < prop  key = "classic_compatible" >true</ prop >  
                 < prop  key = "template_exception_handler" >ignore</ prop >  
             </ props >  
         </ property >  
     </ bean >  
     
     
     <!-- 配置数据源 -->
     < bean  id = "dataSource"   class = "com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method = "close" >
         < property  name = "driverClass"  value = "com.mysql.jdbc.Driver"  />
         < property  name = "jdbcUrl" >
             < value > <![CDATA[jdbc:mysql://${db.host}:${db.port}/${db.database}?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true]]> </ value >
         </ property >
         < property  name = "user"                value = "${db.userName}"  />
         < property  name = "password"            value = "${db.password}"  />
         < property  name = "maxPoolSize"         value = "12"  />
         < property  name = "minPoolSize"         value = "0"  />
         < property  name = "maxStatements"       value = "100"  />
         < property  name = "initialPoolSize"     value = "3"  />
         < property  name = "maxIdleTime"         value = "10" />
         < property  name = "idleConnectionTestPeriod"    value = "10"  />
         < property  name = "testConnectionOnCheckin"     value = "true"  />
         < property  name = "testConnectionOnCheckout"    value = "false"  />
         < property  name = "preferredTestQuery"          value = "SELECT 1 FROM DUAL"  />
     </ bean >
 
     <!-- 配置读的 ibatis (从库)-->
     < bean  id = "sqlSessionFactory"  class = "org.mybatis.spring.SqlSessionFactoryBean" >
         < property  name = "dataSource"  ref = "dataSource"  />
         < property  name = "configLocation"  value = "classpath:config/sqlMapConfig.xml" />
     </ bean >
     
     
     < bean  id = "sqlSession"  class = "org.mybatis.spring.SqlSessionTemplate" >
         < constructor-arg  index = "0"  ref = "sqlSessionFactory"  />
     </ bean >    
     
      <!-- 事务控制 (主库)-->
     < bean  id = "transactionManager"  class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
         < property  name = "dataSource"  ref = "dataSource"  />
     </ bean >
     
</ beans >

注意

?

1
在 spring.xml中配置 freemaker的 templateLoaderPath 目录 在springMVC-servlet.xml中则不需要在配置 仅需要在 spring.xml中配置即可.

如果出现以下错误,即有可能是2个都配置了地址或者是配置的目录错误

?

1
Could not resolve view with name  'free'  in servlet with name  'springMVC'


以上 我们就完成了freemaker和springMVC的整合.下面是我们的测试 controller

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import  javax.servlet.http.HttpServletRequest;  
   
import  org.springframework.stereotype.Controller;  
import  org.springframework.web.bind.annotation.RequestMapping;  
import  org.springframework.web.bind.annotation.RequestMethod;  
import  org.springframework.web.servlet.ModelAndView;  
   
public  class  SpringMvcController {  
   
     @RequestMapping (value= "/welcome" ,method=RequestMethod.GET)   
     public  ModelAndView getFirstPage() {  
                 //welcom就是视图的名称(welcome.ftl)  
         ModelAndView mv =  new  ModelAndView(); 
         mv.setViewName( "welcome" ); 
         mv.addObject( "name" "this is freemaker test!!!" );  
         return  mv;  
     }  
}

welcome.ftl

?

1
2
3
4
5
6
7
8
9
10
<! DOCTYPE  html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
< html >  
< head >  
< meta  http-equiv = "Content-Type"  content = "text/html; charset=ISO-8859-1" >  
< title >Insert title here</ title >  
</ head >  
< body >  
Hello ${name}  
</ body >  
</ html >

运行我们的项目:输入 htp://localhost:8080/你的项目名/welcome  controller的地址在配置文件中配置过滤器

页面就会显示

Hello this is freemaker test!!!

转载于:https://my.oschina.net/Denniswang/blog/668415

分享到:
评论

相关推荐

    SpringMVC+Freemaker Demo

    通过这个"SpringMVC+Freemaker Demo",你可以深入了解这两种技术的结合使用,以及它们如何协同工作来构建一个完整的Web应用。实际操作过程中,可以进一步学习异常处理、国际化、缓存等高级话题,提升自己的开发能力...

    ext + spring Json view + springMVC + Freemaker

    "ext + spring Json view + springMVC + Freemaker"的组合提供了一种强大的解决方案,它整合了多种技术,以实现丰富的用户界面、灵活的数据处理和高效的视图渲染。下面我们将深入探讨这些技术及其相互作用。 1. **...

    基于注解的springmvc 整合freemarker的入门例子

    这是一个基于eclipse+springmvc+freemarker+注解的入门例子,是一个war包,import到eclipse就应该可以直接运行,但是确保maven环境到存在;我做为一个入门者,是参照了...

    freemarker与springmvc整合的例子

    **FreeMarker与SpringMVC整合基础** FreeMarker是一个强大的模板引擎,它被广泛应用于Web开发中,用于生成动态HTML或其他格式的文档。SpringMVC是Spring框架的一部分,它是一个轻量级的MVC(Model-View-Controller...

    IDEA+MAVEN+springMVC+mybatis+mySQL+freemaker

    【描述】"IDEA+MAVEN+springMVC+mybatis+mySQL+freemaker整合的框架" 提到的整合意味着这些组件被协同配置,以实现高效且可维护的Web应用开发。这种整合通常涉及以下步骤: 1. **IDEA配置**:在IntelliJ IDEA中,...

    spring mvc整合thymeleaf示例

    thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果。 thymeleaf是一个...

    SpringMVC+JPA+Freeemarker

    **整合SpringMVC、JPA和FreeMarker** 在本项目中,SpringMVC作为Web层的控制器,负责接收HTTP请求,调用业务逻辑,并返回结果。JPA则作为数据访问层,处理与数据库的交互,通过Spring的Data JPA模块,可以方便地创建...

    Spring4.0.2Hibernate4.3Freemarker2.3.20 bootstrap整合例子

    Spring4 SpringMVC4 Hibernate4 Freemarker Bootstrap3 整合的小DEMO,主要就是一个登录页面,加一个主页面,然后实现增删改查,还有分页的功能 说明:在项目的doc下有数据库文件,新建数据库名为 db_shf,编码设为...

    mvc_action:项目中的功能技术点,整合到SpringMVC的示例

    业余时间,整合以前项目技术,创建springMVC示例,目前升级到4.3.13版本,您的宝贵意见,是我们进步的动力。 项目说明 项目基于maven的多profile环境配置,打包时需要选择(test/pro/dev)打包运行的环境。 **项目特点*...

    api-master:api-master是springMVC项目,基于maven多模块和dubbo的rpc方式整合使用

    api-master是springMVC项目,基于maven多模块依赖,采用dubbo远程调用,目前spring升级到4.3.13版本,欢迎大家提出意见,您的宝贵意见,是我们进步的动力。 项目说明 项目基于maven的多profile环境配置,打包时需要...

    SpringMVC+Spring+Mybatis三大框架综合练习[MySQL/AJAX/IDEA]

    springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;SpringBoot---&gt;SpringCloud---&gt;Redis---&gt;MongoDB---&...

    基于SpringBoot+LayUI+Freemarker+Mybatis的通用后台管理系统源码.zip

    基于SpringBoot+LayUI+Freemarker+Mybatis的通用后台管理系统源码.zip 完整代码,可运行 。...SpringMVC+Spring+SpringBoot+LayUI+freemarker 运行环境 IDEA【或者Eclipse】 + Tomcat6以上 + Redis + MySQL5

    Spring MVC and FreeMarker Sample

    在提供的压缩包`Freemarker_SpringMVC_example`中,包含了完整的示例代码。通过阅读和运行这些代码,你可以更深入地理解Spring MVC与FreeMarker的整合过程,以及它们在实际项目中的应用。 总结来说,Spring MVC和...

    FreeMarker开发指南

    - **与WebWork整合**: FreeMarker可以与其他MVC框架如WebWork、SpringMVC等整合。 5. **高级方法** - **自定义方法**: 可以创建自定义的FreeMarker函数以扩展其功能。 - **自定义Transforms**: 自定义转换器...

    生产实习报告_李响1151

    1. **SSM框架整合**:SSM是Java Web开发中常用的三大框架组合,Spring提供了依赖注入和事务管理,SpringMVC负责处理HTTP请求和响应,Mybatis则用于数据库操作。通过整合这三大框架,可以实现高效、灵活的业务逻辑...

    beetl-core

    - 易于整合:Beetl可以很方便地与多种Web框架整合,如SpringMVC、JFinal、Struts、Nutz、Jodd、Servlet等。 - 支持模板单独开发和测试:即使在没有M(Model)和C(Controller)的情况下,也能够开发和测试模板。 - ...

    JAVA面试题[秒杀/Redis/MongoDB/SpringBoot/SpringCloud]

    ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;SpringBoot---&gt;SpringCloud---&gt;Redis---&gt;MongoDB---&gt;FreeMaker---&gt;...

    SVN和GIT详讲视频

    springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SpringBoot---&gt;SpringCloud---&gt;Redis---&gt;MongoDB---&gt;Freemaker---&gt;Nginx...

    正则表达式详讲

    springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;SpringBoot---&gt;SpringCloud---&gt;Redis---&gt;MongoDB---&...

    EasyUI框架详讲[jQuery]

    springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;SpringBoot---&gt;SpringCloud---&gt;Redis---&gt;MongoDB---&...

Global site tag (gtag.js) - Google Analytics