新聞中心
這里有您想知道的互聯網營銷解決方案
eclipse中整合springMvc和velocity-創(chuàng)新互聯
1.項目所需要的jar包(有些可能多余)
2.在src目錄下創(chuàng)建一個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 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);
//對象 mav.addObject("person",p1);
//request范圍數據 request.setAttribute("requestData", "hunan");
//session范圍數據 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 對應的 mvc-servlet.xml
loopCounter 0 UTF-8 UTF-8
5.在WebContent下創(chuàng)建view文件夾,在里面創(chuàng)建.vm文件 index.vm
Insert title here
顯示對象的屬性
$person.id
$person.name
$person.password
$person.age
if語句
#if($person.id <2) id大于2 #else id小于2 #end
#if($person.password=="123456")密碼等于123456 #end
foreach語句
#foreach($p in $persons) $p.id
$p.name
$!p.email
$dateTool.format("yyyy-MM-dd HH:mm:ss", $p.birthday)
#end
request范圍數據
$requestData
session范圍數據
$sessionData
index.vm里面有一些velocity標簽的部分用法
項目完畢,部署到tomcat運行 http://localhost:8082/TestSpringVelocity/index.vm
部分代碼來自于http://hunan.iteye.com/blog/838657
當前標題:eclipse中整合springMvc和velocity-創(chuàng)新互聯
網頁路徑:http://www.dlmjj.cn/article/dogisc.html