Kite的学习历程之SpringMVC入门案例

Kite学习框架的第十二天

1. SpringMVC入门案例

1.1 环境的搭建

使用maven的appwwb,下面为pom.xml引入的jar包

 <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>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>


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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</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>
      <scope>provided</scope>
    </dependency>

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


1.2 创建控制类

@Controller //交给ioc容器进行管理:控制器Controller负责处理由DispatcherServlet分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model返回给对应的View进行展示。

@RequestMapping(path="/hello") //@RequestMapping来映射Request请求与处理器


package cn.kitey.controller;

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

/**
 * 控制器类
 */
@Controller   //交给ioc容器进行管理:控制器Controller负责处理由DispatcherServlet分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model返回给对应的View进行展示。
public class HelloController {

    @RequestMapping(path="/hello") //@RequestMapping来映射Request请求与处理器
    public String Hello(){
        System.out.println("Hello SpringMvc");
        return "success";
    }

}

1.3 重新创建index.jsp页面,以及条装页面scuccess.jsp

index.jsp


<%--
  Created by IntelliJ IDEA.
  User: 小风筝
  Date: 2020/6/18
  Time: 9:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>入门程序</title>
</head>
<body>

    <h3>入门程序</h3>
    <%--相对的路径写法--%>
    <a href="hello">入门程序</a>
</body>
</html>

scuccess.jsp 创建于WEB-INF/pages/下

<%--
  Created by IntelliJ IDEA.
  User: 小风筝
  Date: 2020/6/18
  Time: 9:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>入门</title>
</head>
<body>
    <h3>表示你已经入门成功了!</h3>
</body>
</html>


1.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.kitey"></context:component-scan>

    <!--开启视图解析器 -->
    <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>

    <!--开启SpringMVC框架注解的支持-->
    <mvc:annotation-driven></mvc:annotation-driven>



</beans>

1.5 配置web.xml wen文件

contextConfigLocation: 使用前端加载器加载spring的配置文件


<!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>
  <display-name>Archetype Created Web Application</display-name>

  <!--前端控制器的配置-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--使用前端控制器加载配置文件-->
    <init-param>
      <param-name>contextConfigLocation </param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--为了启动时就进行创建-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

运行结果截图
Kite的学习历程之SpringMVC入门案例_第1张图片
当我们点击了入门案例后,就会跳转到success.jsp这里我们借助了注解
RequestMappering(path="/hello")执行了控制类中的方法Hello进行返回,这时,springmvc.xml中的视图解析器开始解析,通过internalResourceViewResolver中的prefix。suffix进行问价匹配到了success.jsp.这时入门案例运行成功!

你可能感兴趣的:(每天的学习笔记)