1

I have a simple application which should load some simple JSP pages as per the URLs provided

  • http://localhost:9191/mypath/ --> Should load page1.jsp
  • http://localhost:9191/mypath/first.do --> Should load page1.jsp
  • http://localhost:9191/mypath/second.do --> Should load page2.jsp

On using the application from within IntelliJ the above URLs work as expected.

However, on starting the application with - java -jar app.jar --spring.config.location=file:./application.properties it returns 404 for all the above mentioned URL's.

Unable to figure out why and would appreciate if someone can point me to anything I might be missing.

Details of my project as below. Haven't provided the details of the JSP as they are simple pages with one line of text.

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

Maven Project structure

├── 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>

Error from Console

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
insearch
  • 11
  • 4

1 Answers1

1

JSPs are not supported when using jar packaging. You should package your application as a war file instead by setting <packaging>war</packaging> in its pom. When packaged as a war, it can still be run using java -jar your-app.war.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242