实现无xml化搭建工程
创建新工程 , new - project
这里添加 archetypeCatalog : internal 是为了加快新建速度.
finish 后项目结构如下:
如下:
maven Pom 文件 引入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--导入jackson坐标-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
springservlet中的父子容器
createServletApplicationContext() 和 createRootApplicationContext() 分别表示创建容器, 前者是servlet容器, 一个是 spring 的根容器
这两个容器为父子关系 ; 根容器是不能访问子容器servlet容器的bean的, 而servlet容器是你可以访问根容器里边的bean的. 这就实现了工程中的controller类可以调用service类中的方法, 而 service 不可以调用controller类中的方法.
getServletMapping() 是用与限定dispatcherServlet的访问规则. (就是2.5规范的 servlet-mapping中的url-pattern 中的内容)
详细源码如下:
SpringConfiguration
package com.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
* Spring的配置类,它替代了applicationContext.xml
*/
@Configuration
@ComponentScan(value="com",
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class))
public class SpringConfiguration {
}
SpringMvcConfiguration
package com.config;
/**
* springmvc的配置类,用于替代springmvc.xml配置文件
*/
@Configuration
@ComponentScan("com.web")
@EnableWebMvc
public class SpringMvcConfiguration implements WebMvcConfigurer {
/**
* 添加资源处理规则
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**","/images/**","/css/**")
.addResourceLocations("/js/","/images/","/css/")
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
}
/**
* 创建视图解析器并存入ioc容器
* @return
*/
@Bean
public ViewResolver createViewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
Config
package com.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.EnumSet;
/**
* 初始化spring和springmvc ioc容器的配置类
*/
public class Config extends AbstractDispatcherServletInitializer {
/**
* 注册字符集过滤器
* @param servletContext
* @throws ServletException
*/
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//触发父类的onStartup
super.onStartup(servletContext);
//1.创建字符集过滤器对象
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
//2.设置使用的字符集
characterEncodingFilter.setEncoding("UTF-8");
//3.添加到容器(它不是ioc容器,而是ServletContainer)
FilterRegistration.Dynamic registration = servletContext.addFilter("characterEncodingFilter",characterEncodingFilter);
//4.添加映射
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),
false,"/*");
//解决跨域的过滤器
// FilterRegistration.Dynamic registration1 = servletContext.addFilter("crossOriginFilter",new CrossOriginFilter());
// registration1.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),
// false,"/*");
}
/**
* 用于创建springmvc的Ioc容器
* @return
*/
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext acw = new AnnotationConfigWebApplicationContext();
acw.register(SpringMvcConfiguration.class);
return acw;
}
/**
* 用于指定DispatcherServlet的请求映射
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
/**
* 用于创建spring的ioc容器
* @return
*/
@Override
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext acw = new AnnotationConfigWebApplicationContext();
acw.register(SpringConfiguration.class);
return acw;
}
}
package com.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 入门案例的控制
*/
@Controller
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
System.out.println("控制器方法执行>>>>");
return "success";
}
}
<%@page contentType="text/html; UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<body>
<h2>welcome!</h2>
<img src="images/success.jpg">
<br/>
<a href="${pageContext.request.contextPath}/hello">SpringMVC基于servlet3.0-注解开代码示例</a>
</body>
</html>
注意这里的 "Application context " 的配置 , idea以前的版本和后来版本 (大概2020之后的吧) 是不一样的
老版本如下:
新版本如下: (右边滚动条 滚到底!)
访问测试:
点击下方超链接跳转/hello
// Servlet3.0规范提供的标准接口
public interface ServletContainerInitializer {
// 启动容器是做一些初始化操作,例如注册Servlet,Filter,Listener等等。
public void onStartup(Set<Class<?>> c, ServletContext ctx)
throws ServletException;
}
// 用于指定要加载到ServletContainerInitializer接口实现类中的字节码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface HandlesTypes {
/**
* 指定要加载到ServletContainerInitializer实现类的onStartUp方法中类的字节码。
* 字节码可以是接口,抽象类或者普通类。
*/
Class[] value(); }
过程如下:
springMVC 主题的框架可分为: " 一个中心 , 三个基本点" .
因篇幅问题不能全部显示,请点此查看更多更全内容