Java 弹簧启动 JSP 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29782915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot JSP 404
提问by dephinera
I'm trying to add a jsp page in my Spring Boot service. My problem is that every time I try to go to that page I have this:
我正在尝试在我的 Spring Boot 服务中添加一个 jsp 页面。我的问题是,每次我尝试访问该页面时,我都会遇到以下问题:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Apr 21 23:16:00 EEST 2015 There was an unexpected error (type=Not Found, status=404). No message available
白标错误页面
此应用程序没有明确的 /error 映射,因此您将其视为后备。
Tue Apr 21 23:16:00 EEST 2015 出现意外错误(类型=未找到,状态=404)。没有可用的消息
I have added the prefix and sufix into my application.properties:
我已将前缀和后缀添加到我的 application.properties 中:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
This is my controller class:
这是我的控制器类:
@Controller
public class MarkerController {
@RequestMapping(value="/map")
public String trafficSpy() {
return "index";
}
}
My Application class:
我的应用程序类:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static Logger logger = Logger.getLogger(Application.class.getName());
public static void main(String[] args) {
logger.info("SPRING VERSION: " + SpringVersion.getVersion());
SpringApplication.run(Application.class, args);
}
}
And the index.jsp:
和 index.jsp:
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<body>
<h1>Hello, World!!!</h1>
<p>JSTL URL: ${url}</p>
</body>
</html>
And this is the src file structure:
这是 src 文件结构:
├── src
│?? ├── main
│?? │?? ├── java
│?? │?? │?? └── com
│?? │?? │?? └── example
│?? │?? │?? └── internetprogramming
│?? │?? │?? └── myserver
│?? │?? │?? └── server
│?? │?? │?? ├── Application.java
│?? │?? │?? ├── config
│?? │?? │?? │?? └── DatabaseConfig.java
│?? │?? │?? ├── controller
│?? │?? │?? │?? └── MarkerController.java
│?? │?? │?? ├── dao
│?? │?? │?? │?? ├── MarkerDaoImplementation.java
│?? │?? │?? │?? └── MarkerDaoInterface.java
│?? │?? │?? ├── Marker.java
│?? │?? │?? └── service
│?? │?? │?? ├── MarkerServiceImplementation.java
│?? │?? │?? └── MarkerServiceInterface.java
│?? │?? ├── resources
│?? │?? │?? └── application.properties
│?? │?? └── webapp
│?? │?? └── WEB-INF
│?? │?? └── jsp
│?? │?? └── index.jsp
采纳答案by Biju Kunjummen
Ensure that you have jasper and jstl in the list of dependencies:
确保您的依赖项列表中有 jasper 和 jstl:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Here is a working starter project - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp
这是一个工作启动项目 - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp
回答by MCHAppy
Spring MVC offers no default (fall-back) error page out-of-the-box. The most common way to set a default error page has always been the SimpleMappingExceptionResolver
(since Spring V1 in fact). However Spring Boot also provides for a fallback error-handling page.
Spring MVC 不提供开箱即用的默认(后备)错误页面。设置默认错误页面的最常见方法一直是SimpleMappingExceptionResolver
(实际上从 Spring V1 开始)。然而 Spring Boot 也提供了一个回退错误处理页面。
At start-up, Spring Boot tries to find a mapping for /error
. By convention, a URL ending in /error
maps to a logical view of the same name: error
. Generally this view maps in turn to the error.html
Thymeleaf template. (If using JSP, it would map to error.jsp
according to the setup of your
InternalResourceViewResolver).
在启动时,Spring Boot 尝试为/error
. 按照惯例,以结尾的 URL/error
映射到同名的逻辑视图:error
。通常,此视图依次映射到error.html
Thymeleaf 模板。(如果使用 JSP,它将error.jsp
根据您的 InternalResourceViewResolver 的设置映射到)。
Spring Boot will automatically use and configure Thymeleaf as the view rendering engine, as long as it's on the classpath.
Spring Boot 会自动使用和配置 Thymeleaf 作为视图渲染引擎,只要它在类路径上。
Thymeleaf with Maven:
百里香与 Maven:
Make sure you have Maven 3 installed with the following command: mvn --version. Navigate to the directory you want to create your project in and execute Maven archtetype:
确保使用以下命令安装了 Maven 3:mvn --version。导航到要在其中创建项目的目录并执行 Maven archtetype:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=pl.codeleak.demos.sbt -DartifactId=spring-boot-thymeleaf -interactiveMode=false
The above command will create a new directory spring-boot-thymeleaf. Now you can import it to your IDE. The next step is to configure the application. Open pom.xml and add a parent project:
上述命令将创建一个新目录 spring-boot-thymeleaf。现在您可以将它导入到您的 IDE 中。下一步是配置应用程序。打开 pom.xml 并添加一个父项目:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
Values from the parent project will be the default for this project if they are left unspecified. The next step is to add web dependencies. In order to do so, I firstly removed all previous dependencies (junit 3.8.1 actually) and added the below dependencies:
如果未指定,来自父项目的值将是该项目的默认值。下一步是添加 Web 依赖项。为此,我首先删除了所有以前的依赖项(实际上是junit 3.8.1)并添加了以下依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
Now, wait a second until Maven downloads the dependencies and run mvn dependency:tree to see what dependencies are included. The next thing is a packaging configuration. Let's add Spring Boot Maven Plugin:
现在,稍等片刻,直到 Maven 下载依赖项并运行 mvn dependency:tree 以查看包含哪些依赖项。接下来是打包配置。让我们添加 Spring Boot Maven 插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Thymeleaf with Gradle:
带有 Gradle 的百里香:
To put Thymeleaf on the classpath use
将 Thymeleaf 放在类路径上使用
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
in the gradle build file (using the relevant maven dependency is straightforward).
在 gradle 构建文件中(使用相关的 maven 依赖很简单)。
In your case in order to display the index.jsp
view (in accordance to the controller you are using), you need to place it under src/main/resources/templates/
.
在您的情况下,为了显示index.jsp
视图(根据您使用的控制器),您需要将其放在src/main/resources/templates/
.
If no mapping from /error to a View can be found, Spring Boot defines its own fall-back error page - the so-called Whitelabel Error Page
(a minimal page with just the HTTP status information and any error details, such as the message from an uncaught exception).
如果找不到从 /error 到 View 的映射,Spring Boot 定义了自己的回退错误页面 - 所谓的Whitelabel Error Page
(一个只有 HTTP 状态信息和任何错误详细信息的最小页面,例如来自未捕获的消息的消息)例外)。
回答by jovanchohan
My issue was that I was using @RestController instead of @Controller as the annotation in my controller class. Hope this can help someone out.
我的问题是我在控制器类中使用 @RestController 而不是 @Controller 作为注释。希望这可以帮助某人。
回答by Ajitesh
In newer versions of Spring, following needs to be put in application.properties file:
在较新版本的 Spring 中,需要将以下内容放入 application.properties 文件中:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
Also, JSP files need to be put under src/main/resources/META-INF/resources/WEB-INF/jsp
另外,JSP文件需要放在src/main/resources/META-INF/resources/WEB-INF/jsp下
回答by Omar B.
my issue was Spring vesrion: I found that since 1.4.3 version and above stops supporting the embedded JSPs . So I change version to 1.4.1, it's worked for me.
我的问题是 Spring版本:我发现自 1.4.3 版本及以上版本停止支持嵌入式 JSP。所以我将版本更改为1.4.1,它对我有用。
an other thing take off :
另一件事起飞:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
it will not work with it .
它不会使用它。
回答by Sam2016
In addition to the answers above the application needs to be deployed as war instead jar
除了上面的答案,应用程序需要部署为war 而不是 jar
<groupId>com.igt</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
to run
跑步
java -jar customer-0.0.1-SNAPSHOT.war
Also If you intend to start your application as a war or as an executable application, you need to share the customizations of the builder in a method that is both available to the SpringBootServletInitializer callback and the main method, something like
此外,如果您打算将应用程序作为战争或可执行应用程序启动,则需要在 SpringBootServletInitializer 回调和 main 方法都可用的方法中共享构建器的自定义,例如
package com.igt.customer;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class CustomerApplication extends org.springframework.boot.web.support.SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CustomerApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
Please see
请参见
回答by GovindaRaju
Try and add your error jsp files under error folder.
尝试在错误文件夹下添加您的错误 jsp 文件。
application.properties
spring.mvc.view.prefix=/views/jsp/
spring.mvc.view.suffix=.jsp
jsp files :
/views/jsp/error/401.jsp
/views/jsp/error/404.jsp - to display 404 instead of default whitelabel page
回答by Kike Lebowski
You can use thymeleaf with jsp but you have to write:
您可以将 thymeleaf 与 jsp 一起使用,但您必须编写:
spring.thymeleaf.excluded-view-names=#jsp file without extension
in application.properties file
在 application.properties 文件中
回答by Surasin Tancharoen
To have this in pom.xml
在 pom.xml 中有这个
<!-- JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- jstl for jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
It may be not enough.
这可能还不够。
You must not miss this.
你一定不要错过这个。
<packaging>war</packaging>
Otherwise when you build the package, you will get as a jar file and that does not have JSP nor the embedded tomcat.
否则,当您构建包时,您将获得一个 jar 文件,它既没有 JSP 也没有嵌入的 tomcat。
See runable example and its explanation here https://www.surasint.com/spring-boot-jsp/
在此处查看可运行示例及其解释 https://www.surasint.com/spring-boot-jsp/
回答by Kyung Hwan Min
This is working solution for me about White label error page : Cannot find view page(jsp)
这对我来说是关于白标签错误页面的有效解决方案:找不到视图页面(jsp)
At POM.xml, Make sure packaging is "war" and add tomcat/jasper dependencies
在 POM.xml 中,确保打包是“war”并添加 tomcat/jasper 依赖项
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Add prefix/suffix at application.properties
在 application.properties 添加前缀/后缀
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
** If you use Intellij, you must set Web Resource directories. At Project Structure (ctrl+alt+shift+ S) > Facets > Select Web(your application) > Add(+) Web Resource Directories ( mine is ......\src\main\webapp)
** 如果使用 Intellij,则必须设置 Web 资源目录。在项目结构 (ctrl+alt+shift+ S) > Facets > Select Web(your application) > Add(+) Web Resource Directory(我的是......\src\main\webapp)
** If you have multiple modules(At intellij), Run> Edit configuration> Select springboot your application > Configuration tab> Working directory as $MODULE_WORKING_DIR$
** 如果您有多个模块(在 intellij),运行 > 编辑配置 > 选择 springboot 您的应用程序 > 配置选项卡 > 工作目录为$MODULE_WORKING_DIR$