2014-12-04 54 views
2

我必须在使用servlet的项目中编写控制器。我之前做过,但我从未与AngularJS合作过,所以我通过request.setAttribute()request.getParameter()完成了它,并将Java代码放入JSP页面中。但是现在前端开发者使用了AngularJS,我必须给他返回一个JSON对象。我不知道该怎么做。下面是abTestCtrl.js代码:如何使用Java Servlet将JSON对象返回到AngularJS

app.controller("abTestCtrl", function($scope, $location, $http) { 
     $scope.title = "no title"; 
     $scope.description = "no description"; 
    $scope.getParam = $location.search()['id']; 
    if($scope.getParam === undefined)$scope.getParam = 0; 

    //$scope.getParam=2; 
    //path: localhost8080/UIUM.../servlet-name.java 
     //with two ids 
     //web.xml: serverlet mapping for the path 
     if($scope.getParam==='0'||$scope.getParam === 0){ 
      var saveButton = document.getElementById("saveButton"); 
      saveButton.classList.remove("hidden"); 
     } 
     else{ 
      $http.get('http://localhost:8080/UIUM_IMT4003/ABTestController', {command:'getTestCaseInfo', testcaseID:$scope.getParam}). 
      success(function(data, status, headers, config) { 
       // this callback will be called asynchronously 
       // when the response is available 
       console.log('request succesful'); 
       console.log(data); 
       console.log(status); 
       console.log(headers); 
       console.log(config); 
      }). 
      error(function(data, status, headers, config) { 
       // called asynchronously if an error occurs 
       // or server returns response with an error status. 
       console.log('request not succesful'); 
      }); 
     } 

和我从servlet processRequest()代码:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException { 
     response.setStatus(HttpServletResponse.SC_OK); 
     response.setContentType("application/json; charset=UTF-8"); 
     //PrintWriter printout = response.getWriter(); 

     JSONObject jObject = null; 
     RequestDispatcher view = null; 
     TestcaseRepository testcaseRepo = new TestcaseRepository(); 

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

     if(command == null) 
     { 
      view = request.getRequestDispatcher("/testcases.jsp"); 
      view.forward(request, response); 
     } 

     if(command.equals("getTestCaseInfo")){ 
      String testcaseId = request.getParameter("testcaseID"); 
      Testcase testcase = testcaseRepo.getTestcaseById(testcaseId); 
      jObject = new JSONObject(); 
      jObject.put("id", testcaseId); 
      jObject.put("title", testcase.getTestcaseName()); 
      jObject.put("testscenario", testcase.getTestcaseDescription()); 
//   printout.print(jObject); 
//   printout.flush(); 
      jObject.write(response.getWriter()); 
     }  

能否请你帮我处理此请求,最后返回这个可怜的JSON!

顺便说一句,Servlet不识别command参数。它得到null。但是在AngularJS函数中有这样的参数。

+0

帮助?你只是倾销一些代码而不会说出什么问题。 – Gimby 2014-12-04 15:52:55

+0

如果您遇到错误,请发布错误消息/堆栈跟踪,详细说明预期的内容和发生的情况。顺便说一句,我会用JSON转换器如果我是你。 – Jimmy 2014-12-04 15:56:11

+0

@Gimby引用:“但现在前端开发人员使用AngularJS,我必须给他返回一个JSON对象,而我不知道该怎么做。”对我来说,看起来很明显,我无法将JSON对象返回给AngularJS。 – mityakoval 2014-12-05 16:26:06

回答

1

尝试使用javax.json.JsonObject如下:

JsonObject jo=Json.createObjectBuilder() 
      .add("id", testcaseId) 
      .add("title", testcase.getTestcaseName()) 
      .add("testscenario", testcase.getTestcaseDescription()).build(); 

然后设置响应内容类型为JSON并在响应发送您的JSON对象:什么

response.setContentType("application/json");// set content to json 
PrintWriter out = response.getWriter(); 
out.print(jo); 
out.flush();