文章目录
一.环境搭建
1.springmvc框架部署
new->project->maven
添加
<packaging>war<packaging>,刷新
main目录下新建webapp目录
webapp下新建WEB-INF,WEB-INF下新建web.xml
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
File->Project Structure->Modules,点击Dependencies,如下图点击+,再点击Library,找到Tomcat,点击添加
点击add configuration,再点击+
找到Tomcat Server,Local,再Application server项找到Tomcat安装目录
点击Fix
点击springmvc-demo:war exploded
找到Application context,修改如下
2.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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qfedu</groupId>
<artifactId>springmvc-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.2.13.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
3.创建spring mvc配置文件
在resources目录下创建名为spring-servlet.xml的文件
添加MVC命名空间
spring-servlet.xml
<?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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.qfedu"/>
<!--声明mvc使用注解驱动-->
<mvc:annotation-driven/>
</beans>
4.在web.xml中配置springmvc的前端控制器
springmvc提供了一个名为DispatcherServlet的类(springmvc中央处理器,
也就是前端控制器),用于拦截用户请求交由springmvc处理
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-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>
</web-app>
5.springmvc框架使用
在main目录下新建com.qfedu.controllers包
在一个控制器类中可以多个方法处理不同的请求
在每个方法上添加@RequestMapping(“url”)用于声明当前方法请求的url
package com.qfedu.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/add")
public void add(){
System.out.println("add");
}
@RequestMapping("/list")
public void list(){
System.out.println("list");
}
}
访问
http://localhost:8080/springmvc_demo1/book/list
二.url-pattern中/*和/的区别
1./
(一)拦截HTTP请求,不包括.jsp的请求,不放行静态资源请求
(二)静态资源放行配置
在spring-servlet.xml中添加
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/imgs/**" location="/imgs/"/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>添加图书</h3>
<form action="" method="post">
<p>图书名称:<input type="text"/></p>
<p>图书作者:<input type="text"/></p>
<p>图书价格:<input type="text"/></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
访问http://localhost:8080/springmvc_demo1/book-app.jsp
访问http://localhost:8080/springmvc_demo1/book/list
访问http://localhost:8080/springmvc_demo1/bookapp.html
webapp下新建css目录
book-app.css
body{
background-color: blue;
}
访问http://localhost:8080/springmvc_demo1/book-app.jsp
在spring-servlet.xml中添加以下代码
<mvc:resources mapping="/css/**" location="/css/"/>
再次访问http://localhost:8080/springmvc_demo1/book-app.jsp
<mvc:resources mapping="/html/**" location="/html/"/>
bookapp.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>ubiiubueriugveriygvieruhvueirhviubeuivubeuvbgeyivbhbvuh</h1>
<h3>添加图书</h3>
<form action="" method="post">
<p>图书名称:<input type="text"/></p>
<p>图书作者:<input type="text"/></p>
<p>图书价格:<input type="text"/></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>