SpringMVC 是表现层的技术
MVC 它是作用与表现层的 一种模式
一种用于设计创建Web应用程序表现层的模式
M model(模型):数据模型,用于封装数据
V view(视图): 页面视图,用于展示数据
C controller(控制器):处理用户交互的调度器,用于根据用户需求处理程序逻辑
spirngMVC是一种基于java实现mvc模型的轻量级web框架
使用 SpringMVC 先在pom写坐标
<!-- servlet3.1规范的坐标 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--springmvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
使用springMVC要导入的坐标有,servlet的坐标与springMVC的坐标
一共需要使用的坐标有
<!-- servlet3.1规范的坐标 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!--spring的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring web的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--springmvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--构建-->
<build>
<!--设置插件-->
<plugins>
<!--具体的插件配置-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
写出的页面在 WebApp目录下,记住 web.xml是用来做页面到后端代码映射路径配置的,要写在 WEB-INF目录里
<?xml version="1.0" encoding="UTF-8" ?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这里出现失败 先把端口号 80 换成 8080 试试,或者是有坐标没写
springMVC-xml主要是做controller层的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!--扫描加载所有的控制类类-->
<context:component-scan base-package="com.itheima"/>
</beans>
springMVC-xml 与 applicationContext.xml内写的东西是一样的,将它写在与main/resources/目录内。
servlet的实体类,主要用来连接后端数据与页面数据的传输的
位置是在,main/java/web包内
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("user servlet is running...");
req.getRequestDispatcher("success.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
Controller层写的类,这里是控制类,用来控制servlet页面连接类的代码
@Controller这个注解是将,本类加入bean受spring的控制
@Controller
public class UserController {
//设置当前类为spring的控制类
//设置当前方法的访问映射地址
@RequestMapping("/save")
//设置当前方法返回值类型为String,用于指定请求完成后跳转的页面
public String save(){
System.out.println("user mvc controller is running...");
//设置具体跳转页面
return "success.jsp";
}}
@RequestMapping("/save")这是设置请求路径的,有了这个web.xml的映射路径才能通过 /save 请求路径名称找到这个方法
这里是具体的路径,可不是虚拟路径
接下来写 web.xml配置文件,
这是用来配置 controller控制类 与 servlet页面数据连接类的映射关系的
<?xml version="1.0" encoding="UTF-8" ?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
DispatcherServlet这个servlet类名是spring中对servlet类的通用名称,以后使用spring对servlet的类配置时连接名称都可以这么写。
DispatcherServlet就是springMVC的核心控制器
这是将spring-MVC.xml这个核心配置文件加载到 web.xml配置文件中,做完这步,spring才能与页面servlet类、controller类连接起来
就是初始化servlet
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
classpath*:spring-mvc.xml 表示初始化的文件名称 *代表
Controller层写的类要设置具体跳转页面
return “success.jsp”;
不设置系统就不会跳转页面了,会报出找不到该页面的错误提示