1. 程式人生 > >用Srpingmvc 建立RESTful web service

用Srpingmvc 建立RESTful web service

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path ;
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="<%=basePath%>/resources/js/jquery-2.1.4.min.js"></script>
    <title></title>
    <script type="text/javascript">
        $().ready(function(){
            $.ajax({
                url: "<%=basePath%>/user/caspar",
                type:"PUT"
            }).done(function(data) {
                $("#contents").append("<span>put "+data['name']+"</span><br></br>");
            });

            $.ajax({
                url: "<%=basePath%>/user/caspar",
                type:"PATCH"
            }).done(function(data) {
                $("#contents").append("<span>update "+data['name']+"</span><br></br>");
            });

            $.ajax({
                url: "<%=basePath%>/user/caspar",
                type:"DELETE"
            }).done(function(data) {
                $("#contents").append("<span>delete "+data['name']+"</span><br></br>");
            });

            $.ajax({
                url: "<%=basePath%>/user/caspar",
                type:"GET"
            }).done(function(data) {
                $("#contents").append("<span>get "+data['name']+"</span><br></br>");
            });

            $.ajax({
                url: "<%=basePath%>/user?limit=10&sortby=name&order=asc",
                type:"GET"
            }).done(function(data) {
                for(var i=0; i<data.length; i++) {
                    $("#contents").append("<span>list "+i+": "+data[i]['name']+"</span><br></br>");
                }
            });
        })

    </script>
</head>
<body>
<h1>Hello ${name} !</h1>
<div id="contents">

</div>

</body>
</html>