- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Springmvc @PathVariable的用法解析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1
2
3
4
|
@RequestMapping
(value =
"/auth1/{uuid}/xxx"
, method = RequestMethod.GET)
public
void
imageCode1(
@PathVariable
(value =
"uuid"
) String uuid) {
logger.info(uuid);
}
|
见以上代码,url中的uuid如何解析成为参数传递进来.
(接收请求:如/auth1/xxxx-xxx-xxx/xxx) 。
1. 将/auth1/{uuid}/xxx根据/拆成 auth1、{uuid}、xxx 。
2. 将{uuid}替换成(.*),并纪录key为uuid 。
3. 同样将/auth1/xxxx-xxx-xxx/xxx拆成auth1、xxxx-xxx-xxx、xxx 。
4. 进行正则匹配,并根据group得到uuid=xxxx-xxx-xxx. 。
5. 将uuid=xxxx-xxx-xxx放入request的一个attribute中.
6. 根据反射和标注得到pathvariable名为uuid 。
7. 去request得到这个uuid,然后进行方法调用.
下面是测试springmvc的解析代码.
1
2
3
4
5
6
|
public
static
void
main(String[] args) {
AntPathMatcher matcher =
new
AntPathMatcher();
System.out.println(matcher.match(
"{uuid}"
,
"xxxx"
));
Map<String, String> result = matcher.extractUriTemplateVariables(
"{uuid}"
,
"xxx"
);
System.out.println(result);
}
|
当上述问题写成:
1
2
3
4
|
@RequestMapping
(value =
"/auth1/{uuid}/xxx"
, method = RequestMethod.GET)
public
void
imageCode1(
@PathVariable
String uuid) {
logger.info(uuid);
}
|
时,以下代码模拟测试了反射获取uuid的过程 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
static
void
main(String[] args)
throws
Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(A.
class
);
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
for
(MethodDescriptor methodDescriptor : methodDescriptors) {
System.out.println(
"method:"
+ methodDescriptor.getName());
ParameterDescriptor[] params = methodDescriptor.getParameterDescriptors();
if
(params !=
null
) {
for
(ParameterDescriptor param : params) {
System.out.println(
"param:"
+ param.getName());
}
}
}
Method[] methods = A.
class
.getMethods();
for
(Method method : methods) {
if
(method.getName().equals(
"hello"
)) {
LocalVariableTableParameterNameDiscoverer discoverer =
new
LocalVariableTableParameterNameDiscoverer();
String[] methodNames = discoverer.getParameterNames(method);
for
(String methodName : methodNames) {
System.out.println(methodName);
}
}
}
}
|
现在有如下的一条超链接 。
1
2
|
<a href=
"<c:url value="
/actions/article/readArticle/${article.id}
"/> "
target=
"_blank"
>${article.title}</a>
|
这条超链接的特点就是在URL路径中添加了EL表达式解析出来的id值.
因此,在SpringMVC的Controller层中,需要解析它,使用@PathVariable("articleId") Long articleId 来解析.
@PathVariable是专门用来解析URL请求中的动态参数.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
static
final
String URL_ARTICLE_READ =
"article/readArticle/{articleId}"
;
/**
* 去文章详情页面
* 根据URL路径中指定的文章ID号,去获取制定文章的内容
*
* @param articleId 指定的文章的ID号
* @return 获取此文章的数据,并去文章详情页面
*/
@RequestMapping
(value = {URL_ARTICLE_READ} )
public
ModelAndView readArticle(
@PathVariable
(
"articleId"
) Long articleId){
LOGGER.info(
"enter article detail page, articleId = {}"
,articleId);
final
Article article = articleService.getArticleById(articleId);
...
}
|
这样,页面上的${article.id}的值,就最终映射到了Java中的Long articleId 上了.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/zxy861114/article/details/51459077 。
最后此篇关于Springmvc @PathVariable的用法解析的文章就讲到这里了,如果你想了解更多关于Springmvc @PathVariable的用法解析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
基于SpringBoot版本如下: org.springframework.boot spring-boot-starter-parent 2.5.2
@RestController public class TestController { @GetMapping("/download") public ResponseEntit
概述 记得之前跟前端同事联调接口的时候,后端SpringMVC需要接收数组类型的参数,然后跟前端说需要传数组类型过来。后来前端童鞋传了数组,但是后端接收不成功,联调失败。那时候由于时间关系没有仔细研究
web.xml 片段: contextConfigLocation /WEB-INF/applicationContext-security.xml a
目录 相关准备 功能清单 具体功能:访问首页 ①配置view-controller ②创建页面
Spring mvc是一个非常轻量的mvc框架,注解可以大大减少配置,让请求的拦截变得比较简单。这次记录下@RequestBody 注解接收参数尤其是数组参数的用法。 关于容器的配置不再多说,这里
目录 SpringMVC默认处理的几种异常 @ResponseStatus 异常处理的顺序 自定义异常类(SpringMVC的异常处理)
目录 SpringMVC 接收前端传递的参数四种方式 @RequestParam 获取注解 @PathVariable获取注解 Sp
目录 @PathVariable的用法解析 问题描述 解析过程 动态参数使用@PathVariable
目录 SpringMVC @NotNull校验不生效 加了两个依赖问题解决 @NotNull注解失效原因之一 Lo
springmvc―handlermapping三种映射 handlermapping负责映射中央处理器转发给controller的映射策略,简单说就是控制中央处理器的请求触发哪一个control
目录 使用ModelAndView向request域对象共享数据 使用Model向request域对象共享数据 使用map向request域对象共享数据
整合SSM 环境要求 环境: IDEA MySQL5.7.19 Tomcat9 Maven3.6 要求: 需要熟练掌握MySQL数据库,Spring,Ja
目录 1、SpringMVC简介 2、工作流程与介绍 3、代码截图 以下组件通常使用框架提供实现: 1、Di
简介 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器来实现特定的功能。 过滤器
背景 举个例子,出现中文乱码的例子:提交表单的时候。 表单 ?
请求进入DispatcherServlet的doDispatch后,获取HandlerMethod。然后根据HandlerMethod来确认HandlerApapter,确认后执行HandlerAd
实现需求: 1.用户未登录,跳转到登录页,登录完成后会跳到初始访问页。 2.用户自定义处理(如需要激活),跳转到激活页面,激活完成后会跳到初始访问页。 使用到的框架 springmvc 的拦
为了实现用户登录拦截你是否写过如下代码呢? 1. 基于Filter ?
springmvc dao层和service层的区别 首先解释面上意思,service是业务层,dao是数据访问层 这个问题我曾经也有过,记得以前刚学编程的时候,都是在service里直接调用d
我是一名优秀的程序员,十分优秀!