Maven spring velocity integration tutorial, with code, mavenvelocity
1. Create a maven project and select a webapp template.
2. Add spring dependency in pom. xml.
3. Add the velocity dependency to pom. xml.
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version></dependency>
4. Use spring's DispatcherServlet in web. xml and select the corresponding suffix name.
<servlet> <servlet-name>layout</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>layout</servlet-name> <url-pattern>*.htm</url-pattern> <url-pattern>*.vm</url-pattern></servlet-mapping>
5. Use the velocity ing in the servlet configuration file.
<! -- View --> <bean id = "velocityConfig" class = "org. springframework. web. servlet. view. velocity. velocitycycler "> <property name =" resourceLoaderPath "value ="/views/velocity/"/> <! -- Configure the velocity page path --> <! -- Velocity configuration properties --> <property name = "configLocation" value = "classpath: velocity. properties"/> <! -- Velocity configuration path --> </bean> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. velocity. velocityViewResolver "> <property name =" suffix "value = ". vm "/> <! -- <Property name = "suffix" value = ". htm"/> --> </bean>
6. Create a velocity/views folder in the webapp path, which corresponds to the view position in the previous step, and create a hellowrold example.
7. Add the corresponding mapping code in src/main/java.
@Controller@RequestMapping(value = "/velocity")public class VelocityDemo { Logger logger = LoggerFactory.getLogger(VelocityDemo.class); @RequestMapping(value = "/hellosite") public String index(Model model) throws Exception { model.addAttribute("name", "highkgao"); logger.info(model.toString()); return "hellosite"; }}
8. Enable and use tomcat to access
Http: /localhost: 8080/velocityDemo/velocity/hellosite. vm
The result is:
Hello World VM! Highkgao
Demo project download link:
Http://download.csdn.net/detail/highkgao1988/9063501