spring3.2之后开始支持java配置方式开发web项目,不使用web.xml,但需要在servlet3.0环境,一般tomcat7会支持,6不行

下图中:MyAppInitializer和SpringServletContainerInitializer是查找关系,没有继承

创建maven的web项目

容器首先会加载这个类

package org.spitter.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

//	抽象类的实现类会自动地配置DispatcherServlet 和 spring 应用的上下文
	@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[]{"/"};
	}

}

然后是RootConfig.class

package org.spitter.config;

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.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"org.spitter"},
	excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {

}

然后是WebConfig.class

package org.spitter.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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"org.spitter.web"})
public class WebConfig extends WebMvcConfigurerAdapter {

//	配置视图解析器
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/page/");
		resolver.setSuffix(".jsp");
		resolver.setExposeContextBeansAsAttributes(true);
		return resolver;
	}
//	配置静态资源的处理
	@Override
	public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
}

最后写一个Controller

package org.spitter.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

	@RequestMapping(value="/myhome",method=RequestMethod.GET)
	public String home(){
		return "home";
	}
}

这样,初始web项目完成,可以启动tomcat加载了

【Spring】web开发 javaConfig方式 图解的更多相关文章

  1. Spring整合web开发

    正常整合Servlet和Spring没有问题的 public class UserServlet extends HttpServlet { public void doGet(HttpServlet ...

  2. Eclipse Che开发Spring Web应用(入门) (二)

    在上篇博客中我们介绍了如何安装Eclipse Che这种浏览器SDK之后,收到了许多开发者的提问,为了方便初学者开发Java web应用,笔者又一步步实践了spring web开发(demo)过程,欢 ...

  3. spring web 测试用例

    spring web 测试有三种方式 1. 自己初始化 MockMvc 2.依赖@springbootTest 它会帮你生产 webTestClient ,只需自己注入即可. 3.启动的时候,不加载整 ...

  4. 2021年Web开发的7大趋势

    技术发展日新月异,所以 Web 开发人员也需要及时了解行业最新的发展趋势. 全球有超过 17.4 亿个网站.在每一个细分领域都有无数企业争夺搜索引擎的排名前列位置.开发人员应该了解和发现更多创新的 W ...

  5. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  6. Spring Boot的web开发&静态资源配置方式

    Web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 1.1. 自动配置的ViewResolve ...

  7. 使用Spring Boot开发Web项目(二)之添加HTTPS支持

    上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...

  8. Spring Boot Web 开发注解篇

    本文提纲 1. spring-boot-starter-web 依赖概述 1.1 spring-boot-starter-web 职责 1.2 spring-boot-starter-web 依赖关系 ...

  9. 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...

随机推荐

  1. java多态加深

    当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法. public class Dtai ...

  2. bash脚本编程---循环

    bash为过程式编程语言 代码执行顺序: 1.顺序执行:逐条执行 2.选择执行:代码有一个分支,条件满足时才会执行                       两个或以上的分支,只会执行其中一个满足条 ...

  3. Serv-u Mysql数据库用户

    Serv-u 关联Mysql数据库用户需要用到ODBC数据源,windows不自带支持MySQL.所以要网上下载自己安装 官网下载地址:http://dev.mysql.com/downloads/c ...

  4. 有关java 8

    http://www.iteye.com/news/27608     Java 8 发布时间敲定,延期半年 http://www.iteye.com/news/24631/   Java 8 的重要 ...

  5. 打造 高性能,轻量级的 webform框架---js直接调后台(第二天)

    问题2: 每次与后台打交道 都需要写一些自己都看不太懂的事件,而且传参数很麻烦,这就是.net 封装的事件,如何解决呢?        首先以为webfrom事件,都需要写 服务器控件来绑定后台的事件 ...

  6. ETL作业调度软件TASKCTL4.1集群部署

    熟悉TASKCTL4.1一段时间后,觉得它的调度逻辑什么的都还不错,但是感觉单机部署不太够用.想实现跨机调度作业,就要会TASKCTL的集群部署.下面就是我在网上找到的相关资料,非原创. 单机部署成功 ...

  7. 设计模式笔记——GoF设计模式汇总

    目录 · 总述 · 记忆 · 效果 · 面向对象设计原则 · 创建型模式 · 单例模式(Singleton) · 效果 · 分类 · 代码(饿汉式) · 代码(懒汉式) · 代码(双重检测锁式) ·  ...

  8. CPUImageRGBFilter 实现

    参考自: https://github.com/BradLarson/GPUImage GPUImageRGBFilter: Adjusts the individual RGB channels o ...

  9. Git时光机穿梭之版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  10. (3)markdown软件的使用

    运行Mou.zip解压出来一个软件,它让托到应用程序中,然后打开 另一种软件为gitBook 安装好软件后,使用快捷键F4可以调出所有的应用程序 使用md(markdown简称)有个缺点就是,当内容比 ...