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包弄错了

eclipse中整合springMvc,velocity和sitemesh的更多相关文章
- eclipse中整合springMvc和velocity
1.项目所需要的jar包(有些可能多余) 2.在src目录下创建一个bean 一个一个controller ,路径如下 person代码: package com.test.bean; import ...
- web项目环境搭建(2):整合SpringMVC+velocity
velocity是一个基于java的模板引擎.velocity应用于web开发时,前端设计人员可以只关注页面的显示效果,而java程序人员只关注业务逻辑代码.velocity将java代码从web页面 ...
- eclipse中整合ejb和web工程
用 Eclipse JEE 版本的话,新建一个 Enterprise Application Project 工程(New --> Java EE --> Enterprise Appli ...
- Eclipse中安装springmvc插件
我网上找了很多方法,常见的两种: 方法一: 先去下载spring-framework-x.x.x.RELEASE.zip包,然后解压,后面需要配置什么,具体的笔者也记不了,哈哈哈 方法二: 打开菜单栏 ...
- github在eclipse中的配置
http://www.cnblogs.com/yejiurui/archive/2013/07/29/3223153.html http://blog.csdn.net/shehun1/article ...
- Git/Github的使用以及与Eclipse的整合
Git简介 Git是一个免费的.分布式的版本控制工具,或是一个强调了速度快的源代码管理工具.每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,不依赖于网络 ...
- 零基础学习java------40---------Maven(maven的概念,安装,maven在eclipse中使用),springboot(spring整合springmvc(注解),spring整合mybatis(常见的配置文件)),前端页面(bootstrap软件)
一 maven 1. Maven的相关概念 1.1 项目开发中遇到的问题 (1)都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? (2)为什么在我的机器上可以正常打包,而配置管理 ...
- Eclipse中Maven+Spring3.2.8+SpringMVC HelloWorld项目
本文适合有一定spring和springmvc基础,并想使用Maven管理项目的人. 源码打包:http://pan.baidu.com/s/1hqurUcs 转载请声明出处(http://www.c ...
- eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合mybatis
接上一篇: eclipse 创建maven 项目 动态web工程完整示例 eclipse maven工程自动添加依赖设置 maven工程可以在线搜索依赖的jar包,还是非常方便的 但是有的时候可能还需 ...
随机推荐
- rem自适应布局的回顾总结
使用rem实现自适应布局,应该算是当前移动前端的一大趋势,有些人对此还有点迷惑,搞不懂rem是如何实现自适应布局,如何根据设计稿来调整rem的值?rem布局如何用雪碧背景图片?rem一定要加载JS吗? ...
- CodeForces 716B Complete the Word
题目链接:http://codeforces.com/problemset/problem/716/B 题目大意: 给出一个字符串,判断其是否存在一个子串(满足:包含26个英文字母且不重复,字串中有‘ ...
- C#值类型参数传递的性能开销
Performance issues Let's dig a little deeper. When data is passed into methods as value type paramet ...
- CentOS 7 AMD64安装nginx和mysql
NGINX: rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.0-1.el7.ngx.x86_64.rpm 查看: ...
- Orchard源码分析(4.4):Orchard.Caching.CacheModule类
概述 CacheModule也是一个Autofac模块. 一.CacheModule类 CacheModule将DefaultCacheManager注册为ICacheManager: ...
- PHP7的安装
PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导致crash了, 那么整个服务就挂 ...
- java 的 AccessController.doPrivileged使用
AccessController.doPrivileged意思是这个是特别的,不用做权限检查. 在什么地方会用到呢:加入1.jar中有类可以读取一个文件,现在我们要使用1.jar去做这个事情.但是我们 ...
- VS Code 开发asp.net core 遇到的坑
摘要 微软发布.NET Core 1.0,ASP.NET Core 1.0 与 Entity Framewok 1.0也有一段时间了,一直没进行这方面的学习,节前公司让调研这方面的可行性.所以还是从最 ...
- [Angularjs]系列——学习与实践
写在前面 这个系列是来这家公司到现在,边用边学,以及在工作中遇到的问题进行的总结.深入的东西不多,大多是实际开发中用到的东西. 这里做一个目录,方便查看. 系列文章 [Angularjs]ng-sel ...
- URAL1826. Minefield 题解
原题: http://acm.timus.ru/problem.aspx?space=1&num=1826 1826. MinefieldTime limit: 0.5 secondMemor ...