[英]Spring Boot JSP 404
我正在嘗試在我的 Spring 引導服務中添加 jsp 頁面。 我的問題是,每次我嘗試 go 到該頁面時,我都有這個:
白標錯誤頁面
此應用程序沒有顯式映射 /error,因此您將其視為后備。
2015 年 4 月 21 日星期二 23:16:00 EEST 出現意外錯誤(類型=未找到,狀態=404)。 沒有可用的消息
我在 application.properties 中添加了前綴和后綴:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
這是我的 controller class:
@Controller
public class MarkerController {
@RequestMapping(value="/map")
public String trafficSpy() {
return "index";
}
}
我的應用程序 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);
}
}
以及索引.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>
這是 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
確保您的依賴項列表中有 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>
這是一個工作啟動項目 - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp
在較新版本的 Spring 中,需要將以下內容放入 application.properties 文件中:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
另外,JSP文件需要放在src/main/resources/META-INF/resources/WEB-INF/jsp下
這對我來說是關於白標簽錯誤頁面的有效解決方案:找不到視圖頁面(jsp)
在 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>
在 application.properties 添加前綴/后綴
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
** 如果使用 Intellij,則必須設置 Web 資源目錄。 在 Project Structure (ctrl+alt+shift+ S) > Facets > Select Web(your application) > Add(+) Web Resource Directory(我的是......\\src\\main\\webapp)
** 如果您有多個模塊(在 intellij),運行 > 編輯配置 > 選擇 springboot 您的應用程序 > 配置選項卡 > 工作目錄為$MODULE_WORKING_DIR$
我的問題是我在控制器類中使用 @RestController 而不是 @Controller 作為注釋。 希望這可以幫助某人。
我們將 Spring Boot 添加到我們的系統中,以便在沒有獨立 tomcat 的情況下將其作為可執行應用程序運行,並且在 JSP 初始化期間還面臨404 狀態。
應該怎么做才能修復它:
a) 將依賴項添加到您的 pom 文件(警告:tomcat-embed-jasper 必須具有未提供的編譯范圍):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependencies>
b) 添加 spring boot maven 插件以構建您的應用程序:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
c) 檢查您的工件使用的是 *.war 文件,而不是 jar(因為 JSP 支持限制):
<packaging>war</packaging>
e) 現在您應該能夠使用 maven 插件或命令行運行您的 Spring Boot 應用程序 - java -jar /you/path/application-name.war:
f) 但是如果您使用的是多模塊 maven 項目並且想要使用 IntelliJ Idea 運行 spring boot 應用程序,那么更改“工作目錄”-> $MODULE_DIR$ 很重要:
我的問題是Spring版本:我發現自 1.4.3 版本及以上版本停止支持嵌入式 JSP。 所以我將版本更改為1.4.1 ,它對我有用。
另一件事起飛:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
它不會使用它。
如果您使用的是IDEA開發工具,那么您可以嘗試指定
Configurations -> Configuration -> environment -> Working directory
$MODULE_DIR$
的值
除了上面的答案,應用程序需要部署為war 而不是 jar
<groupId>com.igt</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
跑步
java -jar customer-0.0.1-SNAPSHOT.war
此外,如果您打算將應用程序作為戰爭或可執行應用程序啟動,則需要在 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);
}
};
}
}
請參見
Spring MVC 不提供開箱即用的默認(后備)錯誤頁面。 設置默認錯誤頁面的最常見方法一直是SimpleMappingExceptionResolver
(實際上從 Spring V1 開始)。 然而 Spring Boot 也提供了一個回退錯誤處理頁面。
在啟動時,Spring Boot 會嘗試為/error
查找映射。 按照慣例,以/error
結尾的 URL 映射到同名的邏輯視圖: error
。 通常,此視圖依次映射到error.html
Thymeleaf 模板。 (如果使用 JSP,它將根據您的 InternalResourceViewResolver 的設置映射到error.jsp
)。
Spring Boot 會自動使用和配置 Thymeleaf 作為視圖渲染引擎,只要它在類路徑上。
百里香與 Maven:
確保使用以下命令安裝了 Maven 3:mvn --version。 導航到要在其中創建項目的目錄並執行 Maven archtetype:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=pl.codeleak.demos.sbt -DartifactId=spring-boot-thymeleaf -interactiveMode=false
上述命令將創建一個新目錄 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>
如果未指定,來自父項目的值將是該項目的默認值。 下一步是添加 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>
現在,稍等片刻,直到 Maven 下載依賴項並運行 mvn dependency:tree 以查看包含哪些依賴項。 接下來是打包配置。 讓我們添加 Spring Boot Maven 插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
帶有 Gradle 的百里香:
將 Thymeleaf 放在類路徑上使用
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
在 gradle 構建文件中(使用相關的 maven 依賴很簡單)。
在您的情況下,為了顯示index.jsp
視圖(根據您使用的控制器),您需要將它放在src/main/resources/templates/
。
如果找不到從 /error 到 View 的映射,Spring Boot 定義了自己的回退錯誤頁面 - 所謂的Whitelabel Error Page
(一個只有 HTTP 狀態信息和任何錯誤詳細信息的最小頁面,例如消息來自未捕獲的異常)。
您可以將 thymeleaf 與 jsp 一起使用,但您必須編寫:
spring.thymeleaf.excluded-view-names=#jsp file without extension
在 application.properties 文件中
在 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>
這可能還不夠。
你一定不要錯過這個。
<packaging>war</packaging>
否則,當您構建包時,您將獲得一個 jar 文件,它既沒有 JSP 也沒有嵌入的 tomcat。
在此處查看可運行示例及其解釋https://www.surasint.com/spring-boot-jsp/
對我來說,這似乎是一個日食問題。 我能夠在 IDE 外使用 java -jar 啟動 springboot 應用程序。
確保您的 .jsp 頁面位於 WebContent 下:
WebContent|-
|-Jsp
|-home.jsp
對於 spring-boot、Jsp 模板或 Thymeleaf 模板映射 Z2A2D595E6ED9A0B234F027F2B6Z 到類路徑。
前任。 /views/some_path 需要位於項目src/main/resources/META-INF/resources/views/some_path
嘗試在錯誤文件夾下添加您的錯誤 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.