springmvc笔记

一、MVC的定义

Model:数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数据和行为),不过现在一般都分离开来:Value Object(数据Dao) 和 服务层(行为Service)。也就是模型提供了模型数据查询和模型数据的状态更新等功能,包括数据和业务。
View:负责进行模型的展示,一般就是我们见到的用户界面,客户想看到的东西。
Controller(调度员): 接收用户请求,委托给模型进行处理(状态改变),处理完毕后把返回的模型数据返回给视图,由视图负责展示。也就是说控制器做了个调度员的工作。
最常用的MVC:(Model)Bean +(view) Jsp +(Controller) Servlet

二、servlet创建流程:

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.0modelVersion>

    <groupId>com.sslgroupId>
    <artifactId>SpringMVCartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.12version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.10.0version>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.62version>
        dependency>

    dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>

            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>

project>

第一步:创建maven,父工程:pom.xml,如上例代码所示

第二步:创建子工程,idea右键Add Framwork Support添加web支持

第三步:实现HelloServlet继承HttpServlet接口,并创建/WEB-INF/jsp/test.jsp

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获得参数
        //2 调用业务层
        //3 视图转发或者重定向
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

第四步:web.xml中注册HelloServlet,测试跳转:http://localhost:8080/springmvc_01_servlet//helloServle

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>HelloServletservlet-name>
        <servlet-class>com.ssl.web.HelloServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>HelloServletservlet-name>
        <url-pattern>/helloServleturl-pattern>
    servlet-mapping>
web-app>

三:springmvc工作原理:

springmvc笔记

SpringMVC底层工作原理:

  1. DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心。用户发出请求,DispatcherServlet接收请求并拦截请求。
  2. 假设url为 : http://localhost:8080/SpringMVC/hello
  3. 服务器域名:http://localhost:8080
  4. web站点:/SpringMVC
  5. hello表示控制器:/hello
  6. 通过分析,如上url表示为:请求位于服务器localhost:8080上的SpringMVC站点的hello控制器。
  7. HandlerMapping为处理器映射。DispatcherServlet调用HandlerMapping,HandlerMapping根据请求url查找Handler。
  8. HandlerExecution表示具体的Handler,其主要作用是根据url查找控制器,如上url被查找控制器为:hello。
  9. HandlerExecution将解析后的信息传递给DispatcherServlet,如解析控制器映射等。
  10. HandlerAdapter表示处理器适配器,其按照特定的规则去执行Handler。
  11. Handler让具体的Controller执行。
  12. Controller将具体的执行信息返回给HandlerAdapter,如ModelAndView。
  13. HandlerAdapter将视图逻辑名或模型传递给DispatcherServlet。
  14. DispatcherServlet调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名。
  15. 视图解析器将解析的逻辑视图名传给DispatcherServlet。
  16. DispatcherServlet根据视图解析器解析的视图结果,调用具体的视图。
  17. 最终视图呈现给用户。

如不使用注解开发则需要进行如下操作:

第一步:配置web.xml

完成DispatcherServlet,关联resource配置文件

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_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>SpringMvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springMvc_servlet.xmlparam-value>
        init-param>

        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>SpringMvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

第二步:配置springMvc_servlet.xml

获得视图解析器、映射器、适配器,绑定跳转url

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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    bean>

    <bean id="/hello" class="com.ssl.controller.HelloController"/>
beans>

第三步:/WEB-INF/jsp/hello.jsp

@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hellotitle>
head>
<body>
--接受传递的参数--%>
${msg}
body>
html>

第四步:HelloController实现Controller

访问:http://localhost:8080/springmvc_02_hellomvc/hello

public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //1 创建modelAndView
        ModelAndView mv = new ModelAndView();
        //2 调用业务层,这里没有,就不写
        //3 封装对象,放在mv中添加
        mv.addObject("msg", "Hello SpringMvc");
        //4 封装要跳转的视图,WEB-INF/jsp/hello.jsp
        mv.setViewName("hello");
        return mv;
    }
}

springmvc笔记

如使用注解开发:

第一步:配置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">

    <servlet>
        <servlet-name>springMvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springMvc_servlet.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>springMvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

第二步:springMvc_servlet.xml

注解省略了映射器、适配器,专注于写视图解析器;跳转的Controller也不用配置进Spring

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
        https://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">

    <context:component-scan base-package="com.ssl.controller"/>

    <mvc:default-servlet-handler/>

    <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/jsp/hello.jsp

@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hellotitle>
head>
<body>
${msg}
body>
html>

第四步:HelloController

简化了实现的接口,使用@注解配置映射器

访问:http://localhost:8080/springmvc_03_annotation/hello

@Controller
public class HelloController {
    /**
     * @param model 模型
     * @return 被视图解析器处理:访问"/WEB-INF/jsp/hello.jsp资源
     * 访问的url:RequestMapping("/hello")
     */
    @RequestMapping("/hello")
    public String hello(Model model) {
        //封装数据
        model.addAttribute("msg", "Hello SpringMvc_annotation");
        //被视图解析器处理:访问"/WEB-INF/jsp/hello.jsp资源
        return "hello";
    }

Original: https://www.cnblogs.com/whiplash/p/16336676.html
Author: 小熊冰淇淋
Title: springmvc笔记

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/580599/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球