SpringMVC(一)---SpringMVC入门

时间:2019-08-06
本文章向大家介绍SpringMVC(一)---SpringMVC入门,主要包括SpringMVC(一)---SpringMVC入门使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一,MVC 概述

MVC:模型,视图,控制器,是一种软件设计规范,本质是将业务逻辑,数据,显示,分离的方式来编写代码;前后端分离
Model:数据模型,提供要展示的数据,一般我们都会把这两个分离开来。数据Dao,服务层Service
View:负责进行数据的渲染和展示,客户端想要看到的东西
Controller:接收用户请求,交给Model处理,从Model更新后的数据或者结果,返回给前端

1,Model1

在早期 Java Web 的开发中,统一把显示层、控制层、数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1

Model1的弊端

  • JSP 和 Java Bean 之间严重耦合,Java 代码和 HTML 代码也耦合在了一起
  • 要求开发者不仅要掌握 Java ,还要有高超的前端水平
  • 前端和后端相互依赖,前端需要等待后端完成,后端也依赖前端完成,才能进行有效的测试
  • 代码难以复用

    2,Model2

    因为Model1的种种弊端,所以很快这种方式就被 Servlet + JSP + Java Bean 所替代了,首先用户的请求会到达 Servlet,然后根据请求调用相应的 Java Bean,并把所有的显示结果交给 JSP 去完成,这样的模式我们就称为 MVC 模式

    controller(控制器):
  • 取得表单的数据
  • 调用业务的逻辑方法
  • 转向指定的页面

Model(模型):

  • Dao:操作数据库
  • Service:业务逻辑
  • 保存数据的更新状态

View(视图):

  • 网页, JSP,用来展示模型中的数据

二,SpringMVC 架构

SpringMVC 是 Spring的一部分,是基于Java实现的MVC的轻量级Web框架

SpringMVC优点

  • 趋势,使用的人多
  • 简单,易学,轻量级
  • 高效,基于请求和响应的MVC框架
  • 约定优于配置
  • 功能强大:RestFul,数据验证,格式化,主题,本地化,异常处理......

三,Hello SpringMVC

让我们来写一下我们的第一个 Spring MVC 程序

第一步:创建一个Maven项目

第二步:导入相关架包

<dependencies>
  <!--junit包单元测试-->
  <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>
    <!--解决资源导出问题-->
    <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,注册DispatcherServlet

**映射路径为 / 【不要用/*,会404】**

<?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">

  <!--1.注册DispatcherServlet-->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--2.关联SpringMVC配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    
    <!--3.这个东西要和服务器一起启动-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

第四步:配置springMVC的配置文件

在resources下创建springmvc-config.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <!--扫描指定包下的注解,让指定的类能够被IOC容器管理-->
    <context:component-scan base-package="com.shandx.controller"/>

    <!--静态资源过滤-->
    <mvc:default-servlet-handler/>

    <!--MVC注解驱动-->
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean id="InternalResourceViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/"/>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

第五步:编写Controller类

//@Controller是为了让Spring IOC容器初始化时自动扫描到
@Controller
public class HelloWorldController {

    //请求映射("路径")
    @RequestMapping("/hello")
    public String hi(Model model){
        model.addAttribute("msg","Hello,SpringMVC");
        
        //方法返回的结果是视图的名称hello
        // 加上配置文件中的前后缀变成WEB-INF/hello.jsp。
        return "hello"; //WEB-INF/jsp/hello.jsp
    }
}

第六步:编写视图层

注意视图的位置,要和视图解析器对应 web-inf / jsp,可以通过EL表示取出Model中存放的值

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SpringMVC</title>
</head>
<body>

${msg}

</body>
</html>

第七步:启动Tomcat进行测试

1,配置Tomcat

2,测试结果

项目结构

原文地址:https://www.cnblogs.com/tqsh/p/11309864.html