我在Spring有一个登录.html页面。我有一个控制器在端口 8080 上为这个页面提供服务。如何使用余烬获取此完整页面?
这是我的控制器:
@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";
}
}
这是我的余烬,我将如何显示此页面?
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();
}
});
您可以获取数据并将其设置在控制器上,然后将其显示在模板中,如下所示:
应用/控制器/应用程序.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}}
我在这里使用了 fetch API,因为我更熟悉它,但无论您如何提取数据,操作都是相同的。