Spring 3 MVC:使用Velocity模板引擎

使用netbeans8建立java web项目HelloSpring,选择Spring 3.2.7。

velocity请到http://velocity.apache.org/download.cgi下载。

添加Velocity:

项目完成时,结构如下:



然后,菜单栏“File”-> “Project properties (HelloSpring)”->“Libraries”,把velocity的jar以及其依赖的一些jar全部引入:


结果:


下面看一下各个文件的源码。


/web/WEB-INF/web.xml:


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


/web/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>


/web/WEB-INF/template/velocity.properties:


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


/web/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>你好</h2>
    </body>
</html>



/web/WEB-INF/template/hello.vm:

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



/src/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");  

    }   

}



使用netbeans run这个项目,项目会自动部署。

打开浏览器,访问 http://localhost:8080/HelloSpring/,可以看到:



访问 http://localhost:8080/HelloSpring/hi,可以看到:



更多+参考


2、Spring MVC整合Velocity
http://blog.csdn.net/qust008/article/details/9623513

3、spring mvc sitemesh velocity整合
http://blog.csdn.net/qust008/article/details/9625179
sitemesh为模板引擎提供了模板继承的功能。

velocity下载
http://velocity.apache.org/download.cgi

Velocity:Part 1 - Basic Application and Environment Setup
http://wiki.apache.org/velocity/VelocityAndSpringStepByStep

Spring下velocity页面中文乱码问题
http://jackandroid.iteye.com/blog/579655


Java开源模板引擎:给出了很多的基于Java的模板引擎。
http://www.open-open.com/21.htm

FreeMarker:另一个模板引擎
http://freemarker.org/






转载于:https://my.oschina.net/letiantian/blog/367512

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值