SpringMvc框架
springmvc框架是对servlet的封装
mvc:struts2,springmvc
1.springmvc-helloworld
1.1 maven项目(javaee)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>springmvc-hello</display-name>
</web-app>
1.2 引入springmvc框架
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dyit.spring</groupId>
<artifactId>springmvc-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- 定义依赖的版本号-->
<project.spring.version>5.3.16</project.spring.version>
<project.log.version>1.2.17</project.log.version>
<project.lombok.version>1.18.22</project.lombok.version>
<project.json.version>2.13.1</project.json.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${project.spring.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${project.log.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${project.lombok.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${project.json.version}</version>
</dependency>
</dependencies>
</project>
1.3 完成controller层的代码
/**
* 控制层
* @author 86184
*
*/
@Controller
public class publisherController {
@GetMapping("/findAll")
@ResponseBody//转换成json对象
public HttpResp findAll() {
List<Publisher> list = new ArrayList<Publisher>();
list.add(new Publisher(1, "工人出版社", "武汉"));
list.add(new Publisher(2, "神话出版社", "广州"));
list.add(new Publisher(3, "儿童出版社", "长沙"));
HttpResp dto = new HttpResp(2001, "出版社添加成功", list, new Date());
return dto;
}
}
1.4 配置springmvc框架
1.4.1 配置IoC容器(mvc容器)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.dyit.controller"/><!-- 自动扫描注解类,并创建对象-->
<mvc:resources location="static/" mapping="static/**"/> <!-- 不处理静态资源(前端页面) -->
<mvc:annotation-driven/> <!-- 注解驱动,配合@responsebody-->
</beans>
1.4.2 配置servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>springmvc-hello</display-name>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc-*.xml</param-value> <!-- 配置servlet -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern> <!-- 拦截所有请求 -->
</servlet-mapping>
</web-app>