springmvc完全注解+sitemesh拦截

干掉web.xml

基于servelet3.0,完全注解,将web.xml消灭掉。

WebApplicationInitializer代替web.xml工作

基于注解的方式,在应用启动之前,使用该类去注册相关拦截器和servlet。这里主要说springmvc的注册,主要是注册dispatcherservlet,初始化web的代码如下:

public class WebInitializer implements WebApplicationInitializer{

    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        //注册springmvc
        ctx.register(SpringMvcConfig.class);
        ctx.setServletContext(servletContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
        //拦截所有请求
        servlet.addMapping("/");

        servlet.setLoadOnStartup(1);
    }



}

springmvc配置

通过注解的形式进行springmvc的配置,代替原始的springmvc.xml。

/**
 * springmvc的配置
 * 注解解释为:
 * 配置类
 * 允许使用springmvc,启用之后会开启一些默认配置,比如ViewResolver
 * 扫描包为com.cx.os
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.cx.os")
public class SpringMvcConfig extends WebMvcConfigurerAdapter{
    @Bean
    public InternalResourceViewResolver viewResolver(){
        //viewresolver来渲染页面
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        //指定渲染页面路径
        viewResolver.setPrefix("/WEB-INF/views/");
        //指定页面格式
        viewResolver.setSuffix(".jsp");
        //使用jstl标准进行页面的渲染
        viewResolver.setViewClass(JstlView.class);
        return  viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/statics/**").addResourceLocations("/WEB-INF/statics/");
    }

这里涉及到springmvc的一个渲染器和静态资源的注册。

  1. 渲染器:主要有jstl和jsp两种,在设置viewclass之前需要设置渲染页面的路径和页面的格式,使用如下方法进行设置:
 //指定渲染页面路径
        viewResolver.setPrefix("/WEB-INF/views/");
        //指定页面格式
        viewResolver.setSuffix(".jsp");
  1. 静态资源注册
    springmvc默认拦截了所有页面请求,同时包括css和js文件。重写addResourceHandlers方法进行资源拦截,其中addResourceHandler为请求路径,addResourceLocations为实际路径。
    验证静态资源是否设置成功的方式:
    直接在浏览器访问资源文件如:localhost:8080/statics/js/jquery.js,看是否正常访问到资源。

sitemesh拦截器

sitemesh3.0提供了基于注解的配置方式,集成ConfigurableSiteMeshFilter之后,重写applyCustomConfiguration方法。

@WebFilter(filterName = "sitemesh",urlPatterns = {"/*"})
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter{
    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        //使用decorator.jsp去装配所有的页面
        builder.addDecoratorPath("/*","/decorator.jsp");
    }
}

其中,webfilter取代web.xml的拦截器。而重写的applyCustomConfiguration方法里面对应的addDecoratorPath方法两个参数分别为,装饰页面和装饰模板。上面代码表示使用decorator.jsp装饰所有页面。这里需要注意一下装饰模板的路径。

注意事项

  1. 在进行maven打包的时候报错,找不到web.xml文件。通过配置pom文件解决该问题:
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
          <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
          </configuration>
      </plugin>
  1. 注意使用maven tomcat插件的时候,该插件默认打包的web路径为webapp,将展示jsp和html文件放在webapp下面可以正常访问,放到resources下面则不能正常访问。
    如何去配置打包属性[warSourceDirectory],暂未找出合理方法
    见apache官方文档,这里面有讲解,暂未找到解决方案。
    如果,你的资源文件在resources下面,那么只能使用tomcat发布或者将资源文件放到webapp下面。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,496评论 6赞 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,434评论 19赞 139
  • 在严格高要求家庭中长大的我,一直还算努力。如果用时间观念强、自律、有想法等所谓成功人士必备的品质来标榜自己,也不算...
    2a23bc5d6f2d阅读 283评论 0赞 0
  • 01 因公司业务扩大,进了几个新同事。 一个多月后某天,上班路上遇到新同事爱丽丝。她跟我抱怨:这段时间的工作太基础...
    温姬拉阅读 567评论 1赞 1

友情链接更多精彩内容