我正在尝试做简单的spring MVC库
你知道为什么我在View上有问题吗?当我在控制器中使用homepage方法时,它应该显示给我index.html,但我一直只得到白标签错误页面,而且我不知道为什么:/
我的结构:[https://i.imgur.com/5trggrb.png]
我的控制器:
null
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");
}
}
我的POM:
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyLibrary</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Something</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/book?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
spring.thymeleaf.mode=LEGACYHTML5
您的演示项目有一些问题。
您将runner
类放入默认包中。这不是一个好主意,因为在这种情况下,spring boot无法设置组件扫描的默认包。当您运行应用程序时,您应该看到如下内容:
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
解决方案:将您的所有类移动到通用包。
在pom.xml
文件中添加以下依赖项以使Thymeleaf模板正常工作:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然后删除pom.xml
中存在的以下依赖项:
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
当您运行应用程序并打开http://localhost:8080时,您将看到以下异常:
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
添加以下依赖项:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
在应用了所有这些步骤之后,您将在应用程序的主页上看到“Hello”。希望有帮助。
我不知道如何解决这个问题 spring端:role.java 因此,如果我现在尝试inline=“javascript”: 当我运行它时,我得到这样的响应: 白标签错误页
我坚持使用这个简单的MVC示例。当我启动应用程序并转到localhost:8080时,我得到了“白标签错误页面”,甚至我在“src/main/资源/模板”中创建了“index.html”。我还在我的索引方法上添加了@Request estMap(“/”)。我找不到问题。 : : -在"src/main/资源/模板"下:
下面是我的代码:我正在从application.properties文件swagerconfig.java获取所有值 下面是我的springboot初始值设定项: ServletInitializer.java 日志显示它已映射: 这是我得到的错误:
当我使用ide运行我的springboot应用程序时,它工作正常。 但当我运行它的jar时,我得到了白标签错误页面。 白标签错误页面 此应用程序没有 /error的显式映射,因此您将此视为后备。 2018年10月9日星期二15:42:47出现意外错误(类型=未找到,状态=404)/WEB-INF/views/index。jsp
当我运行spring boot MVC应用程序时,得到以下白标签错误页面。 白标签错误页 此应用程序没有/error的显式映射,因此您将其视为一种后退。 Wed Apr 13 15:45:59 IST 2016出现意外错误(Type=内部服务器错误,Status=500)。循环视图路径[home]:将再次发送回当前处理程序URL[/rewards/web/home]。检查您的ViewResolve
当我在本地执行“java-jar target/loginmodel-0.0.1-snapshot.jar”并尝试在浏览器上看到它时,它工作得很好,没有任何错误。我只是在尝试在docker容器上运行应用程序时才得到错误。到底错在哪里?