How to implement dynamic value transfer using @ PathVariable annotation
- 2021-12-05 06:08:39
- OfStack
Directory @ PathVariable annotation realizes dynamic value transfer and dynamic parameters are resolved by @ PathVariable
@ PathVariable annotation realizes dynamic value transfer
Dynamic value transfer
@RequestMapping(value="/Test/{id}")
public void Test(@PathVariable Integer id){
.............
}
Usage
In 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
There is now 1 hyperlink as follows
<a href="<c:url value=" rel="external nofollow" /actions/article/readArticle/${article.id}"/> "
target="_blank">${article.title}</a>
The characteristic of this hyperlink is that the id value parsed from the EL expression is added to the URL path.
Therefore, in the Controller layer of SpringMVC, it needs to be parsed using @ PathVariable ("articleId") Long articleId.
@ PathVariable is designed to resolve 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 URL Article specified in the path ID Number, to get the content of the article
*
* @param articleId Of the specified article ID No.
* @return Get the data for 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);
...
}
Thus, the value of ${article. id} on the page is finally mapped to ES50articleId in Java.