带有 JSP 的 Spring Boot 应用程序不能作为独立的 jar 工作,而是在 IntelliJ 中工作

时间:2021-05-19 01:27:02

标签: java spring spring-boot spring-mvc jsp

我有一个简单的应用程序,它应该根据提供的 URL 加载一些简单的 JSP 页面

  • http://localhost:9191/mypath/ --> 应该加载 page1.jsp
  • http://localhost:9191/mypath/first.do --> 应该加载 page1.jsp
  • http://localhost:9191/mypath/second.do --> 应该加载 page2.jsp

在 IntelliJ 中使用应用程序时,上述 URL 会按预期工作。

但是,在启动应用程序时 - java -jar app.jar --spring.config.location=file:./application.properties 它为所有上述 URL 返回 404。

无法弄清楚原因,如果有人能指出我可能遗漏的任何内容,我们将不胜感激。

我的项目详情如下。没有提供 JSP 的详细信息,因为它们是简单的页面 一行文字。

Java version        : 11.9
Spring Boot version : 2.3.4.RELEASE
Spring Version      : 5.2.9.RELEASE

Maven 项目结构

├── src
│   └── main
│       ├── java
│       │   └── com.springjsp
│       │       └── config
|       |           └── Config.java
│       │       └── controller
|       |           └── TestController.java
│       │       └── App.java
│       ├── resources
│       │   └── application.properties
│       └── webapp
|           └── WEB-INF
│               └── jsp
│                   └── ui
│                       └── page1.jsp
│                       └── page2.jsp
│                       └── error.jsp

App.java

@SpringBootApplication
public class App {
       public static void main(String[] args){SpringApplication.run(App.class, args);}

Config.java

@Configuration
public class Config {
       @Bean
       TestController page1(){
               return new TestController("/ui/page1","/ui/error");
       }

       @Bean
       TestController page2(){
               return new TestController("/ui/page2","/ui/error");
       }

       @Bean
       SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
              SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
              simpleUrlHandlerMapping.setOrder(1);
              Map<String, Object> mappings = new HashMap<>();
              mappings.put("", page1());
              mappings.put("first.do", page1());
              mappings.put("second.do", page2());
              simpleUrlHandlerMapping.setUrlMap(mappings);
              return simpleUrlHandlerMapping;
       }
}         

TestController.java

public class TestController implements Controller {
    private String formView;
    private String errorView;

    public TestController(String formView, String errorView) {
        this.formView = formView;
        this.errorView = errorView;
    }

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            return new ModelAndView(formView);
        } catch (Exception ex) {
            return new ModelAndView(errorView);
        }
    }
}

application.properties

server.port=9191
server.servlet.context-path=/mypath

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

pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
    </dependencies>

        <resources>
            <resource>
                <directory>${basedir}/src/main/resources/</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>${basedir}/src/main/webapp</directory>
            </resource>
        </resources>

来自控制台的错误

2021-05-19T11:48:34.387 [http-nio-11970-exec-2] INFO  o.a.c.c.C.[.[localhost].[/mypath] - Initializing Spring DispatcherServlet 'dispatcherServlet'
    2021-05-19T11:48:34.388 [http-nio-11970-exec-2] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
    2021-05-19T11:48:34.388 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Detected StandardServletMultipartResolver
    2021-05-19T11:48:34.399 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
    2021-05-19T11:48:34.399 [http-nio-11970-exec-2] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 11 ms
    2021-05-19T11:48:34.412 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - GET "/mypath/", parameters={}
    2021-05-19T11:48:34.420 [http-nio-11970-exec-2] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapped to com.springjsp.controllers.TestController@e638281
    2021-05-19T11:48:34.424 [http-nio-11970-exec-2] DEBUG o.s.w.s.v.ContentNegotiatingViewResolver - Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
    2021-05-19T11:48:34.425 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.view.JstlView - View name '/ui/page1', model {}
    2021-05-19T11:48:34.432 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.view.JstlView - Forwarding to [/WEB-INF/jsp//ui/page1.jsp]
    2021-05-19T11:48:34.436 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND
    2021-05-19T11:48:34.437 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - "ERROR" dispatch for GET "/mypath/error", parameters={}
    2021-05-19T11:48:34.440 [http-nio-11970-exec-2] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
    2021-05-19T11:48:34.489 [http-nio-11970-exec-2] DEBUG o.s.w.s.v.ContentNegotiatingViewResolver - Selected 'text/html' given [text/html, text/html;q=0.8]
    2021-05-19T11:48:34.494 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Exiting from "ERROR" dispatch, status 404

1 个答案:

答案 0 :(得分:0)

JSPs are not supported when using jar packaging。您应该通过在其 pom.xml 中设置 <packaging>war</packaging> 来将您的应用程序打包为一个 war 文件。打包为 war 时,它仍然可以使用 java -jar your-app.war 运行。