springboot對velocity的支援
阿新 • • 發佈:2018-12-18
最近在使用springboot中,用velocity作為檢視模板的時候,一直出現問題,無法返回頁面。
通過查詢資料得知,springboot預設是不支援velocity的,需要新增而外配置。
並且在高版本的springboot中是不支援velocity了。最高只到1.4.x版本支援,1.5以及以後的版本,都不支援velocity了。
推薦使用:spring4.3之後,推薦FreeMarker作為檢視。
package org.springframework.web.servlet.view.velocity; import org.springframework.web.servlet.view.AbstractUrlBasedView; /** * Convenience subclass of VelocityViewResolver, adding support * for VelocityLayoutView and its properties. * * <p>See VelocityViewResolver's javadoc for general usage info. * * @author Juergen Hoeller * @since 1.2.7 * @see VelocityViewResolver * @see VelocityLayoutView * @see #setLayoutUrl * @see #setLayoutKey * @see #setScreenContentKey * @deprecated as of Spring 4.3, in favor of FreeMarker */ @Deprecated public class VelocityLayoutViewResolver extends VelocityViewResolver { }
配置springboot+velocity
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hgh.springboot</groupId> <artifactId>ssm</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>ssm</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.26</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- springboot只在1.4.x版本之前支援 velocity,後面的版本就不支援了 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
WebMvcConfig
package com.hgh.springboot.ssm; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.velocity.VelocityLayoutView; import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter{ /** * 配置檢視解析器 * 針對jsp * @return */ // @Bean // public InternalResourceViewResolver viewResolver(){ // InternalResourceViewResolver viewResolver= new InternalResourceViewResolver(); // //配置的是專案編譯之後,檢視所在的路徑 // ///resources/templates/views/index.jsp // viewResolver.setPrefix("/resources/templates/views/"); // viewResolver.setSuffix(".jsp"); // viewResolver.setViewClass(JstlView.class); // //viewResolver.setViewClass(VelocityLayoutView.class); // return viewResolver; // } // /** // * 配置檢視解析器 // * 針對velocity,spring後面已經不推薦使用了,改用freemark // * @return // */ @Bean public VelocityLayoutViewResolver viewResolver(){ VelocityLayoutViewResolver viewResolver= new VelocityLayoutViewResolver(); //配置的是專案編譯之後,檢視所在的路徑 viewResolver.setPrefix("/"); viewResolver.setSuffix(".vm"); viewResolver.setViewClass(VelocityLayoutView.class); //viewResolver.setViewClass(VelocityLayoutView.class); return viewResolver; } }
SsmApplication
package com.hgh.springboot.ssm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
@EnableWebMvc
public class SsmApplication {
public static void main(String[] args) {
SpringApplication.run(SsmApplication.class, args);
}
}
VelocityContoller
package com.hgh.springboot.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class VelocityContoller {
@RequestMapping("velocity")
public String helloVelocity(){
return "testvelocity";
}
}
testvelocity.vm(/ssm/src/main/resources/templates/testvelocity.vm)
<!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=UTF-8">
<title>你好</title>
</head>
<body>
12341324
</body>
</html>