- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
计算器!
我已经与这个恼人的错误作斗争了几天了。我发现很多人处于同样的情况,但他们的解决方案对我不起作用。我正在使用 Spring Boot,并尝试为我的项目制作主页。目前,在我的 Controller 类中我有这样的设置:
@RequestMapping("/")
public String getHomePage(Map<String, Object> model) {
return "home";
}
我遇到的问题是当我尝试加载“/”网址时,我收到此错误消息:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Mar 25 14:46:01 MDT 2020
There was an unexpected error (type=Internal Server Error, status=500).
Could not resolve view with name 'home' in servlet with name 'dispatcherServlet'
javax.servlet.ServletException: Could not resolve view with name 'home' in servlet with name 'dispatcherServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1353)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at
从该日志看来,我的 Controller 找不到 home.jsp 主页。我相信我最初的 .jsp 页面位于 resources/view/文件夹中。
我通过别人的经验调查了这些问题:
RequestMapping DisplatcherServlet no mapping found这篇文章表明他们的 REST API 无法正常工作,但我的 API 的所有组件都按预期工作。
By default, where does Spring Boot expect views to be stored?他的发现很有趣:
JSPs simply do not play well with Spring Boot as it has limitations depending on the embedded container chosen ... [He further explains all that is necessary is ] the default Spring Boot template and 2 ThymeLeaf dependencies with the views named as ViewName.html placed in src/main/resources/templates [is all that is needed].
但是,即使在尝试通过添加依赖项 thymeleaf 和 thymeleaf-spring4、删除 spring.mvc.view.prefix 和 spring.mvc.view.suffix (将其设置为默认值)并复制来修改我的项目之后我的 home.html 文件复制到 src/main/resources/templates 文件夹,问题仍然存在。
实际上,我最初在这里列出了更多来源,但我的草稿丢失了,我懒得再次浏览所有这些帖子。我需要发布这个问题。其他来源对我应该放置 home.html/home.jsp 的位置有不同的要求。对于每一个建议,我都会确保在 .jsp 和 .html 之间切换以测试它们。有人说 META-INF 无法正常工作,并确保您没有将其作为文件夹名称。有人说您需要在当前现有的资源文件夹中嵌套一个资源文件夹才能使其正常工作。我也遵循了这些修复,但没有一个成功起作用。错误也没有变化,仍然找不到 home.jsp/home.html。为了测试这些解决方案,我已经多次更新了我的 application.properties ,目前我的 View 前缀和后缀是这样的:
spring.mvc.view.prefix=/resources/view/
spring.mvc.view.suffix=.html
我确实确保我的 Spring 版本 (2.2.5.RELEASE) 是最新的。另一个小注意事项,我正在使用 Spring Data REST,但我的 REST API 设置为/api/
您可以在这里看到我的存储库:https://github.com/CloakingOcean/CampaignChronicle
我还将在这里列出主要文件和我的依赖项。
(为了清楚地了解 home.html 的预期位置,删除了其他测试文件夹)
package com.exitium.campaignchronicle;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@EnableWebMvc
public class CampaignchronicleApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CampaignchronicleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(CampaignchronicleApplication.class, args);
}
}
package com.exitium.campaignchronicle.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class MainController {
@RequestMapping("/")
public String getHomePage(Map<String, Object> model) {
return "home";
}
}
#
# JDBC properties
#
spring.datasource.url=jdbc:mysql://localhost:3306/campaignchronicle
spring.datasource.username=cloakingocean
spring.datasource.password=9QfsJGA4V6M74LB
#
# Spring Data REST properties
#
spring.data.rest.base-path=/api
spring.mvc.view.prefix=/resources/view/
spring.mvc.view.suffix=.html
# ==============================================================
# = Logging springframework
# ==============================================================
logging.level.org.springframework.jdbc=DEBUG
logging.level.org.springframework.security=DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.http=DEBUG
package com.exitium.campaignchronicle.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and().formLogin().permitAll();
}
@Bean
public PasswordEncoder getPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.exitium</groupId>
<artifactId>campaignchronicle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>campaignchronicle</name>
<description>Note taking application for Dungeons & Dragons players</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Add dependency for Spring Data REST -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- <dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>9.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
最佳答案
我认为我们可以使用Thymeleaf
。
从 application.properties 中删除以下内容
spring.mvc.view.prefix=/resources/view/
spring.mvc.view.suffix=.html
要使用 thymeleaf 模板引擎代替 JSP 和 JSLT,请添加依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
默认情况下,thymeleaf 将在 src/main/resources/templates
中查找 .html
文件(jar 部署)要部署为 war,请更改 application.properties
引用:https://github.com/hovermind/springboot-mvc/blob/master/README.md#thymeleaf-instead-of-jsp
引用:By default, where does Spring Boot expect views to be stored?
关于java - SpringBoot Controller 无法解析名称为 'home' 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60858859/
有谁知道如何实现称为 PRUNING 的图像处理技术?任何想法,例子等?我正在使用 OpenCV 和 C#,如果有人可以提供帮助,我将不胜感激。 最佳答案 我假设您希望从图像中移除不需要的杂散和伪影。
假设我已经构建了一个可调用对象数组 callables = [] callables << block1 callables << block2 callables << block3 callabl
这个问题在这里已经有了答案: std::shared_ptr of this (2 个答案) 关闭 8 年前。 我正在学习 C++11 特性,特别是 shared_ptr,我在引用 this 并将其
我主要是一个oracle新手,所以如果这是一个愚蠢的问题,请原谅我...... 我已经阅读了很多关于 authID 的文章,我的理解是 AUTHID 子句指示 Oracle 是否以调用者的权限 (CU
我使用的是 Python 2.7。 按照惯例,__iter__、__init__、__str__ 等内置方法使用双下划线来标识它们。为什么 next() 函数没有这个? 它与这里的其他内置函数一起
这是疏忽吗?还是跟JVM有关? 最佳答案 Java 确实有指针——不能在指针上执行指针运算。 来自可敬的JLS : There are two kinds of types in the Java p
我越来越多地发现宣传“原生”桌面应用程序的服务,但在下载或研究后发现它们是在 Electron 或类似的 Web View 框架中构建的 Web View 。他们误解了“本地人”还是我? 另外,我可以
我尝试在 Wordpress 中使用,但它在控制台中显示 jstree 不是函数在 header PHP 文件中: /dist/libs/jquery.js"> /dist/themes/defaul
我想让 scons 调用带有目录列表的 nosetests。执行此操作的最佳方法是什么? 最佳答案 如果您需要分析外部应用程序的返回码(例如,如果您调用测试),您需要使用 Command() + py
这是一些尝试使用 OOB(紧急)数据的基本代码。我的问题是,如果客户端使用 C 或 Java,则服务器部分的行为不同。请注意,您可能认为这两个客户端都有些棘手,但如果我使用 C 服务器(以更好地控制
虚拟文件系统 (VFS) 或虚拟文件系统交换机是更具体文件系统之上的抽象层。 VFS的目的是允许客户端应用程序以统一的方式访问不同类型的具体文件系统。 如果我们看到 VFS 的实际工作,这个定义似乎是
"Effective Go"状态: By convention, one-method interfaces are named by the method name plus an -er suff
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 3 个月前。 社区审核了是否
在深入研究 Scala 时,我遇到了 这个词。类型类 . 它一直令人困惑,因为一个类是一个类型,而一个类型可以 在 Scala 中是一个类,“类型”和“类”本身就是抽象术语。 在阅读了一些示例后,我感
我正在尝试编写一个简单地杀死我的程序的函数。它被称为退出聊天。但是,当我调用函数 messenger:quitChat() 时,出现此错误。 ** exception error: bad argum
我正在尝试编写一个简单地杀死我的程序的函数。它被称为退出聊天。但是,当我调用函数 messenger:quitChat() 时,出现此错误。 ** exception error: bad argum
如果您向 DB2/zOS 引擎 DBA 询问有关 DB2 行为的问题,DBA 会将 DB2 引擎称为“他”,就像水手使用“她”来指代他的船一样。 例如:“一旦您填充了可用空间,DB2 仍希望在表空间中
案例:我有 2 个具有以下 OneToMany 关系的表:TagAbstract 1..* TagConf,其中: tag_abstract_ID 是一个字段,用于标识许多 TagConf 记录,这些
我有一个表格,我想让第一行“自动固定”(这意味着当向下滚动时它固定在顶部,而如果不向下滚动,它就不固定)。这是代码:``` table { background: yellow; } td
其他动词对我来说都有意义,但我对“发布”作为动词没有太多上下文。它是像在邮局发帖(这有一定道理,虽然看起来有点牵强)还是像在公告板上发帖(对我来说意义不大)?有谁知道是谁决定了“POST”以及为什么选
我是一名优秀的程序员,十分优秀!