有个url:/result/last/location/{locationId}/*如何通过 HttpServletRequest 用正则表达式把 {locationId} 中内容获取到求教!!!!谢谢!!!!!
4 回答
天涯尽头无女友
TA贡献1831条经验 获得超9个赞
你可以通过 spring的注解@PathVariable来获得locationId
public Result method(@PathVariable String locationId)
你也可以通过string 的split来截取
public class Test{
public static void main(String[] args){
String url = "/result/last/location/{locationId}/*";
String result = getStr(url,"location");
System.out.println(result);
}
public static String getStr(String url,String key){
if(url == null || key == null){
return "";
}
String[] strs = url.split("/");
for(int i=0;i<strs.length;i++){
if(strs[i].equals(key)&&i<strs.length-1){
return strs[i+1];
}
}
return "";
}
}
添加回答
举报
0/150
提交
取消