SpringMVC(四):@RequestMapping -->@PathVariable注解
SpringMVC(四):@RequestMapping -->@PathVariable注解
LevelCoder 发表于7个月前
SpringMVC(四):@RequestMapping -->@PathVariable注解
  • 发表于 7个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

@PathVariable

先来看下@PathVariable源码,解释为针对URI模板变量进行绑定

/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Annotation which indicates that a method parameter should be bound to a URI template
 * variable. Supported for {@link RequestMapping} annotated handler methods in Servlet
 * environments.
 *
 * <p>If the method parameter is {@link java.util.Map Map&lt;String, String&gt;} or
 * {@link org.springframework.util.MultiValueMap MultiValueMap&lt;String, String&gt;}
 * then the map is populated with all path variable names and values.
 *
 * @author Arjen Poutsma
 * @since 3.0
 * @see RequestMapping
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
 * @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {

	/**
	 * The URI template variable to bind to.
	 */
	String value() default "";

}

 

@PathVariable常用占位符参数请求模式,同时支持简单正则表达式模式,下面我们来看案例

控制层

package com.levelcoder.controller;

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

/**
 * 
 * 描述:@RequestMapping @PathVariable 注解
 *
 * 作者:LevelCoder
 *
 * 邮箱:LevelCoder@126.com
 *
 * 日期:2017年6月1日 下午9:08:32
 *
 * 版本:V1.0.0
 */
@Controller
public class RMPathVariableController {
	
	
	/**
	 * 占位符匹配
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/rm/pathVariablePlaceholder/{id}")
	public String pathVariablePlaceholder(@PathVariable("id") Integer id){
		System.out.println("id=" + id);
		return "successPathVariable";
	}
	
	
	/**
	 * 正则表达式匹配
	 * @return
	 */
	@RequestMapping(value="/rm/pathVariableRegexp/{regexp:[a-z-]}")
	public String pathVariableRegexp(@PathVariable("regexp") String regexp){
		System.out.println("正则表达式注解模式参数= : " + regexp);  
        return "successPathVariableRegexp";  
	}
	
}

 

测试index页面

<%@ 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>
	<p>@PathVariable注解</p>
	<a href="rm/pathVariablePlaceholder/1">占位符模式</a>
	<a href="rm/pathVariableRegexp/1">正则表达式模式</a>		
</body>
</html>

 

测试结果页面

<%@ 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>Spring MVC : RequestMapping PathVariable</title>
</head>
<body>
	恭喜LevelCoder:RequestMapping @PathVariable 程序跑通了,继续学习吧
</body>
</html>

 

测试结果

demo地址: http://git.oschina.net/LevelCoder/demos

标签: springmvc
共有 人打赏支持
粉丝 8
博文 54
码字总数 23491
×
LevelCoder
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: