SpringMVC----@RequestMapping__修饰类

1.概述

  1.1  SpringMVC使用@RequestMapping注解为控制器指定可以处理哪些URL请求;

  1.2  在控制器的类定义及方法定义处都可以标注@RequestMapping;

      1.2.1 类定义处标注:提供初步的请求映射信息。相对于WEB应用的根目录;

      1.2.2 方法处标注: 提供进一步的细分映射信息。相对于类定义处的URL;

                 若类定义处未标注,则方法处标记的URL相对于WEB应用的根目录;

  1.3  DispatcherServlet截取请求后,就通过控制器上@RequestMapping提供的映射信息确定请求所对应的处理方法;

2.代码验证

Test类:

package com.yk.springmvc.handlers;

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

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
    
    private static final String SUCCESS = "success";
    
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping(){
        System.out.println("SpringMVCTest.testRequestMapping()");
//        return "success";    下面会有很多,所以定义一个常量
        return SUCCESS;
    }    
}


index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="springmvc/testRequestMapping">Test--RequestMapping</a>
    <br /><br />
    <a href="helloworld">Hello World</a>
</body>
</html>

3.浏览器请求地址

1.    http://localhost:8080/SPRING-MVC-01/
2.    http://localhost:8080/SPRING-MVC-01/springmvc/testRequestMapping

猜你喜欢

转载自www.cnblogs.com/yikuan-919/p/9721683.html