在名称为'dispatcherServlet'的DispatcherServlet中找不到带有URI [/WEB-INF/pages/MainPage.jsp]的HTTP请求的映射 [英] No mapping found for HTTP request with URI [/WEB-INF/pages/MainPage.jsp] in DispatcherServlet with name 'dispatcherServlet'

查看:134
本文介绍了在名称为'dispatcherServlet'的DispatcherServlet中找不到带有URI [/WEB-INF/pages/MainPage.jsp]的HTTP请求的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用注释配置Spring Boot. 我上课

I am trying to configure Spring Boot using annotations. I have class

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

包含此View解析器,效果很好.

which contains this View resolver which works fine.

@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;
}

但是在收到JSP文件应用程序的名称后,引发此错误: 在名称为'dispatcherServlet'的DispatcherServlet中未找到具有URI [/WEB-INF/pages/MainPage.jsp]的HTTP请求的映射.

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

我找到了XML配置的解决方案:

I found solution for XML configuration:

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

但是我使用的是注释配置,所以这种灵魂不适合我.

But I am using annotation configuration, so this soultion is not suitable for me.

我尝试解决此问题,扩展了AbstractAnnotationConfigDispatcherServletInitializer

I tried to resolve this problem 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的示例,但是我仍然不明白应用程序如何在未注释且没有创建此类实例的情况下使用此类.只是声明而已.也许我需要创建此类的实例并将其附加到任何地方?

But nothing has changed after this. By the way I looked at few examples with AbstractAnnotationConfigDispatcherServletInitializer, but I still don't understand how application can use this class when it's not annotated and no instances of this class are created. It's just declared and that's all. Maybe I need to create an instance of this class and attach it anywhere?

无论如何,我在日志中看到以下行:映射servlet:'dispatcherServlet'到[/] 因此,看来我的servlet配置正确.

Anyway I see in log this line: Mapping servlet: 'dispatcherServlet' to [/] So it looks like I have right servlet configuration.

我尝试了此解决方案,但无济于事.我删除了InternalResourceViewResolver并创建了具有以下内容的application.properties:

I tried this solution but it doesn't help. I removed InternalResourceViewResolver and created application.properties with such content:

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

但是之后,我收到: javax.servlet.ServletException:无法在名称为'dispatcherServlet'的servlet中解析名称为'MainPage'的视图

那么解决此问题的正确方法是什么?

So what is the proper way to resolve this problem?

更新 我试图从头开始创建一个新的简单项目.

UPDATE I tried to create a new simple project from scratch.

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

项目的结构:

我使用命令mvn spring-boot运行项目:运行并接收 edu.test.Main:在2.365秒内启动Main(JVM运行5.476). 但是当我打开localhost:8080时,我收到:

I run project with command mvn spring-boot:run and receive edu.test.Main: Started Main in 2.365 seconds (JVM running for 5.476) in output. But when I'm opening localhost:8080 I receive:

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时,什么都没有输出. 那我在做什么错了?

But now I'm not receiving "No mapping found..." error in output. When I am opening localhost:8080 nothing is printed to output at all. So what am I doing wrong?

推荐答案

摆脱自定义视图解析器并设置应用程序属性是正确的开始. Spring Boot的全部意义在于,它可以很好地为您连接这些东西! :)

Getting rid of your custom view resolver and setting the application properties was the right start. The whole point of Spring Boot is that it does a pretty good job of wiring these things up for you! :)

您应该有一个带有请求映射的控制器.像这样:

You should have a controller with the request mappings. Something like:

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

...,它将使用您的MainPage.jsp模板处理对/的任何请求.

... which would use your MainPage.jsp template for any requests to /.

但是,值得注意的是,默认情况下,src/main/webapp的内容不会内置到可执行应用程序jar中.要与他打交道,我知道有两种选择.

However, it's worth noting that by default, the contents of src/main/webapp don't get built into the executable application jar. To deal with his there are a couple of options I know of.

选项1 -将所有内容从/src/main/webapp/移到src/main/resources/static.我认为这也适用于JSP.唯一的麻烦是,除非在IDE中运行应用程序,否则就可以热替换代码.

Option 1 - Move everything from /src/main/webapp/ to src/main/resources/static. I think this works for JSPs too. The only trouble here is that you can hot-replace code unless you're running the application in an IDE.

选项2 -另一种方法(我倾向于使用)是设置我的Maven构建,以便在构建时将src/main/webapp的内容复制到类路径static文件夹中.

Option 2 - The alternative (which I tend to use) is to set up my Maven build to copy the contents of src/main/webapp into the classpath static folder when I build.

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

为了进一步阅读(尽管看起来有点像您已经在做的事情),有一个示例项目,显示了一个使用JSP进行模板制作的Spring Boot应用程序:

For further reading (although this looks quite a bit like what you're already doing), there's a sample project, showing a Spring Boot app using JSP for templating:

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

这篇关于在名称为'dispatcherServlet'的DispatcherServlet中找不到带有URI [/WEB-INF/pages/MainPage.jsp]的HTTP请求的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆