标签:pat The 前缀 ini ping ota webconfig 适配 http
<dependencies>
<!--spring核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!--springmvc核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!--servlet依赖,provided是因为部署到tomcat后,tomcat本身提供servlet,本servlet就会被Tomcat自带的servlet替代-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--jsp依赖,provided是因为部署到tomcat后,tomcat本身提供-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
注:servlet版本要在3.0以上。
取消web.xml的依据
对于每一个应用,在应用启动时,由Tomcat容器创建一个
ServletContainerInitializer 实例。 框架提供的 ServletContainerInitializer 实现必须绑定在 jar 包 的
META-INF/services 目录中的一个叫做 javax.servlet.ServletContainerInitializer 的文件,根据 jar services API,
指定 ServletContainerInitializer 的实现。
文件内容:org.springframework.web.SpringServletContainerInitializer
Tomcat启动时会创建这个接口,但接口无法创建对象,最后会创建接口的实现类,我们需要自定义一个和他有实现关系的类即可,可代替web.xml文件。
graph TD
A[WebApplicationInitializer]-->B[AbstractContextLoaderInitializer]
B-->C[AbstractDispatherServletInitializer]
C-->D[AbstractAnnotationConfigDispatherServletInitializer]
//继承 AbstractDispatcherServletInitializer类(servlet3.0后)
public class MyWebXML extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext cxt = new XmlWebApplicationContext();
//springmvc xml配置文件位置
cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
return cxt;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--配置视图解析器:前缀、后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置映射路径-->
<bean name="/login" class="com.hello.controller.LoginController"/>
<aop:annotation-driven/>
<context:component-scan base-package="com.hello.controller"/>
<!--servlet容器能处理的映射,springMvc不拦截-->
<mvc:default-servlet-handler/>
</beans>
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
//导入springmvc配置类
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { MyWebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
@Configuration
@EnableWebMvc //相当于<aop:annotation-driven/>
@ComponentScan(basePackages = "com.hello.controller") //相当于<context:component-scan base-package="com.offcn.controller"/>
public class MyWebConfig implements WebMvcConfigurer{
//配置视图解析器:前缀、后缀
@Bean
public InternalResourceViewResolver internalResourceViewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
//<!--servlet容器能处理的映射,springMvc不拦截-->
//<mvc:default-servlet-handler/>
//需要实现WebMvcConfigurer重写其中的configureDefaultServletHandling方法
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
标签:pat The 前缀 ini ping ota webconfig 适配 http
原文地址:https://www.cnblogs.com/ygfcoder/p/13406098.html