Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ PathVariable annotations to dynamically pass values

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use @ PathVariable annotation to achieve dynamic value transfer", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use @ PathVariable annotations to dynamically pass values".

@ PathVariable annotation to realize dynamic value transfer

Dynamic value transfer

@ RequestMapping (value= "/ Test/ {id}") public void Test (@ PathVariable Integer id) {.}

Usage

In the action of the page form, write the corresponding method name in controller

TestController.java@RequestMapping (value= "/ {methodName}") public String TZ (@ PathVariable String methodName) {return methodName;} dynamic parameters are resolved using @ PathVariable

Now there is a hyperlink as follows

${article.title}

The feature of this hyperlink is that the id value parsed by the EL expression is added to the URL path.

Therefore, in the Controller layer of SpringMVC, you need to parse it, using @ PathVariable ("articleId") Long articleId to parse.

@ PathVariable is specifically used to parse dynamic parameters in URL requests.

The code in the Controller layer is as follows

Public static final String URL_ARTICLE_READ = "article/readArticle/ {articleId}" / * go to the article details page * according to the article ID number specified in the URL path, get the content of the article * * @ param articleId the ID number of the article specified * @ return to obtain the data of this article And go to the article details page * / @ 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);.}

In this way, the value of ${article.id} on the page is finally mapped to Long articleId in Java.

At this point, I believe you have a deeper understanding of "how to use @ PathVariable annotations to achieve dynamic value transfer". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report