在名为“dispatcherServlet”的 DispatcherServlet 中未找到带有 URI [/WEB-INF/pages/MainPage.jsp] 的 HTTP 请求的映射 [重复]

2024-04-02

我正在尝试使用注释来配置 Spring Boot。 我有课

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

其中包含这个工作正常的视图解析器。

@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 请求的映射。

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

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

但我使用的是注解配置,所以这个解决方案不适合我。

我尝试扩展 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 查看了一些示例,但我仍然不明白应用程序在未注释且未创建此类实例的情况下如何使用此类。这只是宣布而已。也许我需要创建此类的实例并将其附加到任何地方?

无论如何,我在日志中看到这一行:将 servlet: 'dispatcherServlet' 映射到 [/]所以看起来我有正确的 servlet 配置。

I tried 这个解决方案 https://stackoverflow.com/questions/26366806/no-mapping-found-for-http-request-with-uri-spring-4-1-annotation-configuration但这没有帮助。我删除了InternalResourceViewResolver并创建了具有以下内容的application.properties:

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

但之后我收到:javax.servlet.ServletException:无法解析名为“dispatcherServlet”的 servlet 中名为“MainPage”的视图

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

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>

主程序.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);
    }
}

应用程序属性:

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

项目结构:

我使用命令 mvn spring-boot:run 运行项目并接收edu.test.Main:在 2.365 秒内启动 Main(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 时,根本没有打印任何内容。 那么我做错了什么?


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

您应该有一个带有请求映射的控制器。就像是:

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

...这会使用你的MainPage.jsp任何请求的模板/.

不过,值得注意的是,默认情况下,src/main/webapp不要内置到可执行应用程序 jar 中。为了对付他,我知道有几种选择。

Option 1- 将所有东西从/src/main/webapp/ to src/main/resources/static. I think这也适用于 JSP。这里唯一的问题是,除非您在 IDE 中运行应用程序,否则您可以热替换代码。

Option 2- 另一种选择(我倾向于使用)是设置我的 Maven 构建来复制以下内容src/main/webapp进入类路径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>

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在名为“dispatcherServlet”的 DispatcherServlet 中未找到带有 URI [/WEB-INF/pages/MainPage.jsp] 的 HTTP 请求的映射 [重复] 的相关文章

随机推荐