Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use @ PathVariable annotations to dynamically pass values

Shulou Source: shulou.com Published: 2022-06-02 19:39:51 09月14日 Update

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!

Tags: Dynamic articles annotations pages content parameters methods details paths links learning practical deeper middle finger code interest practicality practicality that is easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Shulou Information Apple NVidia