springmvc二: @RequestMapping

本文详细解析了SpringMVC中@RequestMapping注解的使用,包括如何为控制器指定URL请求,以及如何通过value、method、params等属性实现更精确的请求映射。同时介绍了@RequestMapping支持的ant风格路径匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  spring mvc 使用   @RequestMapping注解为控制器指定可以处理哪些URL请求.

 

@RequestMapping 不仅可以修饰方法,也可以修饰类.

       类定义处:

  提供初步的请求映射信息,相对于WEB应用的根目录.

       方法定义处:

  提供进一步的细分映射信息。相对于类定义处的URL。若类定义处未标注@RequestMapping,则方法出标记的URL相对于WEB应用的根目录。

   如下: 请求的url就会变成 http://localhost:8083/springmvc-1/SpringMvc/TestMapping

package com.atChina.controller;

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

@RequestMapping("/SpringMvc")
@Controller
public class RequestMapperingTest {
	
	@RequestMapping("/TestMapping")
	public String TestMapping(){
		System.out.println("TestMapping");
		return "success";
	}
}

  

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

 

 @RequestMapping 的value,mthod, params,heads属性可以让请求映射更精确化

ge

package com.atChina.controller;

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

@RequestMapping("/SpringMvc")
@Controller
public class RequestMapperingTest {
	
    // 只能映射 POST方法的请求
	@RequestMapping(value="/testMethod", method=RequestMethod.POST)
	public String TestMethod(){
		System.out.println("testMethod");
		return "success";
	}
	
}

 @RequestMapping支持ant风格

package com.atChina.controller;

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

@RequestMapping("/SpringMvc")
@Controller
public class RequestMapperingTest {
	
	@RequestMapping("/TestAntPath/*/abc")
	public String TestAntPath(){
		System.out.println("TestAntPath");
		return "success";
	}
	
}

 

 @PathVariable映射URL绑定的占位符

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值