(1)开发架构:一是 C/S 架构 (客户端/服务器),二是B/S架构(浏览器/服务器)。在JavaEE开发中,几乎全部是基于 B/S架构的开发。在B/S 架构中,系统标准的三层架构。
(2)三层架构
这种模式十分简单。页面显示、控制分发。业务逻辑、数据访问全部通过jsp实现。
这种模式通过两部分去实现,
MVC分为三大块,
模型 | 优点 | 缺点 |
---|---|---|
Model1 | 架构简单,比较适合小型项目开发 | JSP 职责不单一,职责过重,不便于维护 |
Model2 | 职责清晰,较适合于大型项目架构 | 不适合小型项目开发 |
MVC | 分工明确,各司其职,互不干涉。适用于大型项目架构,有利于组件的重构 | 增加了系统开发的复杂度 |
SpringMVC 属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web应用程序的全功能 MVC模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring MVC框架或集成其他MVC开发框架。
SpringMVC 位于三层架构中的表现层。作用:接收请求、响应数据,响应的数据通过驶入、模板展示给用户。
构建页面index.jsp发起请求,在服务器端处理请求,控制台打印处理请求成功,跳转main.jsp成功页面;
pom.xml文件
<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.0modelVersion>
<groupId>com.etimegroupId>
<artifactId>day04011artifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<spring.version>5.2.5.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
dependencies>
project>
<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>dispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:application.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
<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"
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-3.0.xsd">
<context:component-scan base-package="com.etime">context:component-scan>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
beans>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
index
测试
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
main
这里是main页面
FirstController.java
package com.etime.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/firstController")
public class FirstController {
@RequestMapping("test01")
public String test01(){
System.out.println("测试 第一个Controller");
return "main";
}
}
浏览器客户端发起请求,请求到达服务器tomcat,tomcat将请求相关信息参数封装到对象request和response中,再将request和response对象交给service方法处理,在service方法中会根据请求路径将请求交给对应的controller执行器处理。
浏览器发送请求,被DispatcherServlet捕获,DispatcherServlet没有直接处理请求,而是将请求交给HandlerMapping处理器映射器,处理器映射器根据请求路径去controller控制层中匹配对应的执行器,并将匹配结果返回给DispatcherServlet,由DispacherServlet调用HandlerAdapter处理器适配器来执行控制层执行器方法;
执行器方法执行后的返回结果,由DispatcherServlet交给视图解析器ViewResolver来处理,找到对应的结果视图,渲染视图,并将结果响应给浏览器。
用户请求到达前端控制器(mvc 模式中的 controller),dispatcherServlet 是整个流程控制的中心,由它调用其它组件处理用户的请求,dispatcherServlet 的存在降低了组件之间的耦合性。
HandlerMapping 负责根据用户请求找到 Handler 即处理器,SpringMVC提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。由Handler 对具体的用户请求进行处理。
通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。
View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jsp、freemarkerView、pdfView,FreeMark ,thymeleaf 等。我们最常用的视图就是 jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。
application.xml
<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
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.etime">context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:annotation-driven>mvc:annotation-driven>
beans>
用于定义映射路径,建立url 和 控制层方法之间的对应关系
@Target({ElementType.METHOD, ElementType.TYPE}) // 注解在方法和类上有效
@Retention(RetentionPolicy.RUNTIME)// runtime :当前的注解在运行阶段生效
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
}
表单中请求参数都是基于 key=value的。SpringMVC绑定请求参数的过程是通过把表单提交请求参数,作为控制器中方法参数进行绑定的。
请求方式:
get: localhost:8080/xxx?username==lisi&age=21
post: username==lisi&age=21 封装到请求体:
servlet:
String name = request.getParameter(“username”);
String age = request.getParameter(“age”);
String [] xxx = request.getParameterValues(“hobby”);
Map
参数绑定:
就是将获得参数绑定在方法的形式参数上, 取代了request.getParameter(“xxx”)
(1)页面定义请求test02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test02
(2)执行器方法绑定参数
@Controller
@RequestMapping("/firstController")
public class FirstController {
@RequestMapping("test01")
public String test01(){
System.out.println("测试 第一个Controller");
return "main";
}
@RequestMapping(value = "test02",method = {RequestMethod.GET})
public String test02(String username,String password,int age){
System.out.println("username = " + username);
System.out.println("password = " + password);
System.out.println("age = " + age);
return "main";
}
}
(1)User实体
public class User {
private String username;
private String password;
private int age;
public User() {
}
public User(String username, String password, int age) {
this.username = username;
this.password = password;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
(2)页面定义请求 test02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test02
(3) 执行器方法绑定参数
@RequestMapping(value = "test03",method = {RequestMethod.POST})
public String test03(User user){
System.out.println("user = " + user);
return "main";
}
(1)User实体
public class User {
private String username;
private String password;
private int age;
private Car car;
private List carList;
public User() {
}
public User(String username, String password, int age) {
this.username = username;
this.password = password;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public List getCarList() {
return carList;
}
public void setCarList(List carList) {
this.carList = carList;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", car=" + car +
", carList=" + carList +
'}';
}
}
(2)Car实体
public class Car {
private String id;
private String name;
private double price;
public Car() {
}
public Car(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
(3)页面定义请求 test02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test02
(4) 执行器方法绑定参数
@RequestMapping(value = "test04",method = {RequestMethod.POST})
public String test04(User user){
System.out.println("user = " + user);
return "main";
}
(1)页面定义请求
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test03
(2) 执行器方法绑定参数
@RequestMapping(value = "test05",method = {RequestMethod.POST})
public String test05(String[] hobby){
System.out.println("hobby = " + Arrays.toString(hobby));
return "main";
}
(1)引入ServletAPI的依赖jar包,注意:jar包作用范围provided(规定),不参与部署
<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.0modelVersion>
<groupId>com.etimegroupId>
<artifactId>day04011artifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<spring.version>5.2.5.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
<scope>providedscope>
dependency>
dependencies>
project>
(2)页面定义请求 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
index
测试
(3) 执行器方法绑定参数
@RequestMapping(value = "test06",method = {RequestMethod.GET})
public void test06(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求转发 进行页面跳转");
request.getRequestDispatcher("../main.jsp").forward(request,response);
}
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
(1) 页面定义请求
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
index
测试
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
main
这里是main页面
${name}
(2) 执行器方法绑定参数
@RequestMapping(value = "test07",method = {RequestMethod.GET})
public ModelAndView test07(Map map){
ModelAndView modelAndView = new ModelAndView();
map.put("name","tom");
modelAndView.addObject(map);
modelAndView.setViewName("main");
return modelAndView;
}
(1)页面定义请求
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
index
测试
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
main
这里是main页面
${name}
(2) 执行器方法绑定参数
@RequestMapping(value = "test08",method = {RequestMethod.GET})
public ModelAndView test08(ModelMap modelMap){
ModelAndView modelAndView = new ModelAndView();
modelMap.addAttribute("name","mary");
modelAndView.setViewName("main");
return modelAndView;
}
dView.addObject(map);
modelAndView.setViewName(“main”);
return modelAndView;
}
#### 方式二
(1)页面定义请求
index.jsp
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
index
测试
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
main
这里是main页面
${name}
(2) 执行器方法绑定参数
@RequestMapping(value = "test08",method = {RequestMethod.GET})
public ModelAndView test08(ModelMap modelMap){
ModelAndView modelAndView = new ModelAndView();
modelMap.addAttribute("name","mary");
modelAndView.setViewName("main");
return modelAndView;
}