gpt4 book ai didi

java - @Controller 的 Spring Boot Whitelabel 错误?

转载 作者:行者123 更新时间:2023-12-02 02:06:33 27 4
gpt4 key购买 nike

我从 springinitializr 下载了一个示例演示项目 web依赖性。然后我创建了这个类。

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController {

@GetMapping("/test")
public String test() {
return "test";
}

}

它与启动 Spring Boot 的 run 方法的文件位于同一个包中。

现在我有一个test.jsp文件。

我应该把它放在哪里?我尝试创建一个 src/main/webapp/WEB-INF/views文件夹并将文件放入其中,并在 application.properties 中添加前缀和后缀属性如下

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

我收到白标错误。我怎样才能让我的 Controller 工作?现在如果我改变 @Controller注释 @RestController注释,我能够得到字符串 test显示在浏览器中,而不是 test.jsp 的内容它工作得很好,因为它应该是一个 RestController。但是@Controller注释根本不起作用。

注意:这是我要访问的网址: http://localhost:8080/test

更新:以下是我点击上述网址时的日志:

2018-06-05 02:21:34.659 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing servlet 'dispatcherServlet'
2018-06-05 02:21:34.659 INFO 9224 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-06-05 02:21:34.659 INFO 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-06-05 02:21:34.659 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver@27b737cf]
2018-06-05 02:21:34.662 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@41be462c]
2018-06-05 02:21:34.665 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@7c946fc0]
2018-06-05 02:21:34.669 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@4f05a3a6]
2018-06-05 02:21:34.673 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@4dc34295]
2018-06-05 02:21:34.674 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
2018-06-05 02:21:34.674 INFO 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
2018-06-05 02:21:34.674 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Servlet 'dispatcherServlet' configured successfully
2018-06-05 02:21:34.681 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/test]
2018-06-05 02:21:34.682 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /test
2018-06-05 02:21:34.684 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String com.example.demo.TestController.test()]
2018-06-05 02:21:34.684 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/test] is: -1
2018-06-05 02:21:34.695 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2018-06-05 02:21:34.695 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'test'
2018-06-05 02:21:34.697 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/WEB-INF/views/test.jsp]] based on requested media type 'text/html'
2018-06-05 02:21:34.697 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/WEB-INF/views/test.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2018-06-05 02:21:34.700 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [/WEB-INF/views/test.jsp] in InternalResourceView 'test'
2018-06-05 02:21:34.702 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/views/test.jsp]
2018-06-05 02:21:34.703 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/views/test.jsp
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/views/test.jsp]
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/views/test.jsp] are [/**]
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/WEB-INF/views/test.jsp] are {}
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/WEB-INF/views/test.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@62f87c44]]] and 1 interceptor
2018-06-05 02:21:34.705 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/WEB-INF/views/test.jsp] is: -1
2018-06-05 02:21:34.705 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-06-05 02:21:34.705 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-06-05 02:21:34.706 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2018-06-05 02:21:34.712 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2018-06-05 02:21:34.713 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@317e671e] based on requested media type 'text/html'
2018-06-05 02:21:34.713 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@317e671e] in DispatcherServlet with name 'dispatcherServlet'
2018-06-05 02:21:34.723 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request

更新:

以下是所附文件夹结构的屏幕截图:

Folder Structure Screenshot

更新:

这是 pom.xml文件。

<?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>com.example</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>2.0.2.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-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

最佳答案

请在您的pom.xml中添加这些依赖项

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

让我知道它是否有效!

关于java - @Controller 的 Spring Boot Whitelabel 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50694057/

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