spring mvc 配置 velocity 模板
2015-06-15 15:57
351 查看
1、必要jar包
1.1 spring的jar包
除了必要的spring核心jar包 和 spring mvc jar包, web jar包特别注意:还要,spring-context-support 这个jar包
[code]<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-framework.version}</version> </dependency>
1.2 velocity的jar包
这个jar包是必须的[code]<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency>
有时候也要用到这个jar包
[code]<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency>
2、xxx-mvc.xml配置文件增加velocity环境和velocity视图解析器
这里是引用spring官方文档的[code]<!-- This bean sets up the Velocity environment for us based on a root path for templates. Optionally, a properties file can be specified for more control over the Velocity environment, but the defaults are pretty sane for file based template loading. --> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/> </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. --> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".vm"/> </bean>
3、查错&问题???
刚开始,没有引用spring-context-surport jar包,发生如下错误:[code]Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.web.servlet.view.velocity.VelocityConfigurer] for bean with name 'velocityConfig' defined in class path resource [mvc-config.xml]: problem with class file or dependent class; nested exception is **java.lang.NoClassDefFoundError: org/springframework/ui/velocity/VelocityEngineFactory** at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) **at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)** at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1241) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154)
但是 org.apache.velocity 的jar包我已经导入,而且工程中 类 VelocityEngineFactory 也是存在的。
往下看错误内容:
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
类 AbstractApplicationContext 是在 spring-context jar包中的
所以这个是一个抽象类,但是我查看了一下它的实现类,没有一个是在 spring-context-support包中的
所以,从错误日志中,我还是不知道, 这个报错为什么要引入 spring-context-support 跟这个jar包有什么关系?
相关文章推荐
- Eclipse中,字体修改及背景色修改
- 解决Myeclipse部署错误:Deploy Location:INVALID
- myeclipse恢复各视图大小
- JAVA多线程断点续传下载
- Mac下Eclipse连接不上Android手机的解决方法
- javaDoc
- spring 使用注解注入bean
- eclipse开发java say hello world
- java 泛型的几点备忘
- Java GC
- 重踏学习Java路上_Day04(java 基础上,选择结构,循环结构,转跳)
- java 注解
- 30 Java Programming Tips and Best Practices for Beginners
- #笔记#圣思园 JavaSE 第73讲——异常 Exception,自定义异常
- 在eclipse导入Extjs4.0项目卡死解决方案
- Java——(九)IO流
- 【Java技术点】Java屏幕截取
- 【j2ee spring】33、巴巴运动网的产品类别展示
- java 慎用ArrayList的contains方法,使用HashSet的contains方法代替
- 用maven+springMVC创建一个项目