我正在使用springboot + thymeleaf来创建一个简单的网站。
这是我的项目,用STS创建的Maven like:
CSS
文件夹是重复的,但我想这不是问题。当我将其作为SpringBoot应用程序运行时,一切工作正常。我的index.html页面就会显示出来,CSS
和JS
都能很好地工作。当我尝试访问index.html
中的th:href
之后的新页面(hello.html
)时,出现了问题。这是html部分:
<html xmlns:th="http://www.thymeleaf.org"> //For thymeleaf
<head>
<title> X BIG DEALS </title>
<link rel="stylesheet" type="text/css" media="all"
href="/css/index/index.css" th:href="@{/css/index/index.css}" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/
bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/
iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> //just bootstrap
</head>
<header id="HD" class="header">
<a class="logo">X4ALL</a>
<div class="header-right">
<a th:href="@{/hello}" id = "HELLO" class="active">Hello</a> //PROBLEM HERE I SUPPOSE
</div>
</header>
当我单击此链接时,将显示一个包含说明的TomCat错误页面:Error 404 - The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
然后我检查了spring,发现了这个错误:
2021-11-17 14:28:19.694 ERROR 90008 --- [nio-8080-exec-5] o.a.c.c.C.[Tomcat].[localhost] : Exception Processing ErrorPage[errorCode=0, location=/error]
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [error], template might not exist or might not be accessible by any of the configured Template Resolvers
所以Thymeleaf
有一个错误,但我不知道如何移动,这是HelloController
:
package it.uniroma3.siw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
@RequestMapping(value= "/hello", method = RequestMethod.GET)
public String loginPage() {
return "hello" ;
}
}
我也试图返回hello.html,但是什么都没有改变。在作为Springboot应用程序运行之前,我做了一个Maven Clean
和Maven Install
。
也许有帮助,application.properties
:
#==================================
# = Datasource configuration
#==================================
spring.jpa.database=POSTGRESQL
spring.sql.init.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/products
spring.datasource.username=postgres
spring.datasource.password=postgres
#==================================
# = Webserver configuration
#==================================
server.port= 8080
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always
#==================================
# = Thymeleaf configurations
#==================================
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
server.servlet.context-path=/
#==================================
# = Logging configuration
#==================================
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
logging.level.org.springframework.web=INFO
logging.level.it.uniroma3.siw.spring=DEBUG
#==================================
# = Misc configuration
#==================================
spring.messages.basename=messages/message
##==================================
# = Security configuration
#==================================
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
附言:我认为没有控制器来处理index.html
,因为在开始的时候我没有创建一个控制器,但是默认页面已经是index.html
了,并且它可以工作。然后我创建了一个接受"/"
的MainController
,但我认为它不起作用。
应用程序文件:
package it.uniroma3.siw.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
//@ComponentScan(basePackages = "it.uniroma3.siw.*")
public class ProgettoApplication {
public static void main(String[] args) {
SpringApplication.run(ProgettoApplication.class, args);
}
}
发布于 2021-11-17 06:38:25
尝尝这个
@SpringBootApplication
@ComponentScan(basePackages = "it.uniroma3.siw.*")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
发布于 2021-11-17 06:38:35
我认为你错误配置了你的application.properties:
spring.thymeleaf.prefix=classpath:/templates/
无论如何,我认为你可以在application.properties中删除所有与胸腺叶相关的配置,因为Spring Boot默认情况下会处理这些问题!
发布于 2021-11-17 06:48:29
要呈现新页面,可以在layout.html中设置一个菜单,其中包含以下链接:
<header th:fragment="site-header">
<a href="index.html" th:href="@{/}"></a>
<a href="#" style="color: white" th:href="@{/}">Home</a>
<a href="#" style="color: white" th:href="@{/add}">Add Items</a>
<a href="#" style="color: white" th:href="@{/items}">Get Items</a>
</header>
THis会给你一个类似如下的菜单:
现在设置对应于每个链接的相应控制器方法。例如,Get Items调用此控制器方法:
@GetMapping("/add")
fun designer(): String? {
return "add"
}
这将出现在模板下的add.html页面上:
您可以将这样的类与@SpringBootApplication一起使用,这样Spring就可以找到主类。
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SecureWebApp {
public static void main(String[] args) throws Throwable {
SpringApplication.run(SecureWebApp.class, args);
}
}
与旧的Spring MVC框架相比,这是一个巨大的改进,在旧的Spring MVC框架中,您必须处理大量的配置文件。更多详细信息请点击此处:
https://spring.io/guides/gs/spring-boot/
这就是在Spring启动应用程序中导航页面的方式。
https://stackoverflow.com/questions/70005804
复制相似问题