首先我们需要进行SpringMVC的配置,这里大家可以参考我的另一篇博客:通过Java文件来配置SpringMVC
我们这次的项目也是在之前那篇博客项目的基础上进行添加的。
我们的集成可以分成以下几个部分:
(1)导入velocity依赖的jar包(主要是pom.xml)
(2)在之前的WebConfig文件中,添加我们的velocity的视图配置
(3)配置velocity.properties文件(velocity的配置文件)
一、导入velocity依赖的jar包
这是我们直接通过界面添加的依赖包,如果大家想在pom.xml文件下直接配置,也是可以的,主要是添加以下的dependency
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-velocity</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
二、velocity视图配置
该部分对应的WebConfig文件内容为如下:
package indi.tangkai.orderlist.config; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.velocity.VelocityConfigurer; import org.springframework.web.servlet.view.velocity.VelocityToolboxView; import org.springframework.web.servlet.view.velocity.VelocityViewResolver; @Configuration //启用MVC @EnableWebMvc //启用组建扫描 @ComponentScan("indi.tangkai.orderlist.controller") @SuppressWarnings("deprecation") public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware { private ApplicationContext context; @Bean public ViewResolver viewResolver(){
// InternalResourceViewResolver resolver = new InternalResourceViewResolver();// resolver.setPrefix("/WEB-INF/views/");// resolver.setSuffix(".jsp");// resolver.setExposeContextBeansAsAttributes(true);// return resolver;VelocityViewResolver viewResolver=new VelocityViewResolver();viewResolver.setViewClass(VelocityToolboxView.class);viewResolver.setPrefix("/");viewResolver.setSuffix(".vm");viewResolver.setCache(true);viewResolver.setContentType("text/html;charset=UTF-8");viewResolver.setExposeSpringMacroHelpers(true);viewResolver.setExposeRequestAttributes(true);viewResolver.setRequestContextAttribute("rc");return viewResolver;}//配置静态资源的处理@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { super.configureDefaultServletHandling(configurer); configurer.enable(); //要求对静态资源的访问请求转发到servlet容器的默认servlet上 } //@Beanpublic VelocityConfigurer velocityConfig(){VelocityConfigurer velocityConfig=new VelocityConfigurer();velocityConfig.setResourceLoaderPath("/WEB-INF/views/");velocityConfig.setConfigLocation(context.getResource("/WEB-INF/velocity.properties"));return velocityConfig;}public void setApplicationContext(ApplicationContext context) throws BeansException {// TODO Auto-generated method stubthis.context = context;}}// 配置JSP视图解析器
三、velocity.properties配置文件
首先我们需要在src/main/webapp/WEB-IN目录下创建velocity.properties文件,并添加如下内容
userdirective=org.apache.tiles.velocity.template.AddAttributeDirective,\
org.apache.tiles.velocity.template.AddListAttributeDirective,\
org.apache.tiles.velocity.template.DefinitionDirective,\
org.apache.tiles.velocity.template.GetAsStringDirective,\
org.apache.tiles.velocity.template.ImportAttributeDirective,\
org.apache.tiles.velocity.template.InsertAttributeDirective,\
org.apache.tiles.velocity.template.InsertDefinitionDirective,\
org.apache.tiles.velocity.template.InsertTemplateDirective,\
org.apache.tiles.velocity.template.PutAttributeDirective,\
org.apache.tiles.velocity.template.PutListAttributeDirective
input.encoding=UTF-8
output.encoding=UTF-8
到这里以后我们的整个配置就已经完成了,接下来就让我们来试试有没有配置成功。
四、一个简单的测试
(1)在src/main/webapp/WEB-IN/views目录下创建home.vm,并且键入如下内容
<p style="text-align:center">你好 velocity</p>
(2)将整个项目发布到Tomcat服务器上,方法在我的上一篇博客中有讲到,这边就不在赘述。得到如下结果
可能有同学会遇到这样一个问题就是自己的项目发布以后会出现中文乱码,这里也简单的介绍一下解决方法:
由于我之前在velocity配置的过程中,就已经将差染色体设置成了UTF-8的格式,所以,同学们可以直接将项目改为UTF-8格式的就可以了,具体步骤如下: