如何从AngularJS控制器向Spring Controller发送请求?

时间:2016-10-27 06:12:41

标签: angularjs spring-mvc request-mapping

我正在使用AngularJS在Spring 4中使用Hibernate 5制作SPA。 我无法从AngularJS控制器向Spring Controller发送请求。

执行成功进入submit()函数,但它失败了。

浏览器控制台中显示错误:

POST http://localhost:8050/Spring_Hibernate_MVC/Views/registerStudent 404 (Not Found)

我的项目结构如下所示。

Spring_Hibernate_MVC
=src
-com->karmesh->mvcApp->controller->register->RegisterController.java
=WebContent
-js->app->RegisterController.js
-Views->Register.html

Register.html

<div id="DivRegisterMain" ng-controller="RegisterController as ctrl">

    <form name="myForm" novalidate>
    :
    :
    :
    <input type="submit" value="Submit" ng-click="submit()" ><br>
</form>
</div>

app.js

var routeApp=angular.module("RouteApp",['ngRoute']);

RegisterController.js

routeApp.controller("RegisterController", function($scope, $http) {
:::://some code is here
$scope.submit = function() {

        var req = {
                 method: 'POST',
                 url: '/registerStudent',               
                 data: $scope.studentList
                };

        $http(req).
        then(function(response){
            console.log(response.status);
            console.log("in success");
            $scope.studentList=[];
        }, function(response){
            console.log(response.status);
            console.log("in fail");     
        });
    };
});

的web.xml

<servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

RegisterController.java

@RestController
@RequestMapping("/")
public class RegisterController {

    @Autowired
    private RegisterService registerService;


    @RequestMapping(value="/registerStudent", method = RequestMethod.POST)  
    public ResponseEntity<RegisterDTO> registerStudent(@RequestBody List<RegisterDTO> stdList) {    


        if (registerService.isStudentExist(stdList)) {
            return new ResponseEntity<RegisterDTO>(HttpStatus.CREATED);
        }

        return new ResponseEntity<RegisterDTO>(HttpStatus.CONFLICT);

    }
}

1 个答案:

答案 0 :(得分:0)

[编辑添加&#34; .do&#34;,更改方法返回,添加参数检查]

您正在申请错误的网址。

替换

http://localhost:8050/Spring_Hibernate_MVC/Views/registerStudent 

http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do //notice I removed /views/ and added .do

如果连第二个都不起作用,请尝试

http://localhost:8050/springServlet/registerStudent.do 

因此,在角度控制器中,将URL更改为如下所示

var req = {
        method: 'POST',
        url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',//notice the .do at the end ro try http://localhost:8050/springServlet/registerStudent.do             
        data: $scope.studentList
};

RegisterController

@Controller  //LOOK here
@RequestMapping("/")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @ResponseBody  //LOOK here
    @RequestMapping(value="/registerStudent", method = RequestMethod.POST)  
    public boolean registerStudent(@RequestBody List<RegisterDTO> stdList) {    
       if (stdList != null) {   //check if the object received is not null
         //store your stdList somehow
      }
      return registerService.isStudentExist(stdList);
    }
}

你的AngularController

$scope.submit = function() {
var req = {
        method: 'POST',
        url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',               
        data: $scope.studentList
};

$http(req).then(function(response){
    console.log(response); // prints true or false
    if (response)
      console.log("in success");
    else 
       console.log("in fail");
    $scope.studentList=[];
    }, function(error){
     console.log("in fail");     
    });
};

});