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 <2) 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

posted @ 2013-09-25 14:08  CalronLoveRonnie  阅读(425)  评论(0编辑  收藏  举报
AmazingCounters.com