1. SpringMVC的概述
1. 是一种基于Java实现的MVC设计模型的请求驱动类型的轻量级WEB框架。
2. Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。
Spring 框架提供 了构建 Web 应用程序的全功能 MVC 模块。
3. 使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的 SpringMVC框架或集成其他MVC开发框架,
如Struts1(现在一般不用),Struts2等。
2.spring入门案例
1. 创建WEB工程,引入开发的jar包
1. 具体的坐标如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.itheima</groupId> 8 <artifactId>day01_springmvc_01start</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>day01_springmvc_01start Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 21 </properties> 22 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context</artifactId> 27 <version>5.0.2.RELEASE</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-web</artifactId> 32 <version>5.0.2.RELEASE</version> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-webmvc</artifactId> 37 <version>5.0.2.RELEASE</version> 38 </dependency> 39 <dependency> 40 <groupId>javax.servlet</groupId> 41 <artifactId>servlet-api</artifactId> 42 <version>2.5</version> 43 </dependency> 44 <dependency> 45 <groupId>javax.servlet</groupId> 46 <artifactId>jsp-api</artifactId> 47 <version>2.0</version> 48 </dependency> 49 </dependencies> 50 51 <build> 52 <finalName>day01_springmvc_01start</finalName> 53 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 54 <plugins> 55 <plugin> 56 <artifactId>maven-clean-plugin</artifactId> 57 <version>3.1.0</version> 58 </plugin> 59 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 60 <plugin> 61 <artifactId>maven-resources-plugin</artifactId> 62 <version>3.0.2</version> 63 </plugin> 64 <plugin> 65 <artifactId>maven-compiler-plugin</artifactId> 66 <version>3.8.0</version> 67 </plugin> 68 <plugin> 69 <artifactId>maven-surefire-plugin</artifactId> 70 <version>2.22.1</version> 71 </plugin> 72 <plugin> 73 <artifactId>maven-war-plugin</artifactId> 74 <version>3.2.2</version> 75 </plugin> 76 <plugin> 77 <artifactId>maven-install-plugin</artifactId> 78 <version>2.5.2</version> 79 </plugin> 80 <plugin> 81 <artifactId>maven-deploy-plugin</artifactId> 82 <version>2.8.2</version> 83 </plugin> 84 </plugins> 85 </pluginManagement> 86 </build> 87 </project>
2. 配置核心的控制器(配置DispatcherServlet)
1. 在web.xml配置文件中核心控制器DispatcherServlet
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 <servlet> 8 <servlet-name>dispatcherServlet</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <init-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:springmvc.xml</param-value> 13 </init-param> 14 <load-on-startup>1</load-on-startup> 15 </servlet> 16 <servlet-mapping> 17 <servlet-name>dispatcherServlet</servlet-name> 18 <url-pattern>/</url-pattern> 19 </servlet-mapping> 20 </web-app>
3. 编写springmvc.xml的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd"> 13 <!-- 配置spring创建容器时要扫描的包 --> 14 <context:component-scan base-package="com.fanxian"></context:component-scan> 15 <!-- 配置视图解析器 --> 16 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="prefix" value="/WEB-INF/pages/"></property> 18 <property name="suffix" value=".jsp"></property> 19 </bean> 20 <!-- 配置spring开启注解mvc的支持 21 <mvc:annotation-driven></mvc:annotation-driven>--> 22 23 </beans>
4. 编写index.jsp和HelloController控制器类
1 <!--index.jsp--> 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3 <html> 4 <head> 5 <title>Title</title> 6 </head> 7 <body> 8 <h1>hello</h1> 9 <a href="hello">点击这个</a> 10 </body> 11 </html> 12 13 <!--HelloController.java--> 14 @Controller 15 public class HelloController { 16 @RequestMapping(path = "/hello") 17 public String sayHello(){ 18 System.out.println("Hello world"); 19 return "success"; 20 } 21 }