我正在尝试使用注释来配置 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 时,根本没有打印任何内容。
那么我做错了什么?