Spring MVC调整Velocity的例子
Spring MVC整合Velocity的例子
上午简单的学了下springmvc和velocity的整合,跟大家分享下,项目结构见附件。
1、Person.java是一个简单的JavaBean代码如下:
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; } }
2、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>*.html</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>
3、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> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer" p:resourceLoaderPath="/WEB-INF/velocity"> <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> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver" p:suffix=".html" p:contentType="text/html; charset=UTF-8" p:exposeRequestAttributes="true" p:exposeSessionAttributes="true" p:dateToolAttribute="dateTool" p:numberToolAttribute="numberTool"/> <bean name="/index.html" class="com.test.controller.IndexController"/> </beans>
4、模板文件index.html
<!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>
5、控制器
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("main"); 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; } }
启动tomcat,输入http://xxx.renren.com:8080/index.html。