In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
本篇内容主要讲解"如何使用@PathVariable",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用@PathVariable"吧!
动态参数使用@PathVariable解析
现在有如下的一条超链接
${article.title}
这条超链接的特点就是在URL路径中添加了EL表达式解析出来的id值。
因此,在SpringMVC的Controller层中,需要解析它,使用@PathVariable("articleId") Long articleId 来解析。
@PathVariable是专门用来解析URL请求中的动态参数。
在Controller层的代码如下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 上了。
获取路径中的参数值@PathVariable中的value
本期讲的是@PathVariable注解的具体使用。与@RequestParam的区别
@PathVariable获取URI地址中的参数值,需要结合URI模板映射
@PathVariable会将数据放到模型中,界面可以通过el表达式获取(即浏览器上面可以显示出来),但是@RequestParam不能通过界面获得
1.@PathVariable String name
加上@PathVariable就能够获取到浏览器中rul的参数值
@RequestMapping("/get_param")@Controllerpublic class GetParamController { @RequestMapping("/test6/{name}") public String test6(@PathVariable String name) { System.out.println("GetParamController test6()..."); System.out.println("name="+name); return "test";//跳转到test.jsp /* * ../test6/tom 结果:name=null 就要在参数里面加上@PathVariable 结果就变为:name=tom * ../test6/tom?name=kk 结果:name=tom 拿到的是URI路径里面的值跟后面的没有任何关系 * */ }}2.@PathVariable("username") String name
当浏览器中的是username,而后台要接收的是name
就会报错,由于username和name不匹配
此时要加上value值,@PathVariable("username")
@RequestMapping("/get_param")@Controllerpublic class GetParamController { @RequestMapping("/test7/{username}") //username与下面的name不一致,报错,就需要加上@PathVariable("username") public String test7(@PathVariable("username") String name) {//name和username不一致时,用@PathVariable("username") System.out.println("GetParamController test7()..."); System.out.println("name="+name); return "test";//跳转到test.jsp }}
此时就不会报错了
到此,相信大家对"如何使用@PathVariable"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.