1. 程式人生 > >Spring MVC Velocity模板引擎

Spring MVC Velocity模板引擎

Spring  MVC  Velocity模板引擎

專案完成時,結構如下:


各個檔案的原始碼

webtest\src\main\webapp\WEB-INF\web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="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">

    <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                   org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
            <listener-class>
                  org.springframework.web.context.ContextLoaderListener
            </listener-class>
    </listener>

</web-app>

webtest\src\main\webapp\WEB-INF\dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="letian.spring.controller" />

    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath">
            <value>/WEB-INF/template/</value>
        </property>
        <property name="configLocation" value="/WEB-INF/template/velocity.properties"/>  <!--在配置中防止中文亂碼-->
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix">
            <value>.vm</value>
        </property>
        <property name="contentType">
            <value>text/html;charset=UTF-8</value>  <!--防止中文亂碼-->
        </property>
    </bean>

</beans>

webtest\src\main\webapp\WEB-INF\template\velocity.properties:

input.encoding=UTF-8
output.encoding=UTF-8

webtest\src\main\webapp\WEB-INF\template\index.vm:

<html>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
    <head>
        <title>My first example using Spring 3 MVC</title>
    </head>
    <body>
        <h2>hello</h2>
    </body>
</html>

webtest\src\main\webapp\WEB-INF\template\hello.vm:

<html>
    <head>
        <title>Velocity</title>
    </head>
    <body>
        <h1>${message}</h1>
    </body>
</html>

webtest/src/main/java/letian/spring/controller/HelloController.java:

package letian.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Hi, I am Velocity");
        return "hello";

    }   

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public ModelAndView printHi(ModelMap model) {

        return new ModelAndView("index");  

    }   

}
webtest\pom.xml
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springmvc</groupId>
  <artifactId>webtest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>webtest Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>
<spring.version>4.1.4.RELEASE</spring.version>
         </properties>
         
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
           <version>${spring.version}</version>
     </dependency>
     
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
     </dependency>
          <dependency>
<groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
     </dependency>
         <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
        <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
    </dependency>
    <!-- velocity -->
        <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
  <version>${spring.version}</version>
    </dependency>
    <dependency>

   <groupId>org.apache.velocity</groupId>

   <artifactId>velocity</artifactId>

   <version>1.7</version>

  </dependency>

  <dependency>

   <groupId>org.apache.velocity</groupId>

   <artifactId>velocity-tools</artifactId>

   <version>2.0</version>

  </dependency>
       <!-- velocity -->
<dependency>
	<groupId>	org.apache.commons</groupId>
	<artifactId>commons-collections4</artifactId>
	<version>4.0</version>
</dependency>
	<dependency>
	<groupId>oro</groupId>
	<artifactId>oro</artifactId>
	<version>2.0.8</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>webtest</finalName>
  </build>
</project>


開啟瀏覽器,訪問 http://localhost:8080/webtest/,可以看到


訪問 http://localhost:8080/HelloSpring/hi,可以看到:


轉載:http://www.codeweblog.com/spring-3-mvc-%E4%BD%BF%E7%94%A8velocity%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E/