SpringMVC入门案例/RequestMapping注解详解
项目结构
配置文件
-
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>
-
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>
-
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>
控制器
-
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"; } }
前台页面
-
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>
-
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>