SpringMVC集成velocity模版

jopen 4年前

  1. 项目的目录结构如图


  2. UserAction,首先在action层把需要返给前端的数据组装好,源代码如下

package com.shengfeng.action;    import java.util.HashMap;  import java.util.Map;    import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.beans.factory.annotation.Qualifier;  import org.springframework.stereotype.Controller;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.servlet.ModelAndView;    import com.shengfeng.po.User;  import com.shengfeng.service.UserService;      @Controller  @RequestMapping("/user")  public class UserAction {            @Autowired      @Qualifier("userService")      private UserService userService;            @RequestMapping("/userInfo.html")      public ModelAndView getUserInfo() {          User user = userService.getUserByUserId(1);          Map<String,Object> userInfoMap = new HashMap<String,Object>();          userInfoMap.put("username",user.getUsername());          userInfoMap.put("password",user.getPassword());          userInfoMap.put("level",user.getLevel());          userInfoMap.put("sex",user.getSex());          userInfoMap.put("mobilephone",user.getMobilephone());          userInfoMap.put("img",user.getImg());                    ModelAndView view = new ModelAndView("/user/userInfo");          view.addObject("userInfo",userInfoMap);          return view;                }  }


3.然后spring的配置文件,applicationContext.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:context="http://www.springframework.org/schema/context"   xmlns:aop="http://www.springframework.org/schema/aop"   xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.2.xsd             http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">     <context:annotation-config />     <context:component-scan base-package="com.shengfeng"/>         <bean id="velocityConfigurer"    class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">    <property name="resourceLoaderPath">     <value>WEB-INF/view</value>    </property>    <property name="velocityProperties">     <props>      <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">    <property name="contentType" value="text/html;charset=UTF-8" />    <property name="exposeSpringMacroHelpers" value="true" />    <property name="suffix" value=".vm" />    <property name="toolboxConfigLocation" value="WEB-INF/velocity-toolbox.xml" />    <property name="cache" value="false" />   </bean>      </beans>

4.velocity-toolbox.xml

<?xml version="1.0"?>  <toolbox>      <tool>          <key>date</key>          <scope>request</scope>          <class>              org.apache.velocity.tools.generic.DateTool          </class>          <parameter name="format" value="yyyy-MM-dd HH:mm:ss"/>      </tool>      <tool>          <key>link</key>          <scope>request</scope>          <class>org.apache.velocity.tools.view.tools.LinkTool</class>      </tool>      <tool>          <key>stringUtils</key>          <scope>request</scope>          <class>org.apache.velocity.util.StringUtils</class>      </tool>      <tool>          <key>math</key>          <scope>application</scope>          <class>org.apache.velocity.tools.generic.MathTool</class>      </tool>      <tool>          <key>esc</key>          <scope>request</scope>          <class>org.apache.velocity.tools.generic.EscapeTool</class>      </tool>      <tool>          <key>params</key>          <scope>request</scope>          <class>org.apache.velocity.tools.view.tools.ParameterParser</class>      </tool>  </toolbox>


5.userInfo.vm

{     "username":"$!{userInfo.username}",   "password":"$!{userInfo.password}",   "level":"$!{userInfo.level}",   "sex":$!{userInfo.sex},   "mobilephone":"$!{userInfo.mobilephone}",   "img":"$!{userInfo.img}"     }

6.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"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   version="2.5">     <display-name>myweb</display-name>     <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath*:applicationContext*.xml</param-value>   </context-param>     <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>     <servlet>    <servlet-name>myweb</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>     <param-name>contextConfigLocation</param-name>     <param-value>classpath*:applicationContext.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>    <servlet-name>myweb</servlet-name>    <url-pattern>*.html</url-pattern>   </servlet-mapping>        </web-app>


7.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>shengfeng.item</groupId>   <artifactId>myweb</artifactId>   <packaging>war</packaging>   <version>1.0</version>   <name>myweb Maven Webapp</name>   <url>http://maven.apache.org</url>     <build>    <finalName>myweb</finalName>    <resources>     <resource>      <includes>       <include>datasource.properties</include>       <include>applicationContext.xml</include>       <include>applicationContext-mybatis.xml</include>      </includes>      <directory>src/main/resources</directory>      <filtering>true</filtering>     </resource>     <resource>      <directory>src/main/resources</directory>      <filtering>false</filtering>     </resource>     <resource>      <directory>src/main/java</directory>      <includes>       <include>**/*.xml</include>      </includes>     </resource>    </resources>    <testResources>     <testResource>      <directory>src/test/resources</directory>      <filtering>false</filtering>     </testResource>    </testResources>     </build>         <dependencies>    <dependency>     <groupId>junit</groupId>     <artifactId>junit</artifactId>     <version>4.11</version>     <scope>test</scope>    </dependency>      <!-- mybatis核心包 -->    <dependency>     <groupId>org.mybatis</groupId>     <artifactId>mybatis</artifactId>     <version>3.2.8</version>    </dependency>      <!-- mybatis/spring包 -->    <dependency>     <groupId>org.mybatis</groupId>     <artifactId>mybatis-spring</artifactId>     <version>1.2.2</version>    </dependency>      <!-- mysql驱动包 -->    <dependency>     <groupId>mysql</groupId>     <artifactId>mysql-connector-java</artifactId>     <version>5.1.29</version>    </dependency>      <!-- spring核心包 -->    <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-core</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-web</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-oxm</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-tx</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-jdbc</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-webmvc</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-aop</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-aspects</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-context-support</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-aop</artifactId>     <version>3.2.11.RELEASE</version>    </dependency>      <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-test</artifactId>     <version>3.2.11.RELEASE</version>     <scope>test</scope>    </dependency>        <dependency>     <groupId>com.alibaba</groupId>     <artifactId>fastjson</artifactId>     <version>1.1.41</version>    </dependency>        <dependency>     <groupId>commons-lang</groupId>     <artifactId>commons-lang</artifactId>     <version>2.4</version>    </dependency>      <!-- 阿里巴巴数据源包 -->    <dependency>     <groupId>com.alibaba</groupId>     <artifactId>druid</artifactId>     <version>1.0.2</version>    </dependency>    <dependency>     <groupId>org.apache.httpcomponents</groupId>     <artifactId>httpclient</artifactId>     <version>4.3.5</version>    </dependency>        <dependency>     <groupId>org.apache.velocity</groupId>     <artifactId>velocity</artifactId>     <version>1.7</version>    </dependency>    <dependency>     <groupId>org.apache.velocity</groupId>     <artifactId>velocity-tools</artifactId>     <version>2.0</version>    </dependency>     </dependencies>    </project>

8.启动tomcat,访问http://localhost:8080/myweb/user/userInfo.html可以看到:


返回了一个json数据,大功告成

来自: http://my.oschina.net/lsf930709/blog/593933