现在的位置: 首页 > 综合 > 正文

3、spring mvc sitemesh velocity整合

2012年03月16日 ⁄ 综合 ⁄ 共 3238字 ⁄ 字号 评论关闭

接上一篇http://blog.csdn.net/qust008/article/details/9623513

这配置仍需导入包,新导入的包为commons-beanutils-1.7.0,sitemesh-2.4.2,velocity-tools-view-2.0

先创建一个通用模版,后台返回的vm都镶嵌在这个模版中

在WebContent下,新建文件夹decorators,在decorators下新建文件template.vm,内容如下

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>模版标题</title>
</head>
<body>
	$body
</body>
</html>

在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>

修改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 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/" />
		<property name="suffix" value=".vm" />
	</bean>
</beans>

修改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>

最终工程目录结构如下

访问地址http://localhost:8080/SpringMVC/user/getUser.do即可

至此,简单的不带连接数据库的框架搭建好了,源码下载地址

http://download.csdn.net/detail/qust008/5838407

抱歉!评论已关闭.