@slientStrom
        
        2017-08-07T09:19:29.000000Z
        字数 7751
        阅读 800
    maven,spring,velocity,restful 
此次整合没有使用数据库。只包含controller层和实体类。 
项目结构
<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>com.exapme</groupId><artifactId>springMvc</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>springMvc Maven Webapp</name><url>http://maven.apache.org</url><properties><springVersion>4.3.6.RELEASE</springVersion><jacksonVersion>2.8.7</jacksonVersion></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!--spring的基本包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springVersion}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${springVersion}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${springVersion}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${springVersion}</version></dependency><!-- veloctiy 基本包--><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><!--json包 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jacksonVersion}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>${jacksonVersion}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jacksonVersion}</version></dependency></dependencies><build><finalName>springMvc</finalName></build></project>
<?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:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.learn"></context:component-scan><mvc:annotation-driven /></beans>
<?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:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.learn"></context:component-scan><mvc:annotation-driven /><!-- 将ResponseBody(rest返回的对象转换为json)--><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><property name="messageConverters"><list><bean class="org.springframework.http.converter.StringHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html; charset=UTF-8</value><value>application/json;charset=UTF-8</value></list></property></bean><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html; charset=UTF-8</value><value>application/json;charset=UTF-8</value></list></property></bean></list></property></bean><bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"><property name="resourceLoaderPath" value="/WEB-INF/pages/"/><property name="configLocation" value="classpath:velocity.properties"/></bean><!-- 配置视图的显示 --><bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"><property name="suffix" value=".html" /><!-- 视图文件的后缀名 --><property name="contentType" value="text/html;charset=UTF-8" /></bean></beans>
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:context/context.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:context/context-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>*.htm</url-pattern></servlet-mapping><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>*.json</url-pattern></servlet-mapping></web-app>
package com.learn.controller;import com.learn.controller.res.UserRes;import com.learn.entity.User;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.ArrayList;import java.util.List;@Controllerpublic class UserController {//视图跳转@RequestMapping("index.htm")public String homeView(){return "index";}//数据@RequestMapping("index.json")@ResponseBodypublic User getUser(){User user=new User();user.setName("小米");user.setAge(5);return user;}@RequestMapping("searchAllUser.json")@ResponseBodypublic List<User> searchAllUser(){List<User> users=new ArrayList<User>();User user=new User();user.setAge(5);user.setName("小米");users.add(user);User user1=new User();user1.setName("京东");user.setAge(9);users.add(user1);return users;}}
返回数据:{"name":"小米","age":5}。配置正常。
定义index.html文件
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script><script type="application/javascript" src="#springUrl('/script/index.js')"></script><style type="text/css" >*{margin: 0;padding: 0;}.user{width: 80%;height: 30px;margin: 0 auto;}.left{float: left;width: 50%;height: 30px;}.right{float: left;width: 50%;height: 30px;}</style><title>主页</title></head><body><div class="user"></div><div class="userInfo"></div></body></html>
定义index.js文件
$(function () {$.ajax({type: "post",dataType: "json",url: 'index.json',success: function (data) {var htmlStr="<div class='left'>"+data.name+"</div>"htmlStr+="<div class='right'>"+data.age+"</div>"$(".user").html(htmlStr);$(".userInfo").html(data.name+"年龄:"+data.age);},error: function () {alert("请求失败");}});});
访问index.htm 
界面显示: 
小米   5 
小米年龄:5 
源代码