eclipse中整合springMvc和velocity-创新互联

1.项目所需要的jar包(有些可能多余)

创新互联是一家专业提供锡林浩特企业网站建设,专注与网站设计制作、成都做网站、H5高端网站建设、小程序制作等业务。10年已为锡林浩特众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

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 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下创建view文件夹,在里面创建.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-创新互联
当前地址:http://scyanting.com/article/dogisc.html