2016-01-07 46 views
0

我是一個初學者與春天和角。我在名爲Search的com.test.alex中創建了一個控制器類。在我的搜索類我有這個java代碼:GET與angularjs和彈簧

package com.siemens.cvc.isrules.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 


@Controller 
public class Search { 

    @RequestMapping(value = "/getHelloWord", method = RequestMethod.GET) 
    public @ResponseBody String getHelloWord(ModelMap model){ 
     String jsonData ="[{\"salut\":\"Hello Word\"}]"; 
     return jsonData; 
    } 

} 

然後,我有以下按鈕UI:

<div class="input-group-addon" ng-click="doSearch();"><span class="glyphicon glyphicon-search"></span></div> 

當我按下那個按鈕,我想提出一個要求,以「/ getHelloWord 「(搜索課程)並提醒響應。

這是angularjs控制器:

app.controller("search",function($scope,$http){ 
        $scope.doSearch = function() 
        { 
         $http({ 
          method : 'GET', 
          url : '/ISRulesTool/getHelloWord' 
         }).success(function(data,status,headers,config){ 
          alert(data); 
         }).error(function(data,status,headers,config){ 
          alert("Request Error"); 
         }); 
        }; 
}); 

和我的問題是404 「GET http://localhost:8080/ISRulesTool/getHelloWord 404(未找到)」

如果我需要獲得響應請求?

謝謝。

+0

對不起,該軟件包是com.siemens.cvc.isrules.controller –

+0

你在哪裏聲明瞭路徑/ ISRulesTool? – Eria

+2

您可以使用'@ RestController'而不是'@ Controller',並在所有方法上丟失'@ ResponseBody'。但這不是問題。請注意,在AngularJS> = 1.4.4的情況下,不建議使用.success()和.error()(請參閱https://docs.angularjs.org/api/ng/service/$http)。 – Eria

回答

0

我解決了這個問題。 我需要在web.xml

<servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

添加和創建一個新的文件調度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="com.siemens.cvc.isrules.controller" /> 
    <mvc:annotation-driven /> 
</beans> 

現在工作得很好。 Thx全部支持。

1

我認爲「url」您提到的$ http url params是錯誤的。確保給定的URL是否正確。

app.controller("search",function($scope,$http){ 
       $scope.doSearch = function() 
       { 
        $http({ 
         method : 'GET', 
         url : 'http://localhost:8080/getHelloWord' 
        }).success(function(data,status,headers,config){ 
         alert(data); 
        }).error(function(data,status,headers,config){ 
         alert("Request Error"); 
        }); 
       }; 

});

你可以像我上面提到的那樣更改網址嗎?

通過使用POSTMAN(REST客戶端 - 擴展的鉻)您可以驗證GET請求是否正常工作。

+0

沒有用。我使用http:// localhost:8080/getHelloWord網址。 –

+0

沒有。 OP使用的URL是正確的。他只是使用相對URL而不是像你在這裏提到的絕對URL – Arkantos