java - Thymeleaf 没有将我的控制器方法映射到 html 模板
问题描述
我试图在加载我的一个 html 模板时从我的数据库中加载对象。目前,它们没有加载,我的控制器方法都没有工作。我不知道这是 pom 问题还是我的终端缺少接线...附加我的 html、控制器、应用程序和 pom。
谢谢你的帮助!
聚甲醛
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sg</groupId>
<artifactId>SuperHeroSightings</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SuperHeroSightings</name>
<description>SuperHero Sightings Assessment</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<version>2.44.0</version>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<scope>test</scope>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
HTML
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Heroes</title>
</head>
<body>
<!-- Main Body of Page -->
<div class="container">
<div class="row m-4">
<div class="col text-center border border-dark">
<h1><a href="/" class="text-dark">Heroes</a></h1>
</div>
</div>
<div class="row m-4 border border-dark">
<div class="col text-center m-3">
<a href="home.html" class="btn btn-outline-primary btn-lg">Home</a>
</div>
<div class="col text-center m-3">
<a href="students" class="btn btn-outline-primary btn-lg">Search</a>
</div>
</div>
<div class="col-md-9">
<div id="tableDiv">
<table id="objectTable" class="table table-hover">
<tbody id="contentRows"></tbody>
</table>
</div>
</div>
<div class="row m-4 border border-dark">
<div class="col-3 text-center m-3">
<span class="h3">Add Hero</span>
</div>
<div class="col-7 text-center m-3">
<form action="addHero" method="POST">
<div class="form-group row">
<label for="name" class="col-3 col-form-label">
Name</label>
<div class="col-9">
<input type="text" name="name"
id="name" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label for="description" class="col-3 col-form-label">
Description</label>
<div class="col-9">
<input type="text" name="description"
id="description" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label for="Superpower" class="col-3 col-form-label">
Superpower</label>
<div class="col-9">
<select id="superpower" name="superpowerId" class="form-control" >
<option th:each="superpower : ${Superpowers}"
th:value="${superpower.id}"
th:text="${superpower.name}">
Superpower</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="organizations" class="col-3 col-form-label">
Organizations</label>
<div class="col-9">
<select multiple id="organizations" name="organizationId"
class="form-control" >
<option th:each="organization : ${organizations}"
th:value="${organization.id}"
th:text="${organization.name}">
organization</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary">Add Hero</button>
</form>
</div>
</div>
<div class="row m-4 border border-dark">
<div class="col text-center m-3">
<table class="table table-striped">
<thead>
<tr>
<th>Hero</th>
<th>Superpower</th>
<th>Description</th>
<th>Details</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="hero : ${heroes}">
<td th:text='${hero.heroName}'>Hero Name</td>
<td th:text="${hero.superPower}">Hero Superpower</td>
<td th:text="${hero.heroDescription}">Hero Description</td>
<td><a href="#" th:href="@{/courseDetail(id=${course.id})}">Details</a></td>
<td><a href="#" th:href="@{/editCourse(id=${course.id})}">Update</a></td>
<td><a href="#" th:href="@{/deleteCourse(id=${course.id})}">Delete</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- End main body -->
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="js/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>;
控制器
package com.sg.SuperHeroSightings.controller;
import com.sg.SuperHeroSightings.dao.HeroDao;
import com.sg.SuperHeroSightings.dao.OrganizationDao;
import com.sg.SuperHeroSightings.dao.SuperpowerDao;
import com.sg.SuperHeroSightings.dto.Hero;
import com.sg.SuperHeroSightings.dto.Organization;
import com.sg.SuperHeroSightings.dto.Superpower;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
/**
*
* @author Chelsea, Karma, Mohammed, Patrick
*/
@Controller
public class HeroController {
@Autowired
SuperpowerDao superpowerDao;
@Autowired
OrganizationDao organizationDao;
@Autowired
HeroDao heroDao;
@GetMapping("/heroes")
public String displayHeroes(Model model) {
List<Hero> heroes = heroDao.getAllHeroes();
List<Superpower> superpowers = superpowerDao.getAllSuperpowers();
List<Organization> organizations = organizationDao.getAllOrganizations();
model.addAttribute("heroes", heroes);
model.addAttribute("superpowers", superpowers);
model.addAttribute("organizations", organizations);
return "heroes";
}
应用程序运行器
package com.sg.SuperHeroSightings;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SuperHeroSightingsApplication {
public static void main(String[] args) {
SpringApplication.run(SuperHeroSightingsApplication.class, args);
}
}
更新:
当我在浏览器上加载英雄时,我得到一个找不到文件的错误。
更新:这是我的文件结构: 包
更新:这是我的应用程序属性,其中登录信息已被编辑。
应用特性
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/SuperHeroDB?serverTimezone=America/Chicago&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=[redacted]
spring.datasource.password=[redacted]
spring.profiles.active=database
解决方案
Thymeleaf 并没有让我失望,我直接从我的计算机访问该文件,而不是在我的浏览器中访问 localhost:8080/heroes。我对http请求很陌生,所以请原谅我
推荐阅读
- django - 根据 django ORM 中的喜欢和评论将帖子划分为动态类别
- c++ - 如何使用地图对文本进行编码?
- json - 如何使用 SQL 确定父对象中 JSON 对象的数量
- windows - Autohotkey (ahk) GUI 十字准线实现
- c# - 在 MVC 中设置数据绑定 Dropdownlist 的默认值
- flutter - connectedResult 在颤振应用程序的启动中返回null?
- rpa - UIPath - 没有 Orchestrator 的计划
- reactjs - 在父组件中反应更新状态
- image-processing - 如何使用 Leptonica 进行扩张
- python - Matplotlib 散布不同的图像 (MNIST) 而不是 TSNE 的绘图