SpringMVC基于代码的配置方式(零配置,无web.xml)
来源:互联网 发布:冰川网络收购棋牌游戏 编辑:程序博客网 时间:2024/06/05 05:56
基于配置文件的web项目维护起来可能会更方便,但是有时候我们会有一些特殊的需求,比如防止客户胡乱更改配置,这时候我们需要给配置隐藏到代码中。
1.创建一个动态web项目(无需web.xml)
2.右键项目添加几个package: com.easyweb.config (保存项目配置) com.easyweb.controller (保存springMvc controller)
3.在 com.easyweb.config 新建一个类 WebApplicationStartup ,这个类实现WebApplicationInitializer 接口,是项目的入口,作用类似于web.xml,具体代码如下:
package com.easyweb.config;import javax.servlet.MultipartConfigElement;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration.Dynamic;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;/** * 服务器启动入口类 * * @author Administrator * */public class WebApplicationStartup implements WebApplicationInitializer { private static final String SERVLET_NAME = "Spring-mvc"; private static final long MAX_FILE_UPLOAD_SIZE = 1024 * 1024 * 5; // 5 Mb private static final int FILE_SIZE_THRESHOLD = 1024 * 1024; // After 1Mb private static final long MAX_REQUEST_SIZE = -1L; // No request size limit /** * 服务器启动调用此方法,在这里可以做配置 作用与web.xml相同 */ @Override public void onStartup(ServletContext servletContext) throws ServletException { // 注册springMvc的servlet this.addServlet(servletContext); // 注册过滤器 // servletContext.addFilter(arg0, arg1) // 注册监听器 // servletContext.addListener(arg0); } /** * 注册Spring servlet * * @param servletContext */ private void addServlet(ServletContext servletContext) { // 构建一个application context AnnotationConfigWebApplicationContext webContext = createWebContext(SpringMVC.class, ViewConfiguration.class); // 注册spring mvc 的 servlet Dynamic dynamic = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(webContext)); // 添加springMVC 允许访问的Controller后缀 dynamic.addMapping("*.html", "*.ajax", "*.css", "*.js", "*.gif", "*.jpg", "*.png"); // 全部通过请用 “/” // dynamic.addMapping("/"); dynamic.setLoadOnStartup(1); dynamic.setMultipartConfig(new MultipartConfigElement(null, MAX_FILE_UPLOAD_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD)); } /** * 通过自定义的配置类来实例化一个Web Application Context * * @param annotatedClasses * @return */ private AnnotationConfigWebApplicationContext createWebContext(Class<?>... annotatedClasses) { AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext(); webContext.register(annotatedClasses); return webContext; }}
4.在com.easyweb.config 下添加类 SpringMVC 继承 WebMvcConfigurerAdapter,这个类的作用是进行SpringMVC的一些配置,代码如下:
package com.easyweb.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;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;@Configuration@EnableWebMvc//指明controller所在的包名@ComponentScan(basePackages = {"com.easyweb.controller"})public class SpringMVC extends WebMvcConfigurerAdapter { /** * 非必须 */ @Override public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) { configurer.enable(); } /** * 如果项目的一些资源文件放在/WEB-INF/resources/下面 * 在浏览器访问的地址就是类似:http://host:port/projectName/WEB-INF/resources/xxx.css * 但是加了如下定义之后就可以这样访问: * http://host:port/projectName/resources/xxx.css * 非必须 */ @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**/*").addResourceLocations("/WEB-INF/resources/"); }}
5.添加view配置文件com.easyweb.config下新建类ViewConfiguration,里面可以根据自己的需要定义视图拦截器:
package com.easyweb.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.view.JstlView;import org.springframework.web.servlet.view.UrlBasedViewResolver;import org.springframework.web.servlet.view.tiles2.TilesConfigurer;import org.springframework.web.servlet.view.tiles2.TilesView;@Configurationpublic class ViewConfiguration { @Bean public ViewResolver urlBasedViewResolver() { UrlBasedViewResolver viewResolver; viewResolver = new UrlBasedViewResolver(); viewResolver.setOrder(2); viewResolver.setPrefix("/WEB-INF/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); // for debug envirment viewResolver.setCache(false); return viewResolver; } @Bean public ViewResolver tilesViewResolver() { UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver(); urlBasedViewResolver.setOrder(1); urlBasedViewResolver.setViewClass(TilesView.class); //urlBasedViewResolver. return urlBasedViewResolver; } @Bean public TilesConfigurer tilesConfigurer() { TilesConfigurer tilesConfigurer = new TilesConfigurer(); tilesConfigurer.setDefinitions(new String[] { "classpath:tiles.xml" }); return tilesConfigurer; }}
6.本例中还用了tiles视图解析器,替换了原始的include方式
7.完整代码已上传
http://download.csdn.net/detail/u013816347/8998891
学习路上,欢迎评论指正。
1 2
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- springmvc基于xml文件的配置方式
- web.xml配置,基于SpringMVC
- 探 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用
- 究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用
- Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用
- SpringMVC的XML配置方式
- 三,基于Java配置、无web.xml文件方式配置Spring MVC
- SpringMVC的web.xml配置
- 基于Annotation的零配置方式--AspectJ
- 基于xml配置springmvc
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置
- SpringMVC web.xml配置
- SpringMVC web.xml配置
- SpringMVC4零配置--web.xml
- hdu 5147 Sequence II(前缀和,后缀和)
- scp命令的实际应用
- Struts2动态方法调用
- 《招聘一个靠谱的iOS》面试题参考答案(上)
- 统计数n阶乘中0的位数
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- 随机生成id,不会重复
- 图论基础UVA10047
- 【leetcode每日一题】NO206.Reverse Linked List
- HDU 5218 The E-pang Palace (简单几何—2014广州现场赛)
- 我学正则表达式(2)——{m,n},?,*,+
- hunnu oj 11544 小明的烦恼——找字符串(字符串的最小表示法)
- LeetCode 题解(164): Subsets
- Highmaps网页图表教程之下载Highmaps与Highmaps的地图类型