1.項(xiàng)目所需要的jar包(有些可能多余)
創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供錫林浩特企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為錫林浩特眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。2.在src目錄下創(chuàng)建一個(gè)bean 一個(gè)一個(gè)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 list = new ArrayList();
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);
//對(duì)象 mav.addObject("person",p1);
//request范圍數(shù)據(jù) request.setAttribute("requestData", "hunan");
//session范圍數(shù)據(jù) request.getSession().setAttribute("sessionData", "123456");
return mav;
}
}
3.配置web.xml
mvc
org.springframework.web.servlet.DispatcherServlet
1
mvc
*.vm
index.jsp
BASIC
4.配置servlet name 為 mvc 對(duì)應(yīng)的 mvc-servlet.xml
loopCounter 0 UTF-8 UTF-8
5.在WebContent下創(chuàng)建view文件夾,在里面創(chuàng)建.vm文件 index.vm
Insert title here
顯示對(duì)象的屬性
$person.id
$person.name
$person.password
$person.age
if語(yǔ)句
#if($person.id <2) id大于2 #else id小于2 #end
#if($person.password=="123456")密碼等于123456 #end
foreach語(yǔ)句
#foreach($p in $persons) $p.id
$p.name
$!p.email
$dateTool.format("yyyy-MM-dd HH:mm:ss", $p.birthday)
#end
request范圍數(shù)據(jù)
$requestData
session范圍數(shù)據(jù)
$sessionData
index.vm里面有一些velocity標(biāo)簽的部分用法
項(xiàng)目完畢,部署到tomcat運(yùn)行 http://localhost:8082/TestSpringVelocity/index.vm
部分代碼來(lái)自于http://hunan.iteye.com/blog/838657