共计 6754 个字符,预计需要花费 17 分钟才能阅读完成。
1. 回顾 MVC
1.1、什么是 MVC
- MVC 是模型 (Model)、视图(View)、控制器(Controller) 的简写,是一种软件设计规范。
- 是将业务逻辑、数据、显示拆散的办法来组织代码。
- MVC 次要作用是升高了视图与业务逻辑间的双向偶合。
- MVC 不是一种设计模式,MVC 是一种架构模式。当然不同的 MVC 存在差别。
Model(模型):数据模型,提供要展现的数据,因而蕴含数据和行为,能够认为是畛域模型或 JavaBean 组件(蕴含数据和行为),不过当初个别都拆散开来:Value Object(数据 Dao)和 服务层(行为 Service)。也就是模型提供了模型数据查问和模型数据的状态更新等性能,包含数据和业务。
View(视图):负责进行模型的展现,个别就是咱们见到的用户界面,客户想看到的货色。
Controller(控制器):接管用户申请,委托给模型进行解决(状态扭转),处理完毕后把返回的模型数据返回给视图,由视图负责展现。也就是说控制器做了个调度员的工作
1.2、Servelt
编写一个 Servlet 类,用来解决用户的申请:
package com.servlet; | |
// 实现 Servlet 接口 | |
public class HelloServlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
// 获得参数 | |
String method = req.getParameter("method"); | |
if (method.equals("add")){req.getSession().setAttribute("msg","执行了 add 办法"); | |
} | |
if (method.equals("delete")){req.getSession().setAttribute("msg","执行了 delete 办法"); | |
} | |
// 业务逻辑 | |
// 视图跳转 | |
req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp); | |
} | |
@Override | |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req,resp); | |
} | |
} |
编写 Hello.jsp,在 WEB-INF 目录下新建一个 jsp 的文件夹,新建 hello.jsp:
%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
<html> | |
<head> | |
<title>Index</title> | |
</head> | |
<body> | |
${msg} | |
</body> | |
</html> |
在 web.xml 中注册 Servlet:
<?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_4_0.xsd" | |
version="4.0"> | |
<servlet> | |
<servlet-name>HelloServlet</servlet-name> | |
<servlet-class>com.servlet.HelloServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>HelloServlet</servlet-name> | |
<url-pattern>/user</url-pattern> | |
</servlet-mapping> | |
</web-app> |
2. Spring MVC
Spring MVC 是 Spring Framework 的一部分,是基于 Java 实现 MVC 的轻量级 Web 框架。
查看官网文档:https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-frame…
Spring MVC 的特点:
- 轻量级,简略易学
- 高效 , 基于申请响应的 MVC 框架
- 与 Spring 兼容性好,无缝联合
- 约定优于配置
- 功能强大:RESTful、数据验证、格式化、本地化、主题等
- 简洁灵便
Spring 的 web 框架围绕 DispatcherServlet [调度 Servlet] 设计。
在 Idea 中创立我的项目的根本步骤:
- 创立 Maven 我的项目
- 点击右键“Add Framework Support”
- 抉择“Web Application”将其变成一个 Web 我的项目
- 运行处“Edit Configureation”
- 几点“+”增加 local Tomcat 服务器
- 在“Deployment”处抉择须要部署的我的项目
- 点击 Project Structure 抉择 Artifacts
- 抉择所在我的项目:在 WEB-INF 上面建设和 class 的同级目录 lib
- 点击“+”导入相干依赖
在我的项目首先导入配置:
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-webmvc</artifactId> | |
<version>5.1.9.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>servlet-api</artifactId> | |
<version>2.5</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet.jsp</groupId> | |
<artifactId>jsp-api</artifactId> | |
<version>2.2</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>jstl</artifactId> | |
<version>1.2</version> | |
</dependency> | |
</dependencies> |
2.1 核心控制器
Spring 的 web 框架围绕 DispatcherServlet 设计。DispatcherServlet 的作用是将申请散发到不同的处理器
2.3 Hello SpringMVC
2.3.1 配置开发
2.3.2 注解开发
- 在 maven 中可能存在在资源过滤的问题,首先在 pom.xml 导入:
<build> | |
<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> |
- 配置 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_4_0.xsd" | |
version="4.0"> | |
<!--1. 注册 servlet--> | |
<servlet> | |
<servlet-name>SpringMVC</servlet-name> | |
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |
<!-- 通过初始化参数指定 SpringMVC 配置文件的地位,进行关联 --> | |
<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> | |
<!-- 所有申请都会被 springmvc 拦挡 --> | |
<servlet-mapping> | |
<servlet-name>SpringMVC</servlet-name> | |
<url-pattern>/</url-pattern> | |
</servlet-mapping> | |
</web-app> |
留神:
- / 和 / 的区别:< url-pattern > / </ url-pattern > 不会匹配到.jsp,只针对咱们编写的申请;即:.jsp 不会进入 spring 的 DispatcherServlet 类。< url-pattern > / </ url-pattern > 会匹配 *.jsp,会呈现返回 jsp 视图 时再次进入 spring 的 DispatcherServlet 类,导致找不到对应的 controller 所以报 404 错。
- 增加 Spring MVC 配置文件
在 resource 目录下增加 springmvc-servlet.xml 配置文件(在 web.xml 注册了),配置的模式与 Spring 容器配置根本相似,为了反对基于注解的 IOC,设置了主动扫描包的性能,具体配置信息如下:
<?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 | |
https://www.springframework.org/schema/context/spring-context.xsd | |
http://www.springframework.org/schema/mvc | |
https://www.springframework.org/schema/mvc/spring-mvc.xsd"> | |
<!-- 主动扫描包,让指定包下的注解失效, 由 IOC 容器对立治理 --> | |
<context:component-scan base-package="com.controller"/> | |
<!-- 让 Spring MVC 不解决动态资源 --> | |
<mvc:default-servlet-handler /> | |
<!-- | |
反对 mvc 注解驱动 | |
在 spring 中个别采纳 @RequestMapping 注解来实现映射关系 | |
要想使 @RequestMapping 注解失效 | |
必须向上下文中注册 DefaultAnnotationHandlerMapping | |
和一个 AnnotationMethodHandlerAdapter 实例 | |
这两个实例别离在类级别和办法级别解决。而 annotation-driven 配置帮忙咱们主动实现上述两个实例的注入。--> | |
<mvc:annotation-driven /> | |
<!-- 视图解析器 --> | |
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" | |
id="internalResourceViewResolver"> | |
<!-- 前缀 --> | |
<property name="prefix" value="/WEB-INF/jsp/" /> | |
<!-- 后缀 --> | |
<property name="suffix" value=".jsp" /> | |
</bean> | |
</beans> |
在视图解析器中咱们把所有的视图都寄存在 /WEB-INF/ 目录下,这样能够保障视图平安,因为这个目录下的文件,客户端不能间接拜访。
- 创立 Controller
编写一个 Java 管制类:com.controller.HelloController , 留神编码标准:
package com.controller; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
@Controller | |
@RequestMapping("/HelloController") | |
public class HelloController { | |
// 实在拜访地址 : 我的项目名 /HelloController/hello | |
@RequestMapping("/hello") | |
public String sayHello(Model model){ | |
// 向模型中增加属性 msg 与值,能够在 JSP 页面中取出并渲染 | |
model.addAttribute("msg","hello,SpringMVC"); | |
//web-inf/jsp/hello.jsp | |
return "hello"; | |
} | |
} |
- @Controller 是为了让 Spring IOC 容器初始化时主动扫描到;
- @RequestMapping 是为了映射申请门路,这里因为类与办法上都有映射所以拜访时应该是 /HelloController/hello;
- 办法中申明 Model 类型的参数是为了把 Action 中的数据带到视图中;
- 办法返回的后果是视图的名称 hello,加上配置文件中的前后缀变成 WEB-INF/jsp/hello.jsp。
- 创立视图层
在 WEB-INF/ jsp 目录中创立 hello.jsp,视图能够间接取出并展现从 Controller 带回的信息;
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
<html> | |
<head> | |
<title>SpringMVC</title> | |
</head> | |
<body> | |
${msg} | |
</body> | |
</html> |
-
总结
根本步骤:- 新建一个 web 我的项目
- 导入相干 jar 包
- 编写 web.xml , 注册 DispatcherServlet
- 编写 springmvc 配置文件
- 接下来就是去创立对应的管制类 , controller
- 最初欠缺前端视图和 controller 之间的对应
- 测试运行调试.