【问题标题】:I can't connect to the database with angularjs and spring boot mvc我无法使用 angularjs 和 spring boot mvc 连接到数据库
【发布时间】: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


【解决方案1】:

spring.datasource.url=dbc:mysql 您在 jdbc 中错过“j”是否正常? 您的日志显示了什么?

【讨论】:

  • spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/filmatografia?serverTimezone=UTC 对不起,我不知道不知道为什么它没有显示 j。您能告诉我应该查看哪些日志吗?
  • 当您直接调用 Spring 项目时,您是否有错误(不是使用 Angular,而是使用 spring url -> localhost:8080/...)?同时,你能在你的 Angular 项目中打印“url”吗?
  • 嗨 jlh91!在我的 Spring Boot 项目中没有没有错误我插入 View Controller 以显示我如何打开我的主页。 '@RequestMapping(value = "/", method = RequestMethod.GET) public String viewCaseProduttrici(ModelMap caseproduttrici) { caseproduttrici.addAttribute("listCP", caseprcontr.findAll());返回“索引”; }'
猜你喜欢
  • 2018-01-06
  • 2020-09-03
  • 2018-10-09
  • 1970-01-01
  • 2019-04-12
  • 2021-02-09
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多