No mapping was found for the HTTP request with the URI [/WEB-INF/pages/MainPage.jsp] in the DispatcherServlet named 'dispatcherServlet'

I am trying to customize Spring boot using annotations. I have a class

@EnableWebMvc
@Configuration
@ComponentScan({
    ...
})
@EnableTransactionManagement
@EnableAutoConfiguration
@Import({ SecurityConfig.class })
public class AppConfig extends SpringBootServletInitializer {...}

which contains this prominent resolver that works great.

@Bean
public InternalResourceViewResolver internalViewResolver() {
    InternalResourceViewResolver viewResolver
            = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/pages/");
    viewResolver.setSuffix(".jsp");
    viewResolver.setOrder(1);
    return viewResolver;
}

But after receiving the application name of the JSP file, execute this error: No mapping was found for the HTTP request with the URI [/WEB-INF/pages/MainPage.jsp] in the DispatcherServlet named "dispatcherServlet".

I found a solution for XML configuration:

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 

But I use the configuration of annotations, so this soul is not suitable for me.

I tried to solve this problem by extending AbstractAnnotationConfigDispatcherServletInitializer

public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    //  I thought this method will be equivalent to XML config solution described above
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

. , AbstractAnnotationConfigDispatcherServletInitializer, , , , . . , ?

, : Mapping servlet: 'dispatcherServlet' to [/] , , .

, . InternalResourceViewResolver application.properties :

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

: javax.servlet.ServletException: "MainPage" "dispatcherServlet"

, ?

UPDATE .

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>edu.springtest</groupId>
    <artifactId>SpringTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

Main.java

@Controller
@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class Main {
    @RequestMapping("/")
    String home() {
        return "hello";
    }

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

application.properties

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

:

Project's structure

mvn spring -boot: edu.test.Main: Main 2.365 ( JVM 5.476). localhost: 8080, :

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Dec 20 11:25:29 EET 2014
There was an unexpected error (type=Not Found, status=404).
No message available

" ..." . localhost: 8080 . ?

+4
4

. Spring Boot , !:)

. - :

@RequestMapping("/")
public String mainPage() {
    return "MainPage";
}

... MainPage.jsp /.

, src/main/webapp . , , .

1. /src/main/webapp/ src/main/resources/static. , JSP. , , IDE.

2. ( ) , Maven src/main/webapp classpath static .

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

( , ), , Spring JSP :

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-web-jsp/

+1

, , ​​

​​ CachedResource tomcat-embed-core.jar, , , Eclipse, , :

"C:/MyProject/MyApplication/src/main/webapp/WEB-INF/views/jsp/index.jsp"

, "java -jar...", temp likes:

"/tmp/tomcat-docbase.3976292633605332325.8080/WEB-INF/views/jsp/index.jsp"

/tmp/tomcat -docbase.3976292633605332325.8080 . , , , InternalResourceViewResolver , pathpath. , ResourceHandler.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    registry.addResourceHandler("/WEB-INF/**").addResourceLocations("classpath:/WEB-INF/");
}

, , jsp 404.

0

Intellij IDEA.

tomcat "" . . /Myroot

, -!

0
source
  • You verify that MainPage.jsp is present in WEB-INF / pages / folder.
  • change public class AppConfig extends WebMvcConfigurerAdapter

Change Bean as:

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

May it help you

-1
source

Source: https://habr.com/ru/post/1568328/


All Articles