SpringMVC快速入门

第一章:三层架构和MVC

1.1 三层架构

  1. 服务器端程序的开发,一般都基于两种形式,一种C/S架构程序,一种B/S架构程序;
  2. 使用Java语言基本上都是开发B/S架构的程序,B/S架构又分成了三层架构;
  3. 三层架构

    1. 表现层:WEB层,用来和客户端进行数据交互的。表现层一般会采用MVC的设计模型;
    2. 业务层:处理公司具体的业务逻辑;
    3. 持久层:用来操作数据库;

1.2 MVC模型

  1. MVC全名是Model View Controller 模型视图控制器,每个部分各司其职;
  2. Model:数据模型,JavaBean的类,用来进行数据封装;
  3. View:指JSP、HTML用来展示数据给用户
  4. Controller:用来接收用户的请求,整个流程的控制器。用来进行数据校验等;

第二章:SpringMVC的快速入门

2.1 SpringMVC的概述

是一种基于MVC设计模型的实现的请求驱动类型的轻量级WEB框架;

2.2 SpringMVC的入门程序

2.2.1 创建WEB工程

1)File -> New -> Module

2)Maven -> SDK -> Create from archetype

3)Name -> Next

4)Maven home directory -> User settings file -> Local repository -> Finish

2.2.2 导入坐标

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- 版本锁定 -->
<spring.version>5.0.2.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
</dependencies>

2.2.3 配置核心的控制器(配置DispatcherServlet)

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <!-- 配置核心的控制器 -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 此Servlet被加载时,会加载contextConfigLocation对应的文件(即:服务器启动,则立即加载DispatcherServlet,而DispatcherServlet被创建时则会初始化springmvc.xml,即启动Spring容器) -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!-- 告知服务器,当服务器启动时,立即加载此Servlet -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 配置拦截器解决中文乱码问题 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

2.2.4 编写springmvc.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="cn.itcast"></context:component-scan>

    <!-- 开启springmvc的注解驱动 -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 开启视图解析器 -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

2.2.5 编写index.jsp(发起请求的页面)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h3>入门页面</h3>
        <a href="user/hello">入门案例</a>
    </body>
</html>

2.2.6 编写HelloController控制器类(处理请求)

@Controller
public class HelloController {
    @RequestMapping(path = "/hello")
    public String hello() {
        System.out.println("hello()方法执行了!");
        return "success";
    }
}

2.2.7 编写success.jsp(处理成功后跳转到的页面)

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
    <head>
        <title>成功页面</title>
    </head>
    <body>
        <h3>入门成功!</h3>
    </body>
</html>

2.2.8 启程服务,运行结果

1)请求页面

2)后台输出

3)页面跳转

2.2.9 工程目录结构

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章