将参数从Angular2发送到spring boot

时间:2017-04-24 16:19:58

标签: spring-boot angular2-template angular2-forms angular2-services angular2-directives

我的控制器中有一个Web服务,它接受三个字符串参数。它工作正常(使用Postman测试)。我有一个表格,它将从angular2.Here' s发送thees参数我的控制器:     `@RestController 公共类RegisterAPI {

@Autowired
RegisterMetier registerMetier;


@RequestMapping(value="/register",method = RequestMethod.POST)
public void register(@RequestParam("userName") String userName,@RequestParam("password") String password,@RequestParam("email") String email){
        registerMetier.save(userName, email, password);
}`

现在我的typeScript代码(服务):     @Injectable() export class AddThreshold { constructor ( private http: Http ) {} add(userName:any,password:any,email:any) { const body=JSON.stringify({ userName:userName, password:password,email:email }); console.log('9raaahom'); const headers = new Headers({ 'Content-Type': 'application/json'}); headers.append('Accept', 'application/json'); let options = new RequestOptions({ headers: headers,withCredentials : true }); return this.http.post( http://localhost:8080/register ,body,options) .map((data:Response) => data.status); } 对于我的组件:     `@Component({

moduleId: module.id,
selector: 'app-signup',
templateUrl: './signup.component.html',
providers: [AddThreshold],
styleUrls: ['./signup.component.scss']

}) 导出类SignupComponent实现OnInit {

    angular = false;
    x={};
    y={};
     constructor(
         private AddClass: AddThreshold
         ) {}
      ngOnInit()
      {
      }
      submit (userName:any,password:any,email:any)
     {
         console.log(userName);console.log(password);console.log(email);   

        this.AddClass.add(userName,password,email)
          .subscribe(data => console.log("here"+data));

         console.log("end add ")
         ;
     }

} `

和我的webPageComponent:     `                                        

SB Admin BS 4 Angular2

                                                                                                

                <div class="form-group">
                    <input type="text" class="form-control input-underline input-lg" id="" placeholder="Email" #email >
                </div>

                <div class="form-group">
                    <input type="password" class="form-control input-underline input-lg" id="" placeholder="Password" #password>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control input-underline input-lg" id="" placeholder="Repeat Password">
                </div>
            </div>
            <a class="btn rounded-btn" (click)="submit(userName.value,email.value,password.value)"  > Register </a>&nbsp;
            <a class="btn rounded-btn" [routerLink]="['/login']"> Log in </a>
        </form>
    </div>
</div>

`

0 个答案:

没有答案