SpringMVC(三):@RequestMapping中的URL中设定通配符,可以使用@PathVariable映射URL绑定的占位符

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

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

 在HelloWord.java中添加testPathVariable方法:

package com.dx.springlearn.handlers;

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;

@Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
    private static String SUCCESS = "success";

    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(value = "id", required = true) Integer id) {
        System.out.println("testPathVariable: id: " + id);
        return SUCCESS;
    }
}

index.jsp添加链接:

<a href="class_requestmapping/testPathVariable/1">testPathVariable</a>
    <br>

 

posted @ 2018-01-04 15:37  cctext  阅读(4831)  评论(0编辑  收藏  举报