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包,还是非常方便的 但是有的时候可能还需 ...
随机推荐
- 《零成本实现Web性能测试:基于Apache JMeter》读书笔记
1.性能测试概念 性能测试目的: 评估系统能力,验证系统是否符合预期性能指标 识别系统中的弱点 系统调优,改进系统性能 检测长时间运行可能发生的问题,揭示隐含问题 验证稳定性.可靠性 常见性能指标 B ...
- SQL语句注入
1: select *from user where username='admin' and password='123456' or 1='1'; 万能密码 2: ...
- js切换实现背景颜色
<script type="text/javascript"> obj=document.getElementsByTagName('h1'); ;i<obj.l ...
- 9月20日上午JavaScript函数
函数 一. 函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...
- Java I/O流体系
- 《CSS3实战》读书笔记 第2章 层叠样式表(CSS)
## 层叠样式表 本章将阐述CSS的基本规则. ### 解构CSS 所谓CSS,由选择器(selector)和声明块(declaration block)组成.再进一步细分,每个声明包括了属性和值. ...
- MyEclipse for linux 破解方法
1.安装MyEclipse: uu@pc:~/desktop$ chmod +x myeclipse-pro-2014-GA-offline-installer-linux.run uu@pc:~/d ...
- AWK改变输入输出分隔符实例分析
awk默认从STDIN接受数据,打印文本到STDOUT. awk的默认输入和输出分隔符: FS : 输入字段分隔符,默认空格. RS : 输入行分隔符,默认\n. OFS : 输出字段分隔符,默认空格 ...
- mono中显示debug信息(filename/lineno)
一直发现 mono 的 traceback 没有 fliename.lineno,很奇怪.研究了下,原来编译和运行时要加参数的. dmcs -debug /r:xunit.dll /t:library ...
- python中单引号,双引号,多引号区别
先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行如:s1 = "hello,world"如果要写成多行,那么就要使用\ (“连行符”)吧,如s2 = " ...