Python 官方文档:入门教程 => 点击学习
目录@PathVariable注解实现动态传值动态参数使用@PathVariable解析@PathVariable注解实现动态传值 动态传值 @RequestMapping(va
动态传值
@RequestMapping(value="/Test/{id}")
public void Test(@PathVariable Integer id){
.............
}
用法
在页面表单的action中,写controller中对应的方法名
TestController.java
@RequestMapping(value="/{methodName}")
public String TZ(@PathVariable String methodName){
return methodName;
}
现在有如下的一条超链接
<a href="<c:url value=" rel="external nofollow" /actions/article/readarticle/${article.id}"/> "
target="_blank">${article.title}</a>
这条超链接的特点就是在URL路径中添加了EL表达式解析出来的id值。
因此,在springMVC的Controller层中,需要解析它,使用@PathVariable("articleId") Long articleId 来解析。
@PathVariable是专门用来解析URL请求中的动态参数。
在Controller层的代码如下
public static final String URL_ARTICLE_READ = "article/readArticle/{articleId}";
@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 上了。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 使用@PathVariable注解如何实现动态传值
本文链接: https://www.lsjlt.com/news/155943.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0