filter,servlet添加到springmvc中

注解的方式

ublic class WebAppInit implements WebApplicationInitializer {
    @Override
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(WebMvcConfig .class);
        // ac.refresh();此处应该注释,否则报错

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(ac);
        ServletRegistration.Dynamic registration = servletContext.addServlet("DispatcherServlet", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/");

       

        FilterRegistration.Dynamic f1 = servletContext.addFilter("f1", new MyFilter1());
        f1.addMappingForUrlPatterns(null,false,"/h11");

        FilterRegistration.Dynamic f2 = servletContext.addFilter("f2", new MyFilter2());
        f2.addMappingForUrlPatterns(null,false,"/h22");


    }

 

posted @ 2018-05-25 16:08  chenhonggao  阅读(312)  评论(0编辑  收藏  举报