eclipse中整合springMvc,velocity和sitemesh

1.项目所需要jar包 (有些可能多余)

2.创建UserController   目录如下:

package qust.thb.usermanage.controller;

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

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = "/getUser.do", method = RequestMethod.GET)
    public ModelAndView getUser() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "nice to meet you!!!");
        mv.setViewName("/view/hello");
        return mv;
    }
}

3.部署web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>SpringMVC</display-name>

    <!-- sitemesh配置 -->
    <filter>
        <filter-name>site-mesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>site-mesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <!-- sitemesh servlet配置 -->
    <servlet>
        <servlet-name>site-mesh-velocity</servlet-name>
        <servlet-class>com.opensymphony.module.sitemesh.velocity.VelocityDecoratorServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>site-mesh-velocity</servlet-name>
        <url-pattern>*.vm</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

4.创建springMvc对应的 springMvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--看到下面的beans这个元素标签没有,必须有标签的声明 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="qust.thb.*" />
    <!-- 支持spring3.0新的mvc注解 -->
    <mvc:annotation-driven />
     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
          <property name="resourceLoaderPath" value="/WEB-INF/"/>
          <property name="configLocation" value="/WEB-INF/velocity.properties"/>
    </bean>
    <!-- 配置内容也可以直接写在该文件内
    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer" p:resourceLoaderPath="/view">
       <property name="velocityProperties">
          <props>
            <prop key="directive.foreach.counter.name">loopCounter</prop>
            <prop key="directive.foreach.counter.initial.value">0</prop>
            <prop key="input.encoding">UTF-8</prop>
            <prop key="output.encoding">UTF-8</prop>
          </props>
        </property>
    </bean>
     -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
          <property name="cache" value="true"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=".vm"/>
    </bean>

    <!--另外一种视图解析器
     只是加上前缀、后缀,而不进行视图解析
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".vm" />
    </bean>
    -->
</beans>

5.在WebContent下创建view文件夹,创建hello.vm

<div>
    ${message}
</div>

6.VelocityConfigurer要加载的velocity.properties文件

# ----------------------------------------------------------------------------
# T E M P L A T E  E N C O D I N G
# ----------------------------------------------------------------------------
input.encoding=UTF-8
output.encoding=UTF-8
#runtime.log.logsystem.log4j.category=velocity

以上步骤是spring整合velocity

6.在WEB-INF下创建decorators.xml

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
    <!-- 此处定义不需要过滤的页面 -->
    <excludes>
        
    </excludes>
    
    <!-- 此处用来定义装饰器需要过滤的页面 -->
    <decorator name="template" page="template.vm">
        <pattern>/user/getUser.do</pattern>
    </decorator>
</decorators>

7.在WebContent目录下建立decorators文件夹,创建template.vm文件

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>page include</title>
</head>
<body>
    #set($name="hello world nice")
    $name
    $body
</body>
</html>

其中$body即为要加载的hello.vm的内容,剩下的内容可以为hello.vm添加header footer等内容,实现了sitemesh为页面添加统一的导航条,页面首尾等等的功能

将项目部署到tomcat ,访问 http://localhost:8082/SpringMvc/user/getUser.do

页面将会显示:

hello world nice nice to meet you!!!

部分代码来源于http://blog.csdn.net/qust008/article/details/9625179

 

最后加入:我在搞 maven + springMvc + velocity + sitemesh 搭建一个小项目的时候,sitemesh渲染velocity一直不成功,一直说 Velocity  [error] Null reference [template '/decorators/template.vm', line 12, column 9] : $body cannot be resolved. ,改变.vm文件路径或者改变url名字还是没解决,弄了一下午一晚上才找到原因,原来是jar包弄错了

posted @ 2013-09-25 14:54  CalronLoveRonnie  阅读(952)  评论(0编辑  收藏  举报
AmazingCounters.com