SpringMVC入門案例/RequestMapping註解詳解

SpringMVC入門案例/RequestMapping註解詳解

項目結構

在這裏插入圖片描述

配置文件

  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">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.xiaoge</groupId>
      <artifactId>springmvc_start</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>springmvc_start 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>
        <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-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>
    
      <build>
        <finalName>springmvc_start</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. web.xml

    <!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>
    
        <!-- 指定加載的配置文件 ,通過參數contextConfigLocation加載-->
        <init-param>
    
          <param-name>contextConfigLocation</param-name>
    
          <param-value>classpath:spring.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>
    
    
    </web-app>
    
    
  3. spring.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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/mvc
            https://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="com.xiaoge"></context:component-scan>
    
        <!-- 視圖解析器對象 -->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 指定控制器對象返回的jsp名稱, 去哪個目錄下尋找 -->
            <property name="prefix" value="/WEB-INF/pages/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!--開啓springMVC框架註解的支持-->
        <mvc:annotation-driven />
    </beans>
    

控制器

  1. HelloController**(重點註釋-解釋RequestMapping註解)**

    package com.xiaoge.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * 控制器類
     *
     * @RequestMapping註解:
     *      作用:
     *          放在類上一級訪問目錄
     *          放在方法上二級訪問目錄
     *      屬性:
     *          value: 用於指定請求的url. 它和path屬性的作用是一樣的
     *          method: 用於指定請求的方式
     *          params: 用於指定限制請求參數的條件. 它支持簡單的表達式.
     *                  要求請求參數的key和value必須和配置的一模一樣
     *                  列:
     *                      params = {"accountName"}, 標識請求參數必須有accountName
     *                      params = {"money!100"}, 標識請求參數中money不能是100.
     *          headers: 用於指定配置請求頭的條件.
     *      注意:
     *          以上四種屬性只要出現2個或以上是, 他們的關係是與的關係.
     */
    @Controller
    @RequestMapping(path = "/user")
    public class HelloController {
        /*
            SpringMVC中的註解
            設置請求的路徑
         */
        @RequestMapping(path = "/sayHello")
        public String sayHello(){
            System.out.println("Spring MVC");
            return "success";
        }
    
        /**
         * 測試RequestMapping註解
         * @return
         */
        @RequestMapping(value = "/testRequestMapping", method = {RequestMethod.POST}, params = {"username"}, headers = {"accept"})
        public String testRequestMapping(){
            System.out.println("測試testRequestMapping註解....");
            return "success";
        }
    
    }
    
    

前臺頁面

  1. index.jsp

    <%--
      Created by IntelliJ IDEA.
      User: xiaoge
      Date: 2020/4/19
      Time: 下午1:59
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>首頁</title>
    </head>
    <body>
    <a href="user/sayHello">RequestMapping</a>
    </body>
    </html>
    
    
  2. success.jsp

    <%--
      Created by IntelliJ IDEA.
      User: xiaoge
      Date: 2020/4/19
      Time: 下午1:59
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>success</title>
    </head>
    <body>
        <h3>成功</h3>
    </body>
    </html>
    
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章