SpringMVC
SpringMVC的发展历程
1.Model1
- Model1,有两个层面,一个是视图,一个是业务。其实主要就是jsp层在处理,但是,这样会造成jsp层代码显得臃肿,你想想,jsp层既有前端html的代码,又有后天java的代码,使人看起来不太清晰明了,这样,Model2的时代就到来了。
2.Model2
- Model2的时代,增加了控制层,也就是servlet层。这样,使得代码整体更加解耦,servlet层负责处理用户的需求以及调用业务层,这样Model2就消除了Model1的缺点。代码框架更加完善和清晰。
3.SpringMVC
- 用户将URL发送给服务器,发送请求;
- DispatcherServlet处理请求,将映射文件请求到处理器HandlerMapping中;
- Handler会将请求映射为HandlerExecutionChain的handler对象;
- 将handler作为参数传递给HandlerAdapter,调用handler方法生成ModelAndView实例;
- ViewResolver将ModelAndView解析为view;
- DispatcherServlet获取到view;
- DispatcherServlet将视图返回给用户。
什么是SpringMVC?
- SpringMVC:M:Model(模块),V:View(视图),C:controller(控制器)
- Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。
- Spring MVC 分离了控制器、模型对象、过滤器以及处理程序对象的角色,这种分离让它们更容易进行定制。
- 优点
- 结构松散,几乎可以在 Spring MVC 中使用各类视图;
- 松耦合,各个模块分离;
- 与 Spring 无缝集成;
SpringMVC的简单使用
- 首先,导包,编辑pom.xml;
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>com.baidu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringMVC-02</artifactId>
<packaging>war</packaging>
<name>SpringMVC-02 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC 及 Spring系列包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.24.RELEASE</version>
</dependency>
<!--Servlet核心-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC-02</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<!--解决资源导出问题-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
- 编写SpringMVC-servlet配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描指定包下的注解-->
<context:component-scan base-package="com.baidu.controller"/>
<!–静态资源过滤–>
<mvc:default-servlet-handler/>
<!–加载支持MVC注解驱动–>
<mvc:annotation-driven/>
<!--加载视图解析器-->
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--将文件放在WEB-INF下面,更安全-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--<bean id="/hyf" class="com.baidu.controller.Controller3"/>-->
</beans>
- 编写web.xml文件
<?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_0.xsd" id="WebApp_ID" version="3.0">
<!--<display-name>Archetype Created Web Application</display-name>-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--管理spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-servlet.xml</param-value>
</init-param>
<!--启动时加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 编写跳转的页面;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springMVC</title>
</head>
<body>
${msg}
</body>
</html>
- 编写测试类
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*@Controller用于扫描控制器*/
@Controller
public class Controller2 {
/*请求路径*/
@RequestMapping("/h1")
public String test(Model model){
model.addAttribute("msg","hello2");
return "hello";//进入了WEB-INF/jsp/hello.jsp页面
}
@RequestMapping("/h2")
public String test2(Model model){
model.addAttribute("msg","hello3");
return "hello";
}
}
- 在Idea里面启动 tomcat,在输入栏中输入:h1,或者 h2
就看到运行结果啦。。