Spring 弹簧靴&x2B;百里香叶=”;白标错误页“;尝试呈现index.html时
我正在尝试做简单的Spring MVC库你知道我为什么在视图方面有问题吗,当我在控制器中使用homepage方法时,它应该显示index.html,但我一直只能看到白标错误页面,我不知道为什么:/ 我的结构: [ 我的控制器:Spring 弹簧靴&x2B;百里香叶=”;白标错误页“;尝试呈现index.html时,spring,maven,spring-boot,thymeleaf,Spring,Maven,Spring Boot,Thymeleaf,我正在尝试做简单的Spring MVC库你知道我为什么在视图方面有问题吗,当我在控制器中使用homepage方法时,它应该显示index.html,但我一直只能看到白标错误页面,我不知道为什么:/ 我的结构: [ 我的控制器: package controller; import model.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stere
package controller;
import model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import service.BookService;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class LiberianController{
@Autowired
private BookService bookService;
@RequestMapping(value = "/")
public String homepage() {
return "index";
}
@GetMapping(value = "/allBooks")
public ModelAndView allBooks(ModelAndView modelAndView) {
List<Book> books = bookService.getAllBooks();
modelAndView.addObject("listBooks", books);
modelAndView.setViewName("allBooks");
return modelAndView;
}
@GetMapping(value = "/addBook")
public ModelAndView newBook(ModelAndView modelAndView) {
Book book = new Book();
modelAndView.addObject("book", book);
modelAndView.setViewName("addBook");
return modelAndView;
}
@GetMapping(value = "updateBook")
public ModelAndView updateBook(HttpServletRequest httpServletRequest) {
long id = Long.parseLong(httpServletRequest.getParameter("id"));
Book book = bookService.getBook(id);
ModelAndView modelAndView = new ModelAndView("addBook");
modelAndView.addObject("book", book);
return modelAndView;
}
@RequestMapping(value = "/saveBook",method = RequestMethod.POST)
public ModelAndView saveBook(@ModelAttribute Book book) {
if (book.getId() == 0) {
bookService.addBook(book);
} else {
bookService.updateBook(book.getId(), book);
}
return new ModelAndView("redirect:/allBooks");
}
@GetMapping(value = "/deleteBook")
public ModelAndView deleteBook(HttpServletRequest httpServletRequest) {
long id = Long.parseLong(httpServletRequest.getParameter("id"));
bookService.deleteBook(id);
return new ModelAndView("redirect:/allBooks");
}
}
您的演示项目有一些问题 1.包装结构不正确 您将您的
Runner
类放入默认包。这不是一个好主意,因为在这种情况下,Spring Boot无法为组件扫描设置默认包。当您运行应用程序时,您应该看到如下内容:
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
解决方案:将所有类移动到通用包
2.spring boot starter thymeleaf
缺失
在您的pom.xml
文件中,添加以下依赖项以使Thymeleaf模板工作:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.spring.thymeleaf.mode=LEGACYHTML5
需要额外的依赖关系
运行应用程序并打开时,您将看到以下异常:
org.thymeleaf.exceptions.ConfigurationException: Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in "LEGACYHTML5" mode [http://nekohtml.sourceforge.net]. Maven spec: "net.sourceforge.nekohtml::nekohtml::1.9.15". IMPORTANT: DO NOT use versions of nekoHTML older than 1.9.15.
at org.thymeleaf.templateparser.html.AbstractHtmlTemplateParser.parseTemplate(AbstractHtmlTemplateParser.java:90) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:278) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
这是因为您已指定:
spring.thymeleaf.mode=LEGACYHTML5
您还没有将nekoHTML
库添加到类路径中
解决方案:将以下依赖项添加到您的pom.xml
:
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
net.sourceforge.nekohtml
内科特米尔
1.9.22
应用所有这些步骤后,您将在应用程序的主页上看到“Hello”。希望对您有所帮助
org.thymeleaf.exceptions.ConfigurationException: Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in "LEGACYHTML5" mode [http://nekohtml.sourceforge.net]. Maven spec: "net.sourceforge.nekohtml::nekohtml::1.9.15". IMPORTANT: DO NOT use versions of nekoHTML older than 1.9.15.
at org.thymeleaf.templateparser.html.AbstractHtmlTemplateParser.parseTemplate(AbstractHtmlTemplateParser.java:90) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:278) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
spring.thymeleaf.mode=LEGACYHTML5
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>