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文件解决该问题:

        org.apache.maven.plugins
        maven-war-plugin
        2.3
          
            false
          
      
  1. 注意使用maven tomcat插件的时候,该插件默认打包的web路径为webapp,将展示jsp和html文件放在webapp下面可以正常访问,放到resources下面则不能正常访问。
    如何去配置打包属性[warSourceDirectory],暂未找出合理方法
    见apache官方文档,这里面有讲解,暂未找到解决方案。
    如果,你的资源文件在resources下面,那么只能使用tomcat发布或者将资源文件放到webapp下面。

你可能感兴趣的:(springmvc完全注解+sitemesh拦截)