spring mvc 5.0 @PathVariable 详解 04

来源:互联网 发布:屏幕去蓝光软件 编辑:程序博客网 时间:2024/06/05 05:07


带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义

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


index.jsp

<html><body><h2>Hello World!</h2><a href="hello/abc">hello abc</a><br><br></body></html>

Controller

package com.ruge.mvc;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/** * @author 爱丽丝、如歌 * @Description: TODO * @date 2017/12/19 13:44 */@Controllerpublic class demo01 {    @RequestMapping(value = "/hello/{name}" , method = RequestMethod.GET)    public String hello(@PathVariable(value = "name") String name){        System.out.println("hello"+name);        return "hello";    }}