javascript
在Spring MVC中使用Velocity
在Spring MVC中使用Velocity – Part 1工程中配置velocity
目的
Spring MVC中結(jié)合velocity的配置和操作。
簡(jiǎn)介
我們要顯示一個(gè)課程列表,需要如下的 Java model類,在 mvc model中分別建立:Course.java、Instructor.java。 在service下創(chuàng)建CourseService.java文件,在controller下建立CourseController.java。其中Course是課程表信息;Instructor是任課教師的信息;CourseService用來列出課程信息,兼有隱含DAO;ListCourse是實(shí)現(xiàn)了Controller的控制器,返回ModelAndView。下面分別列出代碼:
Java Model
類 service
類controller
現(xiàn)在編寫Velocity模板,在WEB-INF/velocitly下面建立一個(gè)courseList.vm的文件,內(nèi)容如下:
配置
這里的配置應(yīng)該是在工程建立時(shí)進(jìn)行的工作,為方便查看此文檔,放在這里;分別配置web.xml及Spring配置文件training-servlet.xml。
先要配置java web工程的web.xml。這里spring mvc的servlet配置
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param><filter><filter-name>EncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>EncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>training</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>training</servlet-name><url-pattern>*.htm</url-pattern><servlet-mapping><servlet-name>training</servlet-name><url-pattern>*.json</url-pattern></servlet-mapping><error-page><error-code>500</error-code><location>/common/500.jsp</location></error-page><error-page><error-code>404</error-code><location>/common/404.jsp</location></error-page></web-app>接著Spring MVC的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:beans="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"><context:component-scan base-package="com.simple.mvc" /><!-- 定時(shí)器任務(wù)注解驅(qū)動(dòng) --><task:annotation-driven /><beans:import resource="training-servlet.xml" /><!--<beans:import resource="dataAccessContext.xml" /><beans:import resource="spring-security.xml" />--> </beans>還有 spring 的配置 training-servlet.xml,其中的velocityConfigurer為velocity 引擎配置。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" > <beans><bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="listCourse.htm">listCourse</prop></props></property></bean> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"><property name="resourceLoaderPath"><value>WEB-INF/velocity</value></property></bean><bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"><property name="suffix"><value>.vm</value></property></bean></beans>運(yùn)行
啟動(dòng)服務(wù),并測(cè)試。
建立工程時(shí),保證WEB-INF/lib下有如下包:
spring MVC相關(guān)的jar包,還有velocity-1.4.jar、commons-collections.jar、commons-logging.jar、log4j-1.2.13.jar等。
同時(shí),視需要配置log4j.properties放在WEB-INF下。
然后運(yùn)行工程,在瀏覽器中訪問:
http://localhost:8080/velocity/listCourse.htm
總結(jié)
在spring MVC中配置velocity模板引擎
在Spring MVC中使用Velocity – Part 2 使用velocity 模板的布局layout
簡(jiǎn)介
現(xiàn)在,不支持布局功能的模板無法得到用戶的青睞。velocity自然添加了對(duì)布局的支持。
配置
在velocity中加入布局的支持,使用的解析器引擎不是VelocityViewResolver,變成了VelocityLayoutViewResolver
請(qǐng)看下面的spring-mvc 配置文件
Spring mvc 配置文件(引入velocity相關(guān)配置spring-mvc.xml)
在檢查一下web.xml工程配置
web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>mybatis</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext-user.xml</param-value> </context-param> <filter> <description>字符集過濾器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集編碼</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <description>spring監(jiān)聽器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止spring內(nèi)存溢出監(jiān)聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- spring mvc servlet --> <servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- 配置session超時(shí)時(shí)間,單位分鐘 --> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>velocity及布局模板文件
在上面的配置中,velocity的文件multi為/WEB-INF/view/
Spring MVC類
控制層Controller
UserController.java
package com.simple.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import com.simple.entity.UserEntity; import com.simple.service.UserService; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/userList") public String list(ModelMap model) { model.put("users", userService.getUserEntities()); return "list";} @RequestMapping("/user/{id}") public String detail(@PathVariable(value = "id") String id, ModelMap model) { model.put("user", userService.getUserEntityById(id)); return "detail"; } }模型Model類
UserEntity.java
package com.simple.entity; public class UserEntity { private String userId; private String userName; private String password; private String sex; private String email; // Getter / Setter ... }業(yè)務(wù)邏輯接口(Service)
package com.simple.service; import java.util.List; import com.simple.entity.UserEntity; public interface UserService { UserEntity getUserEntityById(String userId); List<UserEntity> getUserEntities(); // UserEntity insertUserEntity(UserEntity userEntity); }接口實(shí)現(xiàn)(ServiceImpl)
package com.simple.service.impl; import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import com.simple.entity.UserEntity; import com.simple.service.UserService; public class UserServiceImpl implements UserService { private List<UserEntity> users;private UserServiceImpl() {//users = new ArrayList<UserEntity>();UserEntity user = new UserEntity();// user.set......users.add(user);//......}@Override public UserEntity getUserEntityById(String userId) { return this.users.get(0); } @Override public List<UserEntity> getUserEntities() { return this.users; } }總結(jié)
使用velocity的布局功能,velocity的resolver要替換為VelocityLayoutViewResolver。然后就可以使用velocity項(xiàng)目的layout相關(guān)指令了。
總結(jié)
以上是生活随笔為你收集整理的在Spring MVC中使用Velocity的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 猎魂觉醒的心机攻略有哪些
- 下一篇: 为什么先锋电视便宜(汉典为字的基本解释)