eclipse中整合springMvc和velocity
1.项目所需要的jar包(有些可能多余)


2.在src目录下创建一个bean 一个一个controller ,路径如下

person代码:
package com.test.bean;
import java.util.Date;
public class Person {
private int id;
private String name;
private String password;
private int age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
IndexController代码:
package com.test.controller; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.test.bean.Person; public class IndexController implements Controller { @Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView(); Person p1 = new Person();
p1.setId(1);
p1.setName("胡楠");
p1.setPassword("123456");
p1.setAge(24);
p1.setBirthday(new Date()); List<Person> list = new ArrayList<Person>();
for(int i=0;i<10;i++){
Person p = new Person();
p.setId(1000+i);
p.setName("胡楠"+i);
p.setPassword("123456"+i);
p.setAge(24+i);
p.setBirthday(new Date());
list.add(p);
}
//集合
mav.addObject("persons", list);
//对象
mav.addObject("person",p1);
//request范围数据
request.setAttribute("requestData", "hunan");
//session范围数据
request.getSession().setAttribute("sessionData", "123456");
return mav;
} }
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
4.配置servlet name 为 mvc 对应的 mvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<!-- VelocityConfigurer 视图的配置 -->
<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>
<!-- VelocityViewResolver 视图的解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"
p:suffix=".vm" p:contentType="text/html; charset=UTF-8" p:exposeRequestAttributes="true" p:exposeSessionAttributes="true" p:dateToolAttribute="dateTool" p:numberToolAttribute="numberTool"/>
<bean name="/index.vm" class="com.test.controller.IndexController"/>
</beans>
5.在WebContent下创建view文件夹,在里面创建.vm文件 index.vm
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
显示对象的属性
<br> $person.id
<br> $person.name
<br> $person.password
<br> $person.age
<br> if语句
<br> #if($person.id <) id大于2 #else id小于2 #end
#if($person.password=="123456") 密码等于123456 #end
<hr>
foreach语句
<br> #foreach($p in $persons) $p.id
<br> $p.name
<br> $!p.email
<br> $dateTool.format("yyyy-MM-dd HH:mm:ss", $p.birthday)
<br> #end
<hr>
request范围数据
<br> $requestData
<br> session范围数据
<br> $sessionData
<br>
</body>
</html>
index.vm里面有一些velocity标签的部分用法
项目完毕,部署到tomcat运行 http://localhost:8082/TestSpringVelocity/index.vm
部分代码来自于http://hunan.iteye.com/blog/838657
eclipse中整合springMvc和velocity的更多相关文章
- eclipse中整合springMvc,velocity和sitemesh
1.项目所需要jar包 (有些可能多余) 2.创建UserController 目录如下: package qust.thb.usermanage.controller; import org.s ...
- 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 ...
- web项目环境搭建(2):整合SpringMVC+velocity
velocity是一个基于java的模板引擎.velocity应用于web开发时,前端设计人员可以只关注页面的显示效果,而java程序人员只关注业务逻辑代码.velocity将java代码从web页面 ...
- eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合mybatis
接上一篇: eclipse 创建maven 项目 动态web工程完整示例 eclipse maven工程自动添加依赖设置 maven工程可以在线搜索依赖的jar包,还是非常方便的 但是有的时候可能还需 ...
随机推荐
- HTML 通知公告练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python类相关
class A: def bar(self): print("BAR") self.f1() class B(A): def f1(self): print("B&quo ...
- Debian 8安装ibus输入法
# apt-get install ibus ibus-sunpinyin ibus-table-wubi
- ecshop 订单编号 get_order_sn
文件地址include/lib_order.php ,要引用需要先导入 lib_order.php require_once(ROOT_PATH . 'includes/lib_order.php') ...
- python中,ascii,unicode,utf8,gbk之间的关系梳理
在计算机中,经常遇到编码问题,本节主要梳理下ascii,unicode,utf8,gbk 这几种编码之间的关系. ASCII 计算机中,所有数据都以0和1来表示.在一开始的时候,要表示的内容比较少,人 ...
- C#根据时间产生有序的GUID编码
public static Guid GenerateGuid() { byte[] guidArray = Guid.NewGuid().ToByteArray(); , , ); DateTime ...
- Markdown语言详解
相信大家在github上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是默认的README.md文件,也不曾对这个文件有过什么了解.但是在看到别人写的项目的README.md里面竟然有图 ...
- vim关于 引号和 括号的 高效操作-很好很强大的!
http://blog.csdn.net/bigshady/article/details/6019963 对括号匹配, 进行跳转, 使用的是%. 匹配的括号, 都会被高亮显示, 但是: 根据光标的 ...
- Java学习注意事项
一个Java文件中可以包含多个类. 如果有public类,则文件名必须和public类一样. 例如: class Pie { void f(){ System.out.println("Pi ...
- HDU4930 Fighting the Landlords 模拟
Fighting the Landlords Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...