[关闭]
@slientStrom 2017-08-07T17:19:29.000000Z 字数 7751 阅读 644

spring整合velocity

maven,spring,velocity,restful
此次整合没有使用数据库。只包含controller层和实体类。
项目结构

一.pom.xml 定义

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.exapme</groupId>
  5. <artifactId>springMvc</artifactId>
  6. <packaging>war</packaging>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>springMvc Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <springVersion>4.3.6.RELEASE</springVersion>
  12. <jacksonVersion>2.8.7</jacksonVersion>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.11</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <!--spring的基本包 -->
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-context</artifactId>
  25. <version>${springVersion}</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-web</artifactId>
  30. <version>${springVersion}</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-context-support</artifactId>
  35. <version>${springVersion}</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-webmvc</artifactId>
  40. <version>${springVersion}</version>
  41. </dependency>
  42. <!-- veloctiy 基本包-->
  43. <dependency>
  44. <groupId>org.apache.velocity</groupId>
  45. <artifactId>velocity</artifactId>
  46. <version>1.7</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.apache.velocity</groupId>
  50. <artifactId>velocity-tools</artifactId>
  51. <version>2.0</version>
  52. </dependency>
  53. <!--json包 -->
  54. <dependency>
  55. <groupId>com.fasterxml.jackson.core</groupId>
  56. <artifactId>jackson-core</artifactId>
  57. <version>${jacksonVersion}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>com.fasterxml.jackson.core</groupId>
  61. <artifactId>jackson-annotations</artifactId>
  62. <version>${jacksonVersion}</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>com.fasterxml.jackson.core</groupId>
  66. <artifactId>jackson-databind</artifactId>
  67. <version>${jacksonVersion}</version>
  68. </dependency>
  69. </dependencies>
  70. <build>
  71. <finalName>springMvc</finalName>
  72. </build>
  73. </project>

二. 资源文件的配置

2.1 context.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  15. <context:component-scan base-package="com.learn"></context:component-scan>
  16. <mvc:annotation-driven />
  17. </beans>

2.2content-servlet配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  15. <context:component-scan base-package="com.learn"></context:component-scan>
  16. <mvc:annotation-driven />
  17. <!-- 将ResponseBody(rest返回的对象转换为json)-->
  18. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
  19. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  20. <property name="messageConverters">
  21. <list>
  22. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  23. <property name="supportedMediaTypes">
  24. <list>
  25. <value>text/html; charset=UTF-8</value>
  26. <value>application/json;charset=UTF-8</value>
  27. </list>
  28. </property>
  29. </bean>
  30. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  31. <property name="supportedMediaTypes">
  32. <list>
  33. <value>text/html; charset=UTF-8</value>
  34. <value>application/json;charset=UTF-8</value>
  35. </list>
  36. </property>
  37. </bean>
  38. </list>
  39. </property>
  40. </bean>
  41. <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  42. <property name="resourceLoaderPath" value="/WEB-INF/pages/"/>
  43. <property name="configLocation" value="classpath:velocity.properties"/>
  44. </bean>
  45. <!-- 配置视图的显示 -->
  46. <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  47. <property name="suffix" value=".html" /><!-- 视图文件的后缀名 -->
  48. <property name="contentType" value="text/html;charset=UTF-8" />
  49. </bean>
  50. </beans>

2.3web.xml配置

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:context/context.xml</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <servlet>
  14. <servlet-name>dispatcher</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <init-param>
  17. <param-name>contextConfigLocation</param-name>
  18. <param-value>classpath:context/context-servlet.xml</param-value>
  19. </init-param>
  20. <load-on-startup>1</load-on-startup>
  21. </servlet>
  22. <servlet-mapping>
  23. <servlet-name>dispatcher</servlet-name>
  24. <url-pattern>*.htm</url-pattern>
  25. </servlet-mapping>
  26. <servlet-mapping>
  27. <servlet-name>dispatcher</servlet-name>
  28. <url-pattern>*.json</url-pattern>
  29. </servlet-mapping>
  30. </web-app>

三.controller编写。

  1. package com.learn.controller;
  2. import com.learn.controller.res.UserRes;
  3. import com.learn.entity.User;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @Controller
  10. public class UserController {
  11. //视图跳转
  12. @RequestMapping("index.htm")
  13. public String homeView(){
  14. return "index";
  15. }
  16. //数据
  17. @RequestMapping("index.json")
  18. @ResponseBody
  19. public User getUser(){
  20. User user=new User();
  21. user.setName("小米");
  22. user.setAge(5);
  23. return user;
  24. }
  25. @RequestMapping("searchAllUser.json")
  26. @ResponseBody
  27. public List<User> searchAllUser(){
  28. List<User> users=new ArrayList<User>();
  29. User user=new User();
  30. user.setAge(5);
  31. user.setName("小米");
  32. users.add(user);
  33. User user1=new User();
  34. user1.setName("京东");
  35. user.setAge(9);
  36. users.add(user1);
  37. return users;
  38. }
  39. }

四.访问index.json

返回数据:{"name":"小米","age":5}。配置正常。

五。ajax访问数据。

定义index.html文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
  6. <script type="application/javascript" src="#springUrl('/script/index.js')"></script>
  7. <style type="text/css" >
  8. *{
  9. margin: 0;
  10. padding: 0;
  11. }
  12. .user{
  13. width: 80%;
  14. height: 30px;
  15. margin: 0 auto;
  16. }
  17. .left{
  18. float: left;
  19. width: 50%;
  20. height: 30px;
  21. }
  22. .right{
  23. float: left;
  24. width: 50%;
  25. height: 30px;
  26. }
  27. </style>
  28. <title>主页</title>
  29. </head>
  30. <body>
  31. <div class="user">
  32. </div>
  33. <div class="userInfo"></div>
  34. </body>
  35. </html>

定义index.js文件

  1. $(function () {
  2. $.ajax({
  3. type: "post",
  4. dataType: "json",
  5. url: 'index.json',
  6. success: function (data) {
  7. var htmlStr="<div class='left'>"+data.name+"</div>"
  8. htmlStr+="<div class='right'>"+data.age+"</div>"
  9. $(".user").html(htmlStr);
  10. $(".userInfo").html(data.name+"年龄:"+data.age);
  11. },
  12. error: function () {
  13. alert("请求失败");
  14. }
  15. });
  16. });

访问index.htm
界面显示:
小米 5
小米年龄:5
源代码

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注