Spring MVC ---- requestmapping

1. overview

1.1 spring MVC uses @ RequestMapping annotation to specify which URL requests can be processed for the controller;

1.2 @ RequestMapping can be marked at the class definition and method definition of the controller;

1.2.1 mark at class definition: provide preliminary request mapping information. Relative to the root directory of WEB application;

1.2.2 mark in method: provide further subdivision mapping information. Relative to the URL at the class definition;

If the class definition is not marked, the URL marked at the method is relative to the root directory of the WEB application;

1.3 after the dispatcher servlet intercepts the request, it determines the corresponding processing method through the mapping information provided by @ RequestMapping on the controller;

2. Code verification

Test Class:

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";    There will be many, so define a constant
        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. Browser request address

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

Keywords: Java Spring JSP

Added by jbulaswad on Tue, 24 Dec 2019 00:32:25 +0200