一、springmvc项目搭建
1、手动部署webapp项目
<groupId>org.example</groupId>
<artifactId>spring-transaction</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>主要是这一行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RMnKThkh-1663766219838)(C:\Users\冯瑞涛\AppData\Roaming\Typora\typora-user-images\1662266771946.png)]
2、引入jar包
<!-- mvc核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- 日志slf4j的门面实现包 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha14</version>
</dependency>
<!-- 日志slf4j的门面实现包 -->
servlet使用api包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 日志slf4j的门面实现包 -->
thymelea和spring5的整合包
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<!--导入转化返回值为json字符串的jar包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
3.创建Controller类
@Controller //使用@Controller将该类标记为控制层bean
public class UserController {
@RequestMapping("/save.do") //定义访问标识符,类似Servlet中@WebServlet()的功能
@ResponseBody //确认返回给浏览器的值为该方法的返回的内容
public String save(){
System.out.println("use...save");
return "{'model','springmvc'}";
}
}
4.创建springMVC的配置类
@Configuration
@ComponentScan("com.frt.control")
@EnableWebMvc
public class SpringMvcConfig {
}
5.定义spring容器类,在其中加载spring的配置
/*
此过程最主要的操作就是将自己创建的类继承 AbstractDispatcherServletInitializer类,
然后重写createServletApplicationContext()方法
并在其中创建AnnotationConfigWebApplicationContext对象
调用创建的对象中的register(SpringMvcConfig.class);去加载配置类*/
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
@Override //加载springMVC容器(createServletApplicationContext())
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext=new AnnotationConfigWebApplicationContext();
applicationContext.register(SpringMvcConfig.class);
return applicationContext;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};//在这个位置确定哪些请求被servlet处理
}
@Override //加载spring容器(createRootApplicationContext())
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext=new AnnotationConfigWebApplicationContext();
applicationContext.register(SpringConfig.class);
return applicationContext;
}
@Override //中文字符集过滤和设置方法
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter=new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
或者
public class ServletContainerInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override //加载spring容器(getRootConfigClasses())
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override //加载springMVC容器(getServletConfigClasses())
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}