veloctiy入門



-------------------------------------------------------------------------------------------
veloctiy  
-------------------------------------------------------------------------------------------
2007-12-12 20:29           571,259 commons-collections.jar
2007-12-12 20:29            52,915 commons-logging.jar
2007-12-12 20:29         2,545,110 spring.jar
2007-12-12 20:29           361,173 velocity-1.4.jar
-------------------------------------------------------------------------------------------
    
TestController.java
dispatcher-servlet.xml
velocity.properties
web.xml
test.html
-------------------------------------------------------------------------------------------
TestController.java
-------------------------------------------------------------------------------------------
package view;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class TestController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = request.getParameter("name");
        if(name==null)
            name = "spring";
        Map model = new HashMap();
        model.put("name", name);
        model.put("time", new Date());
        return new ModelAndView("test", model);
    }

}

-------------------------------------------------------------------------------------------
dispatcher-servlet.xml
-------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd"
>
    <bean name="/test.html" class="view.TestController" />

    <!--   Velocity      -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".html" />
    </bean>

    <!--   Velocity -->
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <!--        -->
        <property name="configLocation" value="/WEB-INF/velocity.properties" />
        <!--        -->
        <property name="resourceLoaderPath" value="/" />
    </bean>
</beans>


-------------------------------------------------------------------------------------------
velocity.properties

-------------------------------------------------------------------------------------------
runtime.log.logsystem.class = org.apache.velocity.runtime.log.SimpleLog4JLogSystem

runtime.log = view

runtime.log.error.stacktrace = true
runtime.log.warn.stacktrace = true
runtime.log.info.stacktrace = false
runtime.log.invalid.reference = true

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

directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1

directive.include.output.errormsg.start = <!-- include error :
directive.include.output.errormsg.end   = see error log -->

directive.parse.max.depth = 3

resource.loader = file

file.resource.loader.description = Velocity-File-Resource-Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 1

velocimacro.library = /macro.txt
velocimacro.library.autoreload = true


-------------------------------------------------------------------------------------------
web.xml
-------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

-------------------------------------------------------------------------------------------
test.html

-------------------------------------------------------------------------------------------
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Spring_Velocity</title>
</head>
<body>
    <h3>Hello, ${name}, it is ${time}</h3>
</body>
</html>