SpringMVC基本配置
创始人
2024-04-02 06:57:34
0次
-
小常规
- springmvc的处理器对应的bean必须按照规范格式开发,为避免加入无效的bean可通过bean加载过滤器进行包含设定或排除设定,表现层bean标注通常设定为@Controller

- 在此发现图片没有加载出来
- 回到程序去分析
- 当发起一个请求以后
- DispatcherServlet配置拦截所有请求/
- 这就有问题了
- 这使静态资源无法成功加载

-


- 核心控制器拦截的是所有请求,需要对非普通资源请求进行放行,通过配置放行资源实现
- 使用简化格式可以放行所有普通资源调用,无需一一枚举
-
注解驱动
- 目标就是把原有spring-mvc.xml与web.xml文件干掉
- 干掉spring-mvc.xml
![]()
-
@Configuration
@ComponentScan(value = "com.superdemo",includeFilters =@ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class}))
public class SpringMVCConfiguration implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {WebMvcConfigurer.super.addResourceHandlers(registry);registry.addResourceHandler("/static.img/**").addResourceLocations("/static.img/");registry.addResourceHandler("/js/**").addResourceLocations("/js/");registry.addResourceHandler("/css/**").addResourceLocations("/css/");}//放行所有资源@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {WebMvcConfigurer.super.configureDefaultServletHandling(configurer);configurer.enable();}
}
- 干掉web.xml

-
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {@Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();ctx.register(SpringMVCConfiguration.class);return ctx;}@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}@Overrideprotected WebApplicationContext createRootApplicationContext() {return null;}
}
相关内容