@RequestMapping注解&@PathVariable 注解

一、 @RequestMapping注解

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

1.@requestMapping 可以定义在 类 和 方法 上

package com.ibigsea.springmvc.helloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
	/**
	 * 配置@RequestMapping 拦截 localhost:8080/springmvc/hello 请求 
	 * @return
	 */
	@RequestMapping("/hello")
	public String helloWorld() {
		System.out.println("hello world");
		return "helloworld";
	}
}

package com.ibigsea.springmvc.helloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloWorld {
	/** 	 * 配置@RequestMapping 拦截 localhost:8080/springmvc/hello/world 请求  	 * @return 	 */ 	@RequestMapping("/world") 	
	public String helloWorld(){ 	
		System.out.println("hello world"); 	
			return "helloworld"; 
				} 
		}

@RequestMapping

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

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

类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于

WEB 应用的根目录

DispatcherServlet 截获请求后,就通过控制器上

@RequestMapping 提供的映射信息确定请求所对应的处理方法。

@RequestMapping 除了可以使用请求 URL 映射请求外,

还可以使用请求方法、请求参数及请求头映射请求

2… @RequestMapping限定请求方法、请求参数、请求头
/**
* 接收GET请求
* @return
*/
@RequestMapping(value="/get",method = RequestMethod.GET)
public String get(){
System.out.println(“get”);
return “get”;
}

/**
 * 接收POST请求
 * @return
 */
@RequestMapping(value="/post",method = RequestMethod.POST)
public String post(){
	System.out.println("post");
	return "post";
}

/**
 * 只接收 name 参数
 * @return
 */
@RequestMapping(value="/params",params="name")
public String params(String name){
	System.out.println("hello "+name);
	return "helloworld";
}

/**
 * 只接收请求头中 Content-Type 为 text/html;charset=UTF-8的请求
 * @return
 */
@RequestMapping(value="/headers",headers="Content-Type:text/html;charset=UTF-8")
public String headers(){
	System.out.println("headers");
	return "helloworld";
}

1.3. @RequestMapping匹配符
– ?:匹配文件名中的一个字符

– *:匹配文件名中的任意字符

匹配多层路径

实例:

URL : /user/*/create

– /user/bigsea/create 、 /user/sea/create 等URL

URL : /user/**/create

– /user/big/sea/create 、 /user/sea/big/create 等URL

URL : /user/create??

– /user/createaa 、/user/createbb


二. @PathVariable 注解

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的入参中。

/**
* localhost:8080/springmvc/hello/pathVariable/bigsea
* localhost:8080/springmvc/hello/pathVariable/sea
* 这些URL 都会 执行此方法 并且将 bigseasea 作为参数 传递到name字段

 * @param name
	 * @return
	 */
	@RequestMapping("/pathVariable/{name}")
	public String pathVariable(@PathVariable("name")String name){
		System.out.println("hello "+name);
		return "helloworld";
	}

JSP(这里指定全路径):

pathVariable

> <a href="${pageContext.request.contextPath}/hello/pathVariable/bigsea"
> > name is bigsea </a> <br/> <a href="${pageContext.request.contextPath}/hello/pathVariable/sea" >
> name is sea</a> <br/>

分别点击超链接运行结果:

hello bigsea
hello sea

@PathVariable注解,让spring支持参数带值功能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值