0

I am developing an app where we get request from different apps ( with data) and I need to populate form in my app with those data. I do not use any database.

I want to use springboot with angular js.

This is what I tried

in my spring controller

@Slf4j
@CrossOrigin(origins = "*")
@RestController
public class ComplainsController {

  @CrossOrigin(origins = "*")
  @RequestMapping(value = "/aalcomplaint/api/complaint/lodge-complaint", method = {RequestMethod.GET, RequestMethod.POST})
  public ModelAndView lodgeComplaint(final HttpServletRequest request, ModelAndView modelAndView, final RedirectAttributes redirectAttributes) {
     modelAndView.addObject("policyNumber","12345");
    modelAndView.setViewName("redirect:/list-complains");
    return modelAndView;
  }

}

My question is how can I get modelview object in angular js application?

PS: I can get query params but I have too much data and there is limit on number of characters you can put in URL.

2
  • Possible duplicate of Accessing Spring MVC Model Object in AngularJS Commented Apr 5, 2019 at 5:05
  • NO, I do not have any database to store info. Just passing data coming application A from for post to Application B spring controller to Application B angular view Commented Apr 9, 2019 at 6:13

1 Answer 1

0

When you are dealing with client-side technologies like Angular, your server side (Java) needs to worry about is the data (usually json/xml) and how we present it in the view layer.

We will be using spring boot to expose REST APIs and angular5 to build our client that will be consuming the APIs exposed by the server.

Angular Service (ComplaintService.ts) :

import {Injectable} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable()
export class ComplaintService {

  constructor(private http:HttpClient) {}
  private url= '/aalcomplaint/api/complaint/lodge-complaint';

  public getLodgeComplaints() {
    return this.http.get<any[]>(this.url);
  }

}

Angular Component (ComplaintComponent.ts) :

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { ComplaintService } from './complaint.service';

@Component({
  selector: 'complaint-selector',
  templateUrl: './complaint.html',
  styles: []
})
export class ComplaintComponent implements OnInit {

  responseData: any[];

  constructor(private router: Router, private complaintService : ComplaintService){}

  ngOnInit() {
    this.complaintService.getLodgeComplaints()
      .subscribe( data => {
        this.responseData = data;
      });
  };

}

Complaint Component HTML Template (complaint.html) :

<div class="col-md-6">
  <h2> Lodge Complaint Details</h2>

  <table class="table table-striped">
    <thead>
    <tr>
      <th>Policy Number</th>
    </tr>
    </thead>
    <tbody>
    <tr *ngFor="let complaint of responseData">
      <td></td>
    </tr>
    </tbody>
  </table>

</div>

Spring REST Controller:

@Slf4j
@CrossOrigin(origins = "*")
@RestController
public class ComplainsController {
  @CrossOrigin(origins = "*")
  @RequestMapping(value = "/aalcomplaint/api/complaint/lodge-complaint", method = {RequestMethod.GET, RequestMethod.POST})
  public ModelAndView lodgeComplaint(final HttpServletRequest request, ModelAndView modelAndView, final RedirectAttributes redirectAttributes) {
    modelAndView.addObject("policyNumber","12345");
    modelAndView.setViewName("redirect:/list-complains");
    return modelAndView;
  }
}

Hope this helps...!

More details here: https://www.devglan.com/spring-boot/spring-boot-angular-example

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your reply. In my case html form post request comes from server 1 and hits my spring controller on server2. My spring controller redirects to angular page where again anguar page make a get to controller which in turn does redirect. Too much backwards and forwards. Also, I noticed when I put objects in model, it appends to URL and there is limit on number of characters you can put in URL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.