繁体   English   中英

通过ID Angular.js和Servlet删除并获取

[英]Delete and get by ID Angular.js and Servlet

我正在构建一个简单的Angular.js,Java HttpServlet,MongoDB Web项目。 不幸的是,由于我是Angular和HttpServlet的新手,我有一些问题。 GET(获取所有版本)和POST方法一直运行良好,从Mongo通过Java服务插入和检索到Angular UI。

但是,DELETE返回405方法不允许错误,尽管我更新了tomcat的xml文件以允许按照此站点上的其他答案进行操作。 然后,我尝试使用GET方法允许按ID检索单个类别,但尽管与现有代码几乎完全相同,但仍会出现404错误。

问题:

我需要POST到/ angular / 1这样的网址,还是POST为/ angular,因为它有效吗?

失败的getByID方法是否使DELETE无法正常工作,或者如果没有,问题是什么?

最后:为什么getByID方法遇到404?

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="ISO-8859-1"> <title>AJAX with Servlets using AngularJS</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> <script> var app = angular.module('myApp', []).controller("MyController", MyController); function MyController($scope, $http) { $scope.getDataFromServer = function() { $http({ method : 'GET', url : '/Service4/angular' }).success(function(data, status, headers, config) { $scope.category = data; }).error(function(data, status, headers, config) { }); }; $scope.deleteCategory = function() { $http({ method : 'DELETE', url : '/Service4/angular', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, data: 'id='+ $scope.deleteCat }).success(function(data, status, headers, config) { //display data, removed for brevity }).error(function(data, status, headers, config) { }); }; $scope.postDataToServer = function() { $http({ method : 'POST', url : '/Service4/angular', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, data: 'name='+ $scope.name }).success(function(data, status, headers, config) { $http({ method : 'GET', url : '/Service4/angular' }).success(function(data, status, headers, config) { $scope.category = data; }).error(function(data, status, headers, config) { }); }).error(function(data, status, headers, config) { }); }; $scope.getCatByID = function(getID) { $http({ method : 'GET', url : '/Service4/angular/' + $scope.getID }).success(function(data, status, headers, config) { $scope.category = data; }).error(function(data, status, headers, config) { }); }; }; </script> </head> <body> <div ng-app="myApp"> <div ng-controller="MyController"> <button ng-click="getDataFromServer()">Fetch data from server</button> <table> <tr ng-repeat="c in category"> <td>{{c._id}}</td> <td>{{c.name}}</td> </tr> </table> <form ng-submit="postDataToServer()"> <input ng-model="name" type="text" name="name" /> <button type="submit">Send</button> </form> <form ng-submit="deleteCategory()"> <input ng-model="delete" type="text" name="deleteCat" /> <button type="submit">Delete</button> </form> <form ng-submit="getCatByID()"> <input ng-model="getID" type="text" name="getID" /> <button type="submit">Get by ID</button> </form> </div> </div> </body> </html> 

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/angular")
public class AngularServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private int idCount = 0;
    private DB db;
    private MongoClient mongo;
    private DBCollection col; 
/**
 * @see HttpServlet#HttpServlet()
 */
public AngularServlet() {
    super();
    // TODO Auto-generated constructor stub
}

@GET
protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    //mongo cursor creation removed for brevity
    List<DBObject> all2 = cursor.toArray();

    String json = new Gson().toJson(all2);
    response.setContentType("application/json");
    response.getWriter().write(json);

    out.close();
}

@GET
@Path("/{id}")
protected void getCategoryByID(HttpServletRequest request,

HttpServletResponse response, @PathParam("id") String id) throws ServletException, IOException {
    //mongo manipulation removed for brevity
    List<DBObject> all2 = cursor.toArray();

    String json = new Gson().toJson(all2);
    response.setContentType("application/json");
    response.getWriter().write(json);

    out.close();
}

/**
 * @see HttpServlet#doDelete(HttpServletRequest request, HttpServletResponse response)
 */
@DELETE
protected void doDELETE(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");

        String id = "";
        id = request.getParameter("id");

        Category cat = new Category();
        cat.setId(Integer.parseInt(id));
        DBObject query = BasicDBObjectBuilder.start().add("_id", cat.getId()).get();
        WriteResult result = col.remove(query);

    }

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
@POST
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");

        String name = "";

        name = request.getParameter("name");

        //removed for brevity

    }

private static DBObject createDBObject(Category cat) {
    //removed for brevity
}

}

事实证明我的@Path注释被完全忽略了。 我不知道为什么,但我认为另一个问题是恰当的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM