我正在开发一个项目,它不是Spring Boot,而是Spring MVC。我的意思是,我的项目中没有这个类,例如:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
我有三个用于Spring MVC配置文件的类:
@Import(WebSocketConfig.class)
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "......")
public class MainConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/Content/**")
.addResourceLocations("/Content/");
registry.addResourceHandler("/Scripts/**")
.addResourceLocations("/Scripts/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
第二点:
public class MainInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
public static HashMap<String, String> response_code = new HashMap<String, String>();
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { MainConfiguration.class,
WebSocketConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
Security.addProvider(new BouncyCastleProvider());
servletContext.addListener(new MainContextListener());
System.out.println("MainInitializer.onStartup()");
}}
第三,
public class MainContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Context Initialized");
Security.addProvider(new BouncyCastleProvider());
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Shutting down!");
}
}
有一个控制器和jsp文件,我已经配置好在Tomcat Web服务器上运行,对我来说有点奇怪的是,当我将这段代码片段添加到我的pom中时,index.jsp会在浏览器中准确地显示,但当我删除它时,它会给出404未找到控制器的URL。为什么即使我的项目不是Spring Boot项目也需要Spring Boot Starter Parent?我认为下面的代码与Spring Boot相关,因为我的项目不是Spring Boot而是Spring MVC,所以不需要它。但是如果没有将此代码添加到pom中,就会出现问题:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>