SpringBoot推荐开发者使用Java配置来搭建框架,SpringBoot中大量的自动化配置都是通过Java代码配置实现的,而不是XML配置,同理,我们自己也可以使用纯Java来搭建一个SSM环境,即在项目中不存在任何XML配置,包括web.xml
环境要求:
Tomact版本必须在7以上
1.在IDEA中创建一个普通的maven项目
在pom.xml加入项目中需要用到的依赖
4.0.0 com.xiao.ssm SSM-demo1.0-SNAPSHOT war 8 8 2.2 8888 UTF-8 org.springframework spring-webmvc5.2.12.RELEASE javax.servlet javax.servlet-api4.0.1 provided javax.servlet.jsp jsp-api2.2 org.apache.tomcat.maven tomcat7-maven-plugin${tomcat.version} ${webserver.port} /${project.artifactId} ${project.build.sourceEncoding} central https://maven.aliyun.com/nexus/content/repositories/central central https://maven.aliyun.com/nexus/content/repositories/central
2.添加Spring配置
创建SpringConfig.java文件
package com.xiao.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(basePackages = "com.xiao",useDefaultFilters = true,
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class SpringConfig {
}
3.添加SpringMVC配置
创建SpringMVCConfig.java文件
package com.xiao.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
//@ComponentScan(basePackages = "com.xiao",useDefaultFilters = true,
// excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
@ComponentScan(basePackages = "com.xiao")
public class SpringMVCConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js
@Override
protected void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/jsp/",".jsp");
}
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/hello3").setViewName("hello");
}
@Override
protected void configureMessageConverters(List> converters) {
FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter();
converter.setDefaultCharset(Charset.forName("UTF-8"));
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
converter.setFastJsonConfig(fastJsonConfig);
converters.add(converter);
}
}
4.配置web.xml
创建WebInit.java文件
package com.xiao.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//首先来加载SpringMVC的配置文件
AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
ctx.register(SpringMVCConfig.class);
//添加DispatcherServlet
ServletRegistration.Dynamic springmvc=servletContext.addServlet("springmvc",new DispatcherServlet(ctx));
//给DispatcherServlet添加路径映射
springmvc.addMapping("/");
//给DispatcherServlet添加启动时机
springmvc.setLoadonStartup(1);
}
}
WebInit的作用类似于web.xml,这个类需要实现WebApplicationInitializer接口,并实现其方法,当项目启动时,onStartup方法会被自动执行,我们可以在这里进行项目初始化操作,如:加载SpringMVC容器,添加过滤器,添加Listener,添加Servlet等
注:
由于在onStartup里面只加载了springmvc配置,没有加载spring容器,如果要加载Spring容器
方案一:
修改springmvc配置,在配置的包扫描中也去扫描@Configuration注解
推荐 方案二:
去掉springConfig文件,讲所有的配置都放到springmvc里面
5.测试
5.1创建HelloController类
package com.xiao.control;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
package com.xiao.control;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
运行结果:
5.2创建HelloController2类
package com.xiao.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController2 {
@GetMapping("hello2")
public String hello2(){
return "hello";
}
}
运行结果:
5.3路径映射
6.JSON配置
SpringMVC可以接收json参数,也可以返回json参数,这一切依赖于HttpMessageConverter
HttpMessageConverter可以将一个json字符串转为对象,也可以将一个对象转为json字符串,实际上它的底层还是依赖具体的json库
SpringMVC中默认提供了Jackson和gson的HttpMessageConverter,分别是:
MappingJackson2HttpMessageConverter、GsonHttpMessageConverter
7.总结
1.本项目需要在idea中配置外部的tomact才可运行
2.自己也尝试在pom.xml中配置tomact插件,最后发现不行
3.使用mave插件打包不行,因为他会找web.xml,所以找不到就会打包失败



