2017-03-04 65 views
0

我有簡單的應用AngularJS和Spring引導。我想將用戶預先填寫的整個表單傳遞給我的應用程序並註冊。通常我沒有任何問題 - 正在通過JSON obj以正確的結構和presto。不過在這裏我只是跑在cricle。 形式有效載荷發送到控制器,地址映射是正確的,但Java形式的內容爲空,發送到服務器從Angular JS發送表單到Spring

有效載荷:

{"userform":{"name":"a","surname":"a","email":"[email protected]","password":"a","confirmPassword":"a"}} 

這裏是我的html:

<div class="alert alert-danger" ng-show="error"> 
    There was a problem registering. Please try again. 
</div> 
<form role="form" name="userForm" ng-submit="register()"> 
<div class="form-group"> 
    <label for="name">Name:</label> 
    <input type="text" class="form-control" id="name" name="name" ng-model="formData.name"/> 
</div> 
<div class="form-group"> 
    <label for="name">Surname:</label> 
    <input type="text" class="form-control" id="surname" name="surname" ng-model="formData.surname"/> 
</div> 
<div class="form-group"> 
    <label for="name">Email:</label> 
    <input type="text" class="form-control" id="email" name="email" ng-model="formData.email"/> 
</div> 
<div class="form-group"> 
    <label for="password">Password:</label> 
    <input type="password" class="form-control" id="password" name="password" ng-model="formData.password"/> 
</div> 
<div class="form-group"> 
    <label for="confirmpassword">Password:</label> 
    <input type="password" class="form-control" id="confirmpassword" name="confirmpassword" 
      ng-model="formData.confirmpassword"/> 
</div> 
<button type="submit" class="btn btn-primary">Submit</button> 

這裏java腳本

app.controller('register', function ($scope, $rootScope, $http) { 
    $scope.formData = {}; 
    $scope.register = function() { 
     $http.post('/api/register', {userform: $scope.formData}).then(
      function (resposne) { 
       console.log("registered"); 
       console.log(response); 
      }, 
      function (response) { 
       console.log("Register error") 
      } 
     ) 
     } 
}); 

這裏控制器:

@RestController 
@RequestMapping("/api") 
public class ApiController { 

    @RequestMapping(value = "/register", method = RequestMethod.POST) 
    public String register(@RequestBody RegisterForm userform) { 
     System.out.println("name:" + userform.getName()); 
     System.out.println("surname:" + userform.getSurname()); 
     return "OK"; 
    } 
} 

以及最後但並非最不重要的RegisterForm:

public class RegisterForm { 
    private static final String NOT_BLANK_MESSAGE = "Cannot be blank"; 
    @NotBlank 
    @Email(message = "Not email") 
    private String email; 

    @NotBlank(message = RegisterForm.NOT_BLANK_MESSAGE) 
    private String name; 

    @NotBlank(message = RegisterForm.NOT_BLANK_MESSAGE) 
    private String surname; 

    @NotBlank(message = RegisterForm.NOT_BLANK_MESSAGE) 
    private String password; 

    @NotBlank(message = RegisterForm.NOT_BLANK_MESSAGE) 
    private String confirmpassword; 

    /*-----GETTERS AND SETTERS HERE----*/ 
} 

回答

1

您需要發送請求,這樣

$http.post('/api/register', $scope.formData) 

那麼Spring將能夠映射你的請求RegisterForm pojo。

在你的代碼春嘗試與RegisterForm類型的一個領域userform映射你的要求來上課。

+0

你是絕對正確的。謝謝你的幫助 – Khobar