Guest User

Untitled

a guest
Dec 4th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package com.central.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.*;
  7. import com.central.model.Users;
  8. import com.central.repository.UsersRepository;
  9. import com.fasterxml.jackson.core.JsonProcessingException;
  10. import com.fasterxml.jackson.databind.ObjectMapper;
  11.  
  12. @RestController
  13. public class WebController
  14. {
  15. private static final String NULL = null;
  16. @Autowired
  17. UsersRepository userRepo;
  18.  
  19. @CrossOrigin(origins = "http://localhost:4200")
  20. @RequestMapping("/checkUsers")
  21. public String checkLogin() throws JsonProcessingException {
  22.  
  23. ObjectMapper mapper = new ObjectMapper();
  24. List<Users> useObj = (List<Users>) userRepo.findAll();
  25. return(mapper.writeValueAsString(useObj));
  26. }
  27.  
  28. @CrossOrigin(origins = "http://localhost:4200")
  29. @RequestMapping(value = "/checkByCredential", method = RequestMethod.POST)
  30. public String checkLoginByName(@RequestBody Users user) throws Exception{
  31.  
  32. ObjectMapper mapper = new ObjectMapper();
  33. Users useObj1 = userRepo.findByUsernameAndPassword(user.username,user.password);
  34. return(mapper.writeValueAsString(useObj1));
  35. }}
  36.  
  37. import { BrowserModule } from '@angular/platform-browser';
  38. import { NgModule } from '@angular/core';
  39.  
  40. import { HttpModule } from '@angular/http';
  41. import { FormsModule } from '@angular/forms';
  42.  
  43. import { AppComponent } from './app.component';
  44. import { BasicService } from './basic.service';
  45.  
  46.  
  47. @NgModule({
  48.  
  49. declarations : [ AppComponent],
  50. imports : [BrowserModule,HttpModule,FormsModule ],
  51. providers : [BasicService],
  52. bootstrap : [AppComponent]
  53.  
  54. })
  55.  
  56. export class AppModule { }
  57.  
  58. import { Component } from '@angular/core';
  59. import { BasicService } from './basic.service';
  60.  
  61. @Component({
  62. selector: 'app-root',
  63. template: `<h1>{{ title }}</h1>
  64. <div style="margin-left:50px; height: 200px; overflow: auto;">
  65. <table>
  66. <tr><td>Username</td>&emsp;&emsp;<td>Password</td></tr>
  67. <tr *ngFor= " let item of data">
  68. <td>{{ item.username }}</td>&emsp;&emsp;<td>{{ item.password }}</td>
  69. </tr>
  70. </table>
  71. </div>
  72.  
  73.  
  74. <h2>Login</h2>
  75. <form role="form">
  76. <div ng-control-group="credentials">
  77. <label for="username">Username</label>
  78. <input
  79. type="text"
  80. #username
  81. id="username"
  82. ng-control="username"
  83. required>
  84.  
  85. <label for="password">Password</label>
  86. <input
  87. type="password"
  88. #password
  89. id="password"
  90. ng-control="password"
  91. required>
  92. </div>
  93. <button (click)="checkByCredential(username,password)">Login</button>
  94. </form>`
  95. })
  96.  
  97.  
  98. export class AppComponent {
  99. title :string;
  100. data:any;
  101.  
  102.  
  103. constructor(private MyService: BasicService){
  104. this.title="Angular Service";
  105.  
  106. this.MyService.GetUsers()
  107. .subscribe(users => {this.data=users });
  108. }
  109.  
  110. checkByCredential(username: string, password: string) {
  111. this.MyService.checkByCredential(username, password).subscribe(users => {this.data=users });
  112. }
  113. }
  114.  
  115. import { Injectable } from '@angular/core';
  116. import { Http } from '@angular/http';
  117. import 'rxjs/add/operator/map';
  118.  
  119.  
  120. @Injectable()
  121. export class BasicService {
  122. constructor(private http:Http) { }
  123.  
  124.  
  125. GetUsers(){
  126. return this.http.get('http://localhost:8080/checkUsers')
  127. .map(result => result.json());
  128. }
  129.  
  130.  
  131. checkByCredential(username: string, password: string) {
  132. const user = { username: username, password: password };
  133. return this.http
  134. .post('http://localhost:8080/checkByCredential', user)
  135. .map(result => result.json());
  136. } }
Add Comment
Please, Sign In to add comment