编写第一个SpringMVC程序

什么是SpringMVC

MVC : 模型、视图、控制器 , 是一种软件设计规范,说明不是设计模式;

本质:将业务逻辑 , 数据 , 显示 分离的方式来编写代码; 前后端分离;

Model:数据模型,提供要展示的数据,一般我们都会把这两个分离开来 , 数据Dao,服务层Service。

View :负责进行数据的渲染和展示;客户端想要看到的东西

Controller:接收用户请求,交给Model处理,从Model更新后的数据或者结果,返回给前端。调度员

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

我们为什么要学习SpringMVC

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

HelloSpringMVC

我们利用Maven创建一个web项目;
项目结构
在这里插入图片描述
1.Maven导包

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

  <groupId>org.westos</groupId>
  <artifactId>SpringMVC-730</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SpringMVC-730 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-730</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>
  </build>
</project>

2.注册SpringMVC核心Servlet
web.xml

  • 注册DispatcherServlet
  • 关联SpringMVC的配置文件
  • 启动级别为1
  • 映射路径为 / 【不要用/*,会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.注册servlet-->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
    <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>

  <!--所有请求都会被springmvc拦截 -->
  <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>

3.编写SpringMVC的配置文件

springmvc-servlet.xml

  1. 让IOC的注解生效
  2. 静态资源过滤 :HTML . JS . CSS . 图片 , 视频 …
  3. MVC的注解驱动 :
  4. 配置视图解析器
<?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="org.westos.controller"/>

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

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

    <!--
    视图解析器
    作用;方便统一管理
    -->
    <bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>

4.编写Controller类

  • 需要给类加注解 :@Controller , 可以让IOC容器管理 , 等价于Bean
  • 请求映射的路径 :@RequestMapping (“路径”),如果类上有就先写类的,在写方法的;
  • Model :模型传递参数
  • return “hello”; 它会去视图解析中拼接前缀和后缀来找到对应的视图
package org.westos.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/hello")
public class HelloWorldController {

    //请求映射("路径")
    @RequestMapping("/hello")
    public String hi(Model model){
        model.addAttribute("msg","Hello,SpringMVC");
        System.out.println("进入HELLO");
        return "hello"; //WEB-INF/jsp/hello.jsp
    }

}

视图层
hello.jsp

  • 注意视图的位置,要和视图解析器对应 web-inf / jsp , 为什么需要在web-inf路径下 , 因为这个路径下的东西用户无法直接访问 , 更加安全;
  • 可以通过EL表示取出Model中存放的值,或者对象;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SpringMVC</title>
</head>
<body>

${msg}

</body>
</html>

测试

启动Tomcat进行测试,

流程

  1. 建立web项目
  2. 导包
  3. 编写web.xml
  4. 编写springmvc的配置文件 【servletname】-servlet.xml
  5. 编写控制类
  6. 编写视图层
  7. 测试
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值