创建一个Maven项目。无需多说,不知道怎么创建请移步
一、XML版SSM(SS整合没有M)
然后打成war包:
<packaging>war</packaging>
引入依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!--配置WebConfig类时必须得有此依赖,不然会报错没有此包-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
创建Spring、SpringMVC、WebConfig类作为配置文件,结构如下:
spring配置:
package com.juge.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
* @author juge
* @version 1.0
* @date 2020/5/28 19:45
*/
@Configuration
// 注解方式进行包扫描:意思是除去Controller,扫描其他的包
@ComponentScan(basePackages = "com.juge",useDefaultFilters = true,excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = Controller.class)})
public class SpringConfig {
}
springmvc配置:
package com.juge.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
* @author juge
* @version 1.0
* @date 2020/5/28 19:45
*/
@Configuration
// 注解方式进行包扫描:意思是只扫描Controller,以及Configration注解的包(即扫描spring的配置文件)
@ComponentScan(basePackages = "com.juge",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION
,classes = Controller.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Configuration.class)})
public class SpringMvcConfig {
}
webConfig(web.xml)配置:
package com.juge.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
/**
* @author juge
* @version 1.0
* @date 2020/5/28 19:50
*/
//实现WebApplicationInitializer 等同于web.xml配置
public class WebConfig implements WebApplicationInitializer {
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
//加载那两个配置文件
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.setServletContext(servletContext);
//注册SpringMVC的配置
ctx.register(SpringMvcConfig.class);
ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(ctx));
springmvc.addMapping("/");
springmvc.setLoadOnStartup(1);
}
}
到此spring+sringmvc的一个基本环境搭建完成,然后部署(之前博客中已经讲过),最后写测试,结构如下:
controller层:
package com.juge.controller;
import com.juge.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author juge
* @version 1.0
* @date 2020/5/28 19:58
*/
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.hello();
}
}
service层:
package com.juge.service;
import org.springframework.stereotype.Service;
/**
* @author juge
* @version 1.0
* @date 2020/5/28 20:00
*/
@Service
public class HelloService {
public String hello(){
return "hello javassm";
}
}
启动项目,访问:localhost:8080/hello
完成。
整合Mybatis以后再讲。
下面咱们来解决几个问题:
1、静态资源过滤问题
首先我们在resoures下创建static文件夹,然后创建一个html页面:
现在这个hello.html就是我们的静态资源,如果我们想要访问此静态资源,就要配置资源路径映射,在SpringMvcConfig.java类中做如下配置:
//静态资源过滤
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//静态资源访问
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
然后我们把项目跑起来,访问此页面:
2、拦截器问题
有的时候我们需要对访问资源的用户进行拦截,比如某个页面只有登录之后才能访问资源,咱们在这只配置值一下拦截器,拦截所有资源,但是并不禁止资源的访问。
创建拦截器类:
package com.juge.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author juge
* @version 1.0
* @date 2020/5/28 20:32
*/
//拦截器
public class MyInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
然后在SpringMvcConfig中实例化此拦截器,以及对拦截器进行配置:
由于我在拦截器实现方法中在控制台打印方法名,访问任意资源,控制台:
3、使用fastjson的配置
如果使用阿里巴巴提供的数据格式,需要在SpringMvcConfig中进行配置,即实现:configureMessageConverters方法。
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converters.add(converter);
}
@GetMapping(value = "/data",produces = "text/html;charset=utf-8")
public List<String> getData(){
ArrayList<String> list = new ArrayList<String>();
for (int i =0;i<10;i++){
list.add("www.juge.com>>>"+i);
}
return list;
}