Search code examples
springember.jsember-cli

How can I get a HTML Page from Spring using Ember. How to request a html page?


I have a login.html page in Spring. I have a controller to serve this page at port 8080. How can I get this complete page using ember?

This is my controller:

@Controller
public class LoginController {

    LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }

    @RequestMapping("/login")
    public String showJoke(Model model){

        model.addAttribute("date",loginService.getDate());

        return "login";
    }
}

This is my ember how will i display this page?

import Controller from '@ember/controller';

export default Controller.extend({

    init: function () {
        this._super(... arguments)

        let httpRequest = new XMLHttpRequest();
        httpRequest.onreadystatechange = () => {

            if(httpRequest.readyState==4 && httpRequest.status==200){
                console.log("ok");
            }

        }

        httpRequest.open("GET","http://localhost:8080/login",true);
        httpRequest.send();
    }

});

Solution

  • You can fetch the data and set it on the controller and then display it in the template like:

    app/controllers/application.js

    import Controller from '@ember/controller';
    import fetch from 'fetch';
    
    export default Controller.extend({
      htmlData: null,
    
      init: function () {
        this._super(... arguments)
        fetch('http://localhost:8080/login').then(response => {
          this.set('htmlData', response.text());
        }); 
      }
    });
    

    app/templates/application.hbs

    {{this.htmlData}}
    

    I used the fetch API here because I'm more familiar with it, but the action would be the same no matter how you pull in the data.