【发布时间】:2021-04-24 01:40:30
【问题描述】:
我正在尝试将 AngularJS 集成到我的 Spring Boot MVC 项目中,但我不明白如何通过 @RequestMapping 和 @GetMapping 将其连接到 Spring Boot。
当我在本地获取项目时,我在 Chrome 谷歌开发者工具上没有检测到任何错误。
这是我的控制器
var main = angular.module('main', []);
main.component('main', {
templateUrl: 'views/main.template.jsp',
});
main.controller('CaseJson', function($scope, $http) {
$http.get('caseprod/all')
.then(function(response, status) {
$scope.myWelcome = response.data;
}, function(err) {
console.error("Error", err);
});
});
main.controller('getcontroller', function($scope, $http, $location) {
$scope.getfunction = function(){
var url = $location.absUrl() + "caseprod/all";
$http.get(url).then(function (response) {
$scope.response = response.data
}, function error(response) {
$scope.postResultMessage = "Error with status: " + response.statusText;
});
}
})
我尝试了两个控制器 这是它应该显示的地方:
<div data-ng-controller="getcontroller">
<div var="current" data-ng-repeat="case in response">
<div class="container">
<div class="img-container">
<img data-ng-src="{{data.img}}" alt="">
</div>
<div class="user-info">
<h2>"{{data.nome}}"</h2>
<span>"{{data.annoFondazione}}"</span>
</div>
</div>
</div>
</div>
这里是控制器弹簧
@RestController
@RequestMapping("/caseprod")
public class CaseProduttriciController {
@Autowired
private CaseProduttriciService caseprodservice;
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
public List<CaseProduttrici> findAll(){
return caseprodservice.findAll();
}
如果你需要别的东西,请告诉我。谢谢
【问题讨论】:
-
您需要使用完整的 Spring Boot 资源 URL,包括主机名,例如localhost:8080/caseprod/all 作为 Spring Boot 在嵌入式 Apache 服务器上运行
-
请使用通用语言 - 英语有助于理解
-
对不起,Nitesh。我尝试使用 $ http.get ('localhost: 8080 / caseprod / all') 并在第二个控制器中使用 var url = "localhost: 8080 / caseprod / all";但它在 Google 开发者工具上没有显示任何错误
-
你检查你的服务器端口了吗?真的是8080吗?你的 application.properties 怎么说?
-
如果 application.properties 中没有 server.port 的条目,则添加一个进行自定义,如 server.port=8081 为 默认情况下,Spring Boot 使用 8080 端口号启动 Tomcat
标签: angularjs spring-boot spring-mvc