上次分享了关联关系。本次来分享一下Spring MVC的入门。
Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。
1、DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC
2、HandlerMapping的配置,从而将请求映射到处理器
3、HandlerAdapter的配置,从而支持多种类型的处理器
4、处理器(页面控制器)的配置,从而刊行功能处理
5、ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术
1.1、扫描base-package
定义base-package
开启注解扫描
开启动态代理
1.2、配置视图解析器
1.3、静态资源映射
Spring与web容器集成
前面分享过了,这里就不分享了。
SpringMVC 与web容器集成
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
修改web.xml
- <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">
- <display-name>Archetype Created Web Applicationdisplay-name>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:applicationContext.xmlparam-value>
- context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
- <servlet>
- <servlet-name>SpringMVCservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
-
- <async-supported>trueasync-supported>
- servlet>
- <servlet-mapping>
- <servlet-name>SpringMVCservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
-
-
1.首先用户发送请求–>DispatherServlet
2 DispatcherServlet–>HandlerMapping
3 DispatcherServlet–>HandlerAdapter
4 HandlerAdapter–>处理器功能处理方法的调用
5 ModelAndView的逻辑视图名–>ViewRecolver
6 View–>渲染
7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户
hellocontroller
- package com.zhw.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- /**
- * @author louis
- * @create 2022-08-16 21:51
- */
- @Controller
- public class HelloController {
-
- @RequestMapping("/hello1")
- public String hello(){
- return "hello";
- }
- }
hello.jsp
- <%--
- Created by IntelliJ IDEA.
- User: zhw
- Date: 2022/8/16
- Time: 22:38
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>你好MVCtitle>
- head>
- <body>
- 你好MVC
- body>
- html>
bookcontroller
- package com.zhw.controller;
-
- import com.zhw.biz.BookBiz;
- import com.zhw.model.Book;
- import com.zhw.util.PageBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author louis
- * @create 2022-08-16 21:53
- */
- @Controller
- @RequestMapping("/book")
- public class BookController {
- @Autowired
- private BookBiz bookBiz;
-
- @RequestMapping("/add")
- public String add(Book book){
- this.bookBiz.insertSelective(book);
- return "redirect:/book/list";
- }
-
- @RequestMapping("/del/{bid}")
- public String del(@PathVariable(value = "bid") Integer bid){
- this.bookBiz.deleteByPrimaryKey(bid);
- return "redirect:/book/list";
- }
-
- @RequestMapping("/list")
- public String list(HttpServletRequest request, Book book){
- PageBean pageBean = new PageBean();
- pageBean.setRequest(request);
- Map map = new HashMap();
- map.put("bname","%圣墟%");
- List
- request.setAttribute("bookList",list);
- request.setAttribute("pageBean",pageBean);
- return "bookList";
- }
-
- @RequestMapping("/edit")
- public String edit(Book book){
- this.bookBiz.updateByPrimaryKeySelective(book);
- return "redirect:/book/list";
- }
-
- }
index.jsp
- <%--
- Created by IntelliJ IDEA.
- User: zhw
- Date: 2022/8/17
- Time: 9:25
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <a href="${pageContext.request.contextPath}/book/list">查询所有a>
- <a href="${pageContext.request.contextPath}/book/add">增加a>
- <a href="${pageContext.request.contextPath}/book/edit">修改a>
- <a href="${pageContext.request.contextPath}/book/del/22">删除a>
- body>
- html>