gpt4 book ai didi

Java Spring 简单 HTML 链接

转载 作者:行者123 更新时间:2023-11-30 02:03:32 25 4
gpt4 key购买 nike

我对 Spring Web 应用程序相当陌生,需要为大学做一个小项目。目前我有一个简单的 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”(在另一个线程中找到)
  • “./inputbook.html”
  • “../inputbook.html”

有没有办法简单地链接此页面,或者我需要在 Controller 中创建一个操作+一个方法?

谢谢!

更新:

刚刚发生了一些有趣的事情。我添加了在 Controller 中映射站点的方法。当我尝试通过导航栏打开此站点时,它告诉我它未映射。现在(为了测试,我的主页中也有“inputbook.html”文件中的表单)当我通过主页表单输入数据时,它会将其保存到数据库并正确显示。完成此过程后,当我再次单击导航栏时,它会打开“inputbook.html”网站,没有任何问题吗?

Controller :

@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);
}

}

books.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 Controller 来处理请求。这样 HTML 文件就会被渲染出来。您使用什么模板引擎?你在使用 Spring Boot 吗?你有代码吗?

此类 Controller 的示例:

@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.
}
}

关于Java Spring 简单 HTML 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com