Java Spring simple HTML Link



我对Spring Webapplication相当陌生,需要为大学做一个小项目。目前,我有一个简单的html站点,其中显示了一些我手动插入到数据库中的数据。

现在我目前正在处理一个新的 html 文件,以通过单独的表单插入数据。 在我的"主页"页面上,我有一个导航栏,我想在其中单击一个项目并重定向到特定页面。 我尝试将链接直接重定向到我的资源文件夹中的 html 文件,但它似乎不起作用。

<li class="nav-item active">
<a class="nav-link" href="/inputbook.html">Add Book <span class="sr-only">(current)</span></a>
</li>

我还尝试通过以下方式链接它:

  • "${pageContext.servletContext.contextPath}/inputbook.html"(在另一个线程中找到(
  • "./输入簿.html">
  • "../输入簿.html">

有没有办法简单地链接此页面,或者我是否需要在控制器中进行操作 + 方法?

谢谢!

更新:

刚刚发生了一些间歇性的事情。我添加了在控制器中映射站点的方法。 当我尝试通过导航栏打开此站点时,它告诉我它未映射。现在(为了测试,我的主页中也有"inputbook.html"文件中的表单(当我通过主页表单输入数据时,它会将其保存到数据库中并正确显示。完成此过程后,当我再次单击导航栏时,它会毫无问题地打开"输入簿.html"站点?

控制器:

@RequiredArgsConstructor
@Controller
@RequestMapping("/books")
public class BookController {
private final BookService bookService;
private final BookRepository bookRepository;
@GetMapping
public String showBooks(Model model) {
List<Book> books = bookService.findAll();
model.addAttribute("books",books);
return "books"; //view name
}
@PostMapping(value="/search")
public String serachByTitle(Model model, @RequestParam Optional<String> searchTerm) {//Parameter heißt wie Feld in html form
List<Book> books = searchTerm.map(bookService::findByTitleLike)
.orElseGet(bookService::findAll);
model.addAttribute("searchTerm",searchTerm.get());
model.addAttribute("books",books);
return "books";
}
@GetMapping("inputbook.html")
public String inputbook() {
return "inputbook"; // this returns the template name to be rendered from resources/templates. You don't need to provide the extension.
}
@PostMapping(value="/insert")
public String insertBook(Model model,@RequestParam String title) {
Book book = Book.builder()
.title(title)
.description("beschreibung")
.author("auth" )
.isbn(12353)
.creator("creator") // fake it till spring security
.creationTS(LocalDateTime.MIN)
.publisher("pub")
.available(true)
.deliveryTimeDay(2)
.build();
bookRepository.save(book);
return showBooks(model);
}

}

书籍.html("主页"(

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Books</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"/>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="inputbook.html">Add Book <span class="sr-only">(current)</span></a>
</li>
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
</div>
</nav>
<div class="container">
<form method="post" th:action="@{/books/search}">
<div class="form-group">
<label th:for="searchTerm">Searchterm</label>
<input type="text" class="form-control" th:name="searchTerm">
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
**<form method="post" th:action="@{/books/insert}">
<div class="form-group">
<label th:for="title">Titel</label>
<input type="text" class="form-control" th:name="title">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>**
<table  class="table">
<thead>
<tr>
<th>Title</th>
<th>CreationTS</th>
<th>Author</th>
<th>Creator</th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${books}">
<td th:text="${book.title}">Betreff</td>
<td th:text="${book.creationTS}">2018-01-01 10:01:01</td>
<td th:text="${book.author}">TestAuthor</td>
<td th:text="${book.creator}">TestCreator</td>
</tr>
</tbody>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></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>

输入簿.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Books</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"/>
</head>
<body>
<div class="container">
<form method="post" th:action="@{/books/insert}">
<div class="form-group">
<label th:for="title">Titel</label>
<input type="text" class="form-control" th:name="title">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></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>

最简单和最干净的方法是定义一个简单的 Spring 控制器来处理请求。因此,将呈现 HTML 文件。 您使用的是什么模板引擎?你正在使用Spring Boot吗?你在任何地方都有代码吗?

此类控制器的示例:

@Controller
public class SimpleController {
@GetMapping("inputbook.html")
public String inputbook() {
return "inputbook"; // this returns the template name to be rendered from resources/templates. You don't need to provide the extension.
}
}

确保控制器中有一个映射到此 URL 的方法/inputbook.html并返回您想要看到的页面,如下所示。

@GetMapping("inputbook.html")
public String method() {
return "inputbook.html"; //extension depends on view resolver.
}

如果您不想在加载页面之前执行任何业务逻辑,那么现在需要在控制器中创建方法可以直接使用,如下所示。

XML 配置 (Spring-mvc(:

<mvc:view-controller path="/"view-name="index"/>

注释配置(弹簧引导(:

@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
}

最新更新