Karthik
Karthik

Reputation: 1342

Spring boot not able to recognize JSP

I have configured Spring Boot using annotations.
I have the following files
1)AppStarter class for configuring spring boot

 
        @Configuration
        @EnableAutoConfiguration
        @PropertySource(value = "classpath:app.properties", ignoreResourceNotFound = true)
        @ComponentScan(basePackages = "com.sample.config")
        public class AppStarter extends SpringBootServletInitializer{

        @Value("${server.contextPath}")
        private String contextPath;

        @Value("${server.port:8080}")
        private String port;

        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }

        @Bean
        public EmbeddedServletContainerFactory servletContainer() {
            TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
            factory.setPort(Integer.valueOf(port));
            factory.setContextPath(contextPath);
            factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
            return factory;
        }

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(AppStarter.class);
        }

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

2)WebConfig class



        @Configuration
        @ComponentScan(basePackages = {"com.sample.rest"})
        @EnableWebMvc
        public class WebConfig extends WebMvcConfigurerAdapter
        {
            @Bean
            public static PropertySourcesPlaceholderConfigurer properties()
            {
                PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
                Resource[] resources = new ClassPathResource[]{new ClassPathResource("app.properties")};
                configurer.setLocations(resources);
                configurer.setIgnoreUnresolvablePlaceholders(true);
                return configurer;
            }


            @Bean
            public InternalResourceViewResolver viewResolver()
            {
                InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
                viewResolver.setPrefix("/WEB-INF/pages/");
                viewResolver.setSuffix(".jsp");
                return viewResolver;
            }
        }

3)app.properties

core_pool_size = 100
max_pool_size = 600
queue_capacity = 160
server.port=8080
spring.view.prefix: /WEB-INF/pages/
spring.view.suffix: .jsp

4)UserController class

@Controller
public class UserController extends AbstractController {

    @RequestMapping(value = "/user/add")
    public String user()
    {
       return "adduser";
    }
} 

5)pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        <version>1.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
        <version>0.16.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>
    <dependency>
        <groupId>net.sf.dozer</groupId>
        <artifactId>dozer</artifactId>
        <version>5.5.1</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
            <exclusion>
                <artifactId>org.slf4j</artifactId>
                <groupId>slf4j-log4j12</groupId>
            </exclusion>
            <exclusion>
                <artifactId>log4j</artifactId>
                <groupId>log4j</groupId>
            </exclusion>
        </exclusions>
    </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>
        <scope>provided</scope>
    </dependency>
    <!--Spring boot test-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>1.2.0.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>4.3.7.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-java.version}</version>
        <scope>runtime</scope>
    </dependency>
 </dependencies>

6)adduser.jsp which is in webapp/WEB_INF/pages folder

When I try to access my jsp through localhost:8080/sample/user/add i get :

org.springframework.web.servlet.PageNotFound | No mapping found for HTTP request with URI [/sample/WEB-INF/pages/adduser.jsp] in DispatcherServlet with name 'dispatcherServlet'

Can anyone could provide any help on this issue?

Upvotes: 0

Views: 2602

Answers (4)

Lin W
Lin W

Reputation: 299

Try this:

  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer   configurer) {
      configurer.enable();
  }

Upvotes: 0

Ralph
Ralph

Reputation: 120761

  • You configured: spring.view.prefix: /WEB-INF/jsp/
  • But your folder is: webapp/WEB_INF/pages

/jsp vs, /pages

You need to change one of them, so that they match!


Second: you need to request the url (That is written at your controller but not the path of the jsp!

So use: localhost:8080/<yourAppName>/user/add instead of /sample/WEB-INF/pages/adduser.jsp

Upvotes: 1

zmitrok
zmitrok

Reputation: 378

It looks like you have an embedded Tomcat container configured. JSP pages are not supported when Tomcat is embedded. http://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations

Upvotes: 0

faljbour
faljbour

Reputation: 1377

to use localhost:8080/samples/user/add, you would need to change the controller code to this,

@Controller
@RequestMapping("/samples")
public class UserController extends AbstractController {

    @RequestMapping(value = "/user/add")
    public String user()
    {
       return "adduser";
    }
} 

Upvotes: 0

Related Questions