SpringMVC的简单使用与配置

SpringMVC

SpringMVC的发展历程

1.Model1

在这里插入图片描述

  • Model1,有两个层面,一个是视图,一个是业务。其实主要就是jsp层在处理,但是,这样会造成jsp层代码显得臃肿,你想想,jsp层既有前端html的代码,又有后天java的代码,使人看起来不太清晰明了,这样,Model2的时代就到来了。

2.Model2

在这里插入图片描述

  • Model2的时代,增加了控制层,也就是servlet层。这样,使得代码整体更加解耦,servlet层负责处理用户的需求以及调用业务层,这样Model2就消除了Model1的缺点。代码框架更加完善和清晰。

3.SpringMVC

在这里插入图片描述

  1. 用户将URL发送给服务器,发送请求;
  2. DispatcherServlet处理请求,将映射文件请求到处理器HandlerMapping中;
  3. Handler会将请求映射为HandlerExecutionChain的handler对象;
  4. 将handler作为参数传递给HandlerAdapter,调用handler方法生成ModelAndView实例;
  5. ViewResolver将ModelAndView解析为view;
  6. DispatcherServlet获取到view;
  7. DispatcherServlet将视图返回给用户。
什么是SpringMVC?
  • SpringMVC:M:Model(模块),V:View(视图),C:controller(控制器)
  • Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。
  • Spring MVC 分离了控制器、模型对象、过滤器以及处理程序对象的角色,这种分离让它们更容易进行定制。
  • 优点
    • 结构松散,几乎可以在 Spring MVC 中使用各类视图;
    • 松耦合,各个模块分离;
    • 与 Spring 无缝集成;
SpringMVC的简单使用
  1. 首先,导包,编辑pom.xml;
<?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">
    <parent>
        <artifactId>SpringMVC</artifactId>
        <groupId>com.baidu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringMVC-02</artifactId>
    <packaging>war</packaging>

    <name>SpringMVC-02 Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring MVC 及 Spring系列包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.24.RELEASE</version>
        </dependency>

        <!--Servlet核心-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>SpringMVC-02</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <!--解决资源导出问题-->
        <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>
</project>

  1. 编写SpringMVC-servlet配置文件
<?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.baidu.controller"/>

    &lt;!&ndash;静态资源过滤&ndash;&gt;
    <mvc:default-servlet-handler/>

    &lt;!&ndash;加载支持MVC注解驱动&ndash;&gt;
    <mvc:annotation-driven/>
    <!--加载视图解析器-->
    <bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--将文件放在WEB-INF下面,更安全-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--<bean id="/hyf" class="com.baidu.controller.Controller3"/>-->

</beans>
  1. 编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <!--<display-name>Archetype Created Web Application</display-name>-->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--管理spring配置文件-->
    <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>


  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

  1. 编写跳转的页面;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>springMVC</title>
</head>
<body>

${msg}

</body>
</html>

  1. 编写测试类

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*@Controller用于扫描控制器*/
@Controller
public class Controller2 {
	/*请求路径*/
    @RequestMapping("/h1")
    public String test(Model model){
        model.addAttribute("msg","hello2");
        return "hello";//进入了WEB-INF/jsp/hello.jsp页面
    }

    @RequestMapping("/h2")
    public String test2(Model model){
        model.addAttribute("msg","hello3");
        return "hello";
    }
}

  1. 在Idea里面启动 tomcat,在输入栏中输入:h1,或者 h2
    在这里插入图片描述
    就看到运行结果啦。。
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值