如何在Spring Boot中使用JSP

问题描述 投票:0回答:3

按照指南配置了Spring Boot,仍然找不到我的jsp视图。因此,启动后我收到此消息“此应用程序没有 /error 的显式映射,因此您将其视为后备。”

有什么建议吗?

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 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.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lib.secondtry</groupId>
    <artifactId>MyLibrary</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MyLibrary</name>
    <description>MyLibrary</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.44</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.44</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </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>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </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>

特性:

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

控制器:

@Controller
public class FirstController {
    @GetMapping("/")
    public String sayHello(){
        return "hello";
    }
}

控制台:

2022-11-15 23:09:26.879  INFO 11896 --- [  restartedMain] c.l.s.mylibrary.MyLibraryApplication     : No active profile set, falling back to 1 default profile: "default"
2022-11-15 23:09:26.907  INFO 11896 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-11-15 23:09:26.908  INFO 11896 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-11-15 23:09:27.369  INFO 11896 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-11-15 23:09:27.374  INFO 11896 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-15 23:09:27.375  INFO 11896 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.68]
2022-11-15 23:09:27.495  INFO 11896 --- [  restartedMain] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2022-11-15 23:09:27.500  INFO 11896 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-15 23:09:27.501  INFO 11896 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 593 ms
2022-11-15 23:09:27.663  INFO 11896 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-11-15 23:09:27.694  INFO 11896 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-11-15 23:09:27.700  INFO 11896 --- [  restartedMain] c.l.s.mylibrary.MyLibraryApplication     : Started MyLibraryApplication in 1.008 seconds (JVM running for 1.342)
2022-11-15 23:09:43.705  INFO 11896 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-11-15 23:09:43.705  INFO 11896 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-11-15 23:09:43.706  INFO 11896 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

在 main 中我创建了 webapp/WEB-INF/jsp/ 但没有帮助。

java spring spring-boot jsp
3个回答
3
投票

遇到 “JSP 限制”

运行使用嵌入式 servlet 容器(并打包为可执行存档)的 Spring Boot 应用程序时,JSP 支持存在一些限制。

  • 对于 Jetty 和 Tomcat,如果您使用

    war
    包装,它应该可以工作。可执行的 war 在使用
    java -jar
    启动时可以工作,并且也可以部署到任何标准容器。 使用可执行 jar 时不支持 JSP。

  • Undertow 不支持 JSP。

  • 创建自定义

    error.jsp

     页面不会覆盖错误处理的默认视图。应改用自定义错误页面。

在maven中(如果省略)默认打包是

jar

,所以请添加:

  • pom.xml
    <project> ... <packaging>war</packaging> ...
    
    
  • build.gradle
    plugins { ... id 'war' ... }
    
    
  • 或在
  • https://start.spring.io/#!packaging=war (也将 spring-boot-starter-tomcat
     设置为 
    prvoided
     范围(即将其从打包中排除/准备外部部署))
(及其所有后果。)

还请注意“提示”

如果可能,应避免 JSP

。将它们与嵌入式 servlet 容器一起使用时,存在一些“已知限制”。

我能够使用
https://stackoverflow.com/a/50846032/2137125

2
投票
https://stackoverflow.com/a/68619247/2137125

的一部分)让 JSP 与可执行 JAR 一起使用,以及https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1. 但正如第一个链接中的其他答案所说,支持的方式似乎是使用可执行的战争,即你可以java -jar something.war,虽然我没有尝试过,也许是因为我很固执。

编辑 2024:我已经从 JAR 上的 JSP 迁移出来,因为我们开始随机页面未加载,每次重新启动后页面都会更改,或者有时没有页面会受到影响。

    

尝试将您的

.properties

0
投票

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

如果它仍然不起作用,我想在控制台或堆栈跟踪中看到错误。

	

© www.soinside.com 2019 - 2024. All rights reserved.