博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring4 In Action-7.1.2-自定义DispatcherServlet配置、添加其他的Servlet和Filter
阅读量:5423 次
发布时间:2019-06-15

本文共 8166 字,大约阅读时间需要 27 分钟。

的实现类,加载Web的基本配置

代码下载:http://download.csdn.net/download/poiuy1991719/10128895

 

编写SpitterWebInitializer类:

package spittr.config;import javax.servlet.MultipartConfigElement;import javax.servlet.ServletRegistration.Dynamic;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class SpitterWebInitializer extends        AbstractAnnotationConfigDispatcherServletInitializer {    @Override    protected Class
[] getRootConfigClasses() { return new Class
[] { RootConfig.class }; } @Override protected Class
[] getServletConfigClasses() { return new Class
[] { WebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } @Override protected void customizeRegistration(Dynamic registration) {
// 对DispatcherServlet进行额外配置 registration.setMultipartConfig(new MultipartConfigElement( "/tmp/spittr/uploads", 2097152, 4194304, 0)); }}
View Code

 

编写Web配置类:

package spittr.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.InternalResourceViewResolver;/*** * 配置web */@Configuration //配置类,需要的注解@EnableWebMvc  //启用Spring MVC@ComponentScan("spittr.web")  //启用组件扫描,就会扫描到控制器public class WebConfig extends WebMvcConfigurerAdapter {    @Bean    public ViewResolver viewResolver() {
//配置JSP视图解析器 InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/");//在请求路径前,加入资源路径 resolver.setSuffix(".jsp");//在请求路径后,加入资源名称,比如/WEB-INF/views/home.jsp resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class); return resolver; } @Override public void configureDefaultServletHandling( //处理静态资源 DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { super.addResourceHandlers(registry); }}
View Code

 

编写其他组件启动类:

package spittr.config;import java.util.regex.Pattern;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.context.annotation.Import;import org.springframework.core.type.filter.RegexPatternTypeFilter;import spittr.config.RootConfig.WebPackage;@Configuration@Import(DataConfig.class)@ComponentScan(basePackages = { "spittr" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = WebPackage.class) })public class RootConfig {    public static class WebPackage extends RegexPatternTypeFilter {        public WebPackage() {            super(Pattern.compile("spittr\\.web"));        }    }}
View Code
package spittr.config;import javax.sql.DataSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.core.JdbcOperations;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DriverManagerDataSource;@Configurationpublic class DataConfig {    @Bean    public DataSource dataSource() {        DriverManagerDataSource dataSource = new DriverManagerDataSource();        dataSource.setDriverClassName("com.mysql.jdbc.Driver");        dataSource.setUrl("jdbc:mysql:///spittle");        dataSource.setUsername("root");        dataSource.setPassword("root");        return dataSource;    }    @Bean    public JdbcOperations jdbcTemplate(DataSource dataSource) {        return new JdbcTemplate(dataSource);    }}
View Code

 

基本工作完成,下面就开始进入正题:

一、添加另外的自定义Servlet:WebApplicationInitializer类的实现:仅仅完成基本的输出

package com.regist.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyServlet extends HttpServlet {    public MyServlet() {        super();    }    public void destroy() {        super.destroy();    }    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        out.println("");        out.println("");        out.println("  A Servlet");        out.println("  ");        out.print("    This is ");        out.print(this.getClass());        out.println(", using the GET method");        out.println("  ");        out.println("");        out.flush();        out.close();    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        out.println("");        out.println("");        out.println("  A Servlet");        out.println("  ");        out.print("    This is ");        out.print(this.getClass());        out.println(", using the POST method");        out.println("  ");        out.println("");        out.flush();        out.close();    }    public void init() throws ServletException {    }}
View Code
package com.regist.servlet.config;import javax.servlet.ServletRegistration.Dynamic;import javax.servlet.ServletContext;import javax.servlet.ServletException;import org.springframework.web.WebApplicationInitializer;import com.regist.servlet.MyServlet;/*** * 往容器中,注册其他的组件,只需实现WebApplicationInitializer接口即可 * 本方法,注册了一个Servlet */public class MyServletInitializer implements WebApplicationInitializer{    @Override    public void onStartup(ServletContext servletContext) throws ServletException {        System.out.println("-------------------------------------");        Dynamic myServlet = servletContext.addServlet("myServlet", MyServlet.class);//注册Servlet            myServlet.addMapping("/myServlet/*");//映射路径    }}
View Code

 

二、添加Filter拦截器:WebApplicationInitializer类的实现:

package com.regist.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;public class MyFilter implements Filter {    // 来访数量    private int count;    @Override    public void destroy() {    }    @Override    public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws IOException, ServletException {        System.out.println("------------过滤器执行");        chain.doFilter(request, response); // 向下传递过滤器    }    @Override    public void init(FilterConfig filterConfig) throws ServletException {    }}
View Code
package com.regist.filter.config;import javax.servlet.FilterRegistration.Dynamic;import javax.servlet.ServletContext;import javax.servlet.ServletException;import org.springframework.web.WebApplicationInitializer;import com.regist.filter.MyFilter;public class MyFilterInitializer implements WebApplicationInitializer{    @Override    public void onStartup(ServletContext servletContext) throws ServletException {        System.out.println("-----------------class:MyFilterInitializer-------------------");        Dynamic filter = servletContext.addFilter("myFilter", MyFilter.class);        filter.addMappingForUrlPatterns(null, false, "/myServlet/*");//映射路径    }}
View Code

 

转载于:https://www.cnblogs.com/zjsy/p/7880180.html

你可能感兴趣的文章
第03次作业-栈和队列
查看>>
[SDOI2015]约数个数和
查看>>
关于简单的gridview学习笔记
查看>>
ES6学习--搭建环境
查看>>
技术网站收藏
查看>>
插上翅膀,让Excel飞起来——xlwings(四)
查看>>
【Python】排列组合itertools & 集合set
查看>>
优秀的JavaScript开发框架
查看>>
“机器人”来了,你可能没有时间思考变与不变了
查看>>
22 广播小小总结
查看>>
android 之 Dialog
查看>>
127 Word Ladder
查看>>
js获取倒计时
查看>>
loadrunner安装过程中的,注册表问题
查看>>
Browsersyuc 入门
查看>>
git push失败the remote end hung up unexpectedly
查看>>
POJ3087 Shuffle'm Up 简单模拟
查看>>
Django中数据的增删改查
查看>>
iOS模拟器发生了崩溃,去哪找Crash Log
查看>>
[支付宝]即时到账接口对接总结
查看>>