首先需要导入相关jar包
其中: spring-context-support 包一定要导入 如果没有导入将出现以下错误
严重: 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.0" encoding="UTF-8"?>
"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">
/index
contextConfigLocation
classpath:config/spring*.xml
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
1
springMVC
/
完成了web.xml的配置,下面则需要配置我们的 SpringMVC-servlet.xml
DispatcherServlet会根绝web.xml中配置的去找-servlet.xml的文件来加载spring的一些配置信息。我这里就应该取名叫springmvc-servlet.xml
"1.0" encoding="UTF-8"?>
"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">
"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
"messageConverters">
"org.springframework.http.converter.StringHttpMessageConverter">
"supportedMediaTypes">
text/html;charset=UTF-8
package="com.youto.controller"/>
"/assets/" mapping="/assets/**" />
"/manager/**"/>
"com.youto.util.ManagerInterceptor" />
"viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
"cache" value="true" />
"prefix" value="" />
"suffix" value=".ftl" />
"contentType" value="text/html;charset=UTF-8">
"requestContextAttribute" value="request" />
"exposeSpringMacroHelpers" value="true" />
"exposeRequestAttributes" value="true" />
"exposeSessionAttributes" value="true" />
如果是使用freemarker最为视图模板需要再spring的配置文件applicationContext.xml中加入以下配置 由于本人项目中Spring的配置文件叫 spring.xml 故我需要修改的是spring.xml
"1.0" encoding="UTF-8"?>
"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">
"transactionManager"/>
package="com.youto"/>
"propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
"locations">
classpath:config/db.properties
"freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
"templateLoaderPath" value="/WEB-INF/views/" />
"freemarkerSettings">
"template_update_delay">0
"default_encoding">UTF-8
"number_format">0.##########
"datetime_format">yyyy-MM-dd HH:mm:ss
"classic_compatible">true
"template_exception_handler">ignore
"dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
"driverClass" value="com.mysql.jdbc.Driver" />
"jdbcUrl">
//${db.host}:${db.port}/${db.database}?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true]]>
"user" value="${db.userName}" />
"password" value="${db.password}" />
"maxPoolSize" value="12" />
"minPoolSize" value="0" />
"maxStatements" value="100" />
"initialPoolSize" value="3" />
"maxIdleTime" value="10"/>
"idleConnectionTestPeriod" value="10" />
"testConnectionOnCheckin" value="true" />
"testConnectionOnCheckout" value="false" />
"preferredTestQuery" value="SELECT 1 FROM DUAL" />
"sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
"dataSource" ref="dataSource" />
"configLocation" value="classpath:config/sqlMapConfig.xml"/>
"sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
"0" ref="sqlSessionFactory" />
"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
"dataSource" ref="dataSource" />
注意
在 spring.xml中配置 freemaker的 templateLoaderPath 目录 在springMVC-servlet.xml中则不需要在配置 仅需要在 spring.xml中配置即可.
如果出现以下错误,即有可能是2个都配置了地址或者是配置的目录错误
Could not resolve view with name ‘free’ in servlet with name ‘springMVC’
以上 我们就完成了freemaker和springMVC的整合.下面是我们的测试 controller
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;
@Controller
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
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
"Content-Type" content="text/html; charset=ISO-8859-1">
Insert title here
Hello ${name}
运行我们的项目:输入 htp://localhost:8080/你的项目名/welcome controller的地址在配置文件中配置过滤器
页面就会显示
Hello this is freemaker test!!!
工程Demo下载地址
本文作为初学者的学习的记录,以便之后查阅。谢谢。
谢谢您的阅读,希望本站及文档能带给你帮助,给你带来简洁明了的阅读体验。谢谢。
本文地址:http://www.laileshuo.com/?p=867
博客地址:www.laileshuo.com www.laileshuo.cn