一、spring 版本:spring-framework-3.2.7.RELEASE

二、所需其它Jar包:

三、主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
    version="2.5">
 
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
 
    <!-- 编码过虑 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!-- Spring监听 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <!-- Spring MVC DispatcherServlet -->
    <servlet>
        <servlet-name>springMVC3</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC3</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
    <!-- 解决HTTP PUT请求Spring无法获取请求参数的问题 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <servlet-name>springMVC3</servlet-name>
    </filter-mapping>
 
 
    <display-name>UikitTest</display-name>
    <welcome-file-list>
        <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
    </welcome-file-list>
 
</web-app>

springMVC-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 
    <!-- 注解驱动 -->
    <mvc:annotation-driven />
 
    <!-- 扫描包 -->
    <context:component-scan base-package="com.citic.test.action" />
 
    <!-- 用于页面跳转,根据请求的不同跳转到不同页面,如请求index.do则跳转到/WEB-INF/jsp/index.jsp -->
    <bean id="findJsp"
        class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
 
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.do">findJsp</prop><!-- 表示index.do转向index.jsp页面 -->
                <prop key="first.do">findJsp</prop><!-- 表示first.do转向first.jsp页面 -->
            </props>
        </property>
    </bean>
 
    <!-- 视图解析 -->
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <!-- 返回的视图模型数据需要经过jstl来处理 -->
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
 
    <!-- 对静态资源文件的访问 不支持访问WEB-INF目录 -->
    <mvc:default-servlet-handler />
 
    <!-- 对静态资源文件的访问 支持访问WEB-INF目录 -->
    <!-- <mvc:resources location="/uikit-2.3.1/" mapping="/uikit-2.3.1/**" /> -->
 
     
    <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
            </list>
        </property>
    </bean>
 
    <!-- 输出对象转JSON支持 -->
    <bean id="jsonConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringConverter"/>
                <ref bean="jsonConverter" />
            </list>
        </property>
    </bean>
 
</beans>

Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.citic.test.action;
 
import java.util.ArrayList;
import java.util.List;
 
import net.sf.json.JSONObject;
 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.citic.test.entity.Person;
 
/**
 * 基于Restful风格架构测试
 
 * @author dekota
 * @since JDK1.5
 * @version V1.0
 * @history 2014-2-15 下午3:00:12 dekota 新建
 */
@Controller
public class DekotaAction {
 
    /** 日志实例 */
    private static final Logger logger = Logger.getLogger(DekotaAction.class);
 
    @RequestMapping(value = "/hello", produces = "text/plain;charset=UTF-8")
    public @ResponseBody
    String hello() {
        return "你好!hello";
    }
 
    @RequestMapping(value = "/say/{msg}", produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String say(@PathVariable(value = "msg") String msg) {
        return "{\"msg\":\"you say:'" + msg + "'\"}";
    }
 
    @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.GET)
    public @ResponseBody
    Person getPerson(@PathVariable("id"int id) {
        logger.info("获取人员信息id=" + id);
        Person person = new Person();
        person.setName("张三");
        person.setSex("男");
        person.setAge(30);
        person.setId(id);
        return person;
    }
 
    @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.DELETE)
    public @ResponseBody
    Object deletePerson(@PathVariable("id"int id) {
        logger.info("删除人员信息id=" + id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg""删除人员信息成功");
        return jsonObject;
    }
 
    @RequestMapping(value = "/person", method = RequestMethod.POST)
    public @ResponseBody
    Object addPerson(Person person) {
        logger.info("注册人员信息成功id=" + person.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg""注册人员信息成功");
        return jsonObject;
    }
 
    @RequestMapping(value = "/person", method = RequestMethod.PUT)
    public @ResponseBody
    Object updatePerson(Person person) {
        logger.info("更新人员信息id=" + person.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg""更新人员信息成功");
        return jsonObject;
    }
 
    @RequestMapping(value = "/person", method = RequestMethod.PATCH)
    public @ResponseBody
    List<Person> listPerson(@RequestParam(value = "name", required = false, defaultValue = "") String name) {
 
        logger.info("查询人员name like " + name);
        List<Person> lstPersons = new ArrayList<Person>();
 
        Person person = new Person();
        person.setName("张三");
        person.setSex("男");
        person.setAge(25);
        person.setId(101);
        lstPersons.add(person);
 
        Person person2 = new Person();
        person2.setName("李四");
        person2.setSex("女");
        person2.setAge(23);
        person2.setId(102);
        lstPersons.add(person2);
 
        Person person3 = new Person();
        person3.setName("王五");
        person3.setSex("男");
        person3.setAge(27);
        person3.setId(103);
        lstPersons.add(person3);
 
        return lstPersons;
    }
 
}

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
 
<title>Uikit Test</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="uikit-2.3.1/css/uikit.gradient.min.css">
    <link rel="stylesheet" type="text/css" href="uikit-2.3.1/addons/css/notify.gradient.min.css">
</head>
<body>
<div
    style="width:800px;margin-top:10px;margin-left: auto;margin-right: auto;text-align: center;">
    <h2>Uikit Test</h2>
</div>
<div style="width:800px;margin-left: auto;margin-right: auto;">
    <fieldset class="uk-form">
        <legend>Uikit表单渲染测试</legend>
        <div class="uk-form-row">
            <input type="text" class="uk-width-1-1">
        </div>
        <div class="uk-form-row">
            <input type="text" class="uk-width-1-1 uk-form-success">
        </div>
        <div class="uk-form-row">
            <input type="text" class="uk-width-1-1 uk-form-danger">
        </div>
        <div class="uk-form-row">
            <input type="text" class="uk-width-1-1">
        </div>
        <div class="uk-form-row">
            <select id="form-s-s">
                <option>---请选择---</option>
                <option>是</option>
                <option>否</option>
            </select>
        </div>
        <div class="uk-form-row">
            <input type="date" id="form-h-id" />
        </div>
    </fieldset>
    <fieldset class="uk-form">
        <legend>基于Restful架构风格的资源请求测试</legend>
        <button class="uk-button uk-button-primary uk-button-large" id="btnGet">获取人员GET</button>
        <button class="uk-button uk-button-primary uk-button-large" id="btnAdd">添加人员POST</button>
        <button class="uk-button uk-button-primary uk-button-large" id="btnUpdate">更新人员PUT</button>
        <button class="uk-button uk-button-danger uk-button-large" id="btnDel">删除人员DELETE</button>
        <button class="uk-button uk-button-primary uk-button-large" id="btnList">查询列表PATCH</button>
    </fieldset>
</div>
 
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="uikit-2.3.1/js/uikit.min.js"></script>
<script type="text/javascript" src="uikit-2.3.1/addons/js/notify.min.js"></script>
<script type="text/javascript">
    (function(window,$){
 
        var dekota={
             
            url:'',
 
            init:function(){
                dekota.url='<%=basePath%>';
                $.UIkit.notify("页面初始化完成", {status:'info',timeout:500});
                $("#btnGet").click(dekota.getPerson);
                $("#btnAdd").click(dekota.addPerson);
                $("#btnDel").click(dekota.delPerson);
                $("#btnUpdate").click(dekota.updatePerson);
                $("#btnList").click(dekota.listPerson);
            },
            getPerson:function(){
                $.ajax({
                    url: dekota.url + 'person/101/',
                    type: 'GET',
                    dataType: 'json'
                }).done(function(data, status, xhr) {
                    $.UIkit.notify("获取人员信息成功", {status:'success',timeout:1000});
                }).fail(function(xhr, status, error) {
                    $.UIkit.notify("请求失败!", {status:'danger',timeout:2000});
                });
            },
            addPerson:function(){
                $.ajax({
                    url: dekota.url + 'person',
                    type: 'POST',
                    dataType: 'json',
                    data: {id: 1,name:'张三',sex:'男',age:23}
                }).done(function(data, status, xhr) {
                    $.UIkit.notify(data.msg, {status:'success',timeout:1000});
                }).fail(function(xhr, status, error) {
                    $.UIkit.notify("请求失败!", {status:'danger',timeout:2000});
                });
            },
            delPerson:function(){
                $.ajax({
                    url: dekota.url + 'person/109',
                    type: 'DELETE',
                    dataType: 'json'
                }).done(function(data, status, xhr) {
                    $.UIkit.notify(data.msg, {status:'success',timeout:1000});
                }).fail(function(xhr, status, error) {
                    $.UIkit.notify("请求失败!", {status:'danger',timeout:2000});
                });
            },
            updatePerson:function(){
                $.ajax({
                    url: dekota.url + 'person',
                    type: 'POST',//注意在传参数时,加:_method:'PUT' 将对应后台的PUT请求方法
                    dataType: 'json',
                    data: {_method:'PUT',id: 221,name:'王五',sex:'男',age:23}
                }).done(function(data, status, xhr) {
                    $.UIkit.notify(data.msg, {status:'success',timeout:1000});
                }).fail(function(xhr, status, error) {
                    $.UIkit.notify("请求失败!", {status:'danger',timeout:2000});
                });
            },
            listPerson:function(){
                $.ajax({
                    url: dekota.url + 'person',
                    type: 'POST',//注意在传参数时,加:_method:'PATCH' 将对应后台的PATCH请求方法
                    dataType: 'json',
                    data: {_method:'PATCH',name: '张三'}
                }).done(function(data, status, xhr) {
                    $.UIkit.notify("查询人员信息成功", {status:'success',timeout:1000});
                }).fail(function(xhr, status, error) {
                    $.UIkit.notify("请求失败!", {status:'danger',timeout:2000});
                });
            }
        };
        window.dekota=(window.dekota)?window.dekota:dekota;
        $(function(){
            dekota.init();
        });
    })(window,jQuery);
 
</script>
</body>
</html>

部分调试效果:

restful 风格 加上springmvc的更多相关文章

  1. Restful风格的springMVC配搭ajax请求的小例子

    1. GET请求的例子 ajax代码: 请求参数拼接在url后面(参数在服务器可通过HttpServletRequest获取,也可以直接通过@RequestParam自动注入,参考DELETE例子的方 ...

  2. MockMVC - 基于RESTful风格的Springboot,SpringMVC的测试

    MockMVC - 基于RESTful风格的SpringMVC的测试 对于前后端分离的项目而言,无法直接从前端静态代码中测试接口的正确性,因此可以通过MockMVC来模拟HTTP请求.基于RESTfu ...

  3. Spring Boot构建 RESTful 风格应用

    Spring Boot构建 RESTful 风格应用 1.Spring Boot构建 RESTful 风格应用 1.1 实战 1.1.1 创建工程 1.1.2 构建实体类 1.1.4 查询定制 1.1 ...

  4. SpringMVC 构建Restful风格 及问题处理

    基本的请求URL: /person/{id}  GET  得到id的person /person POST      新增person /person/{id}  PUT  更新id的person / ...

  5. Restful风格API接口开发springMVC篇

    Restful风格的API是一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机 ...

  6. SpringMVC实现Restful风格的WebService

    1.环境 JDK7 MyEclipse2014 tomcat8 maven 3.3.3 spring4.1.4 2.创建maven工程 使用MyEclipse创建maven工程的方式可以参考这篇博文( ...

  7. [五]SpringMvc学习-Restful风格实现

    1.Restful风格的资源URL 无后缀资源的访问(csdn用法) 2.SpringMvc对Rest风格的支持 2.1将 /*.do改为/ 2.2 3.@PathVariable获取Url变量 @R ...

  8. SpringMvc笔记-对RESTFUL风格的配置

    1.@RequestMapping注解可以使用如下参数: 1,params:例如params={'username',"age!=100"}表示需要usernmame并且age 属 ...

  9. springMVC中添加restful 风格

    RESTful架构:是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 关于 ...

随机推荐

  1. codeforces 703D D. Mishka and Interesting sum(树状数组)

    题目链接: D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megaby ...

  2. linux 进程学习笔记-进程退出/终止进程

    <!--[if !supportLists]-->Ÿ <!--[endif]-->退出/终止进程 void _exit(int status) 与 void exit(int ...

  3. linux命令学习笔记-md5sum 命令详解

    前言 在网络传输.设备之间转存.复制大文件等时,可能会出现传输前后数据不一致的情况.这种情况在网络这种相对更 不稳定的环境中,容易出现.那么校验文件的完整性,也是势在必行的. 使用说明 md5sum命 ...

  4. 基于OpenCV的面部交换

    需要装python库 OpenCV dlib docopt(根据打开方式选择是否装) # -*- coding: UTF-8 #本电脑试运行 命令 python F:\python_project\s ...

  5. poj 2689Prime Distance(区间素数)埃氏筛法

    这道题的L和R都很大,所以如果直接开一个1~R的数组明显会超时.但是R-L并不大,所以我们考虑把这个区间(L--R)移动到(1--(R-L+1))这个区间再开数组(就是把每个数减L再加1).接下来先用 ...

  6. 【转】python的复制,深拷贝和浅拷贝的区别

    在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用 一般有三种方法, alist=[1,2,3,[& ...

  7. nodejs写文件

    var fs = require('fs'); 2 var txt = "以上程序使用fs.readFileSync从源路径读取文件内容,并使用fs.writeFileSync将文件内容写入 ...

  8. Django 框架

    MVC 框架和MTV框架  (了解即可) MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控 ...

  9. UVa 10534 Wavio Sequence (LIS+暴力)

    题意:给定一个序列,求一个最长子序列,使得序列长度为奇数,并且前一半严格递增,后一半严格递减. 析:先正向和逆向分别求一次LIS,然后再枚举中间的那个数,找得最长的那个序列. 代码如下: #pragm ...

  10. UniqueIdentifier 数据类型

    UniqueIdentifier 数据类型用于存储GUID的值,占用16Byte. SQL Server将UniqueIdentifier存储为16字节的二进制数值,Binary(16),按照特定的格 ...