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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM