时间:2021-12-03 09:26:56 | 栏目:JAVA代码 | 点击:次
最近项目中需要对controller传入的应用标识(appMarkId)进行校验,appMarkId@PathVariable传入到url模版中的,这里用到了aop统一拦截处理,但是在拦截的过程中发现request获取不到该参数,随后进行了研究。

Map map = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
可以用这个方法获取到所有uri模版中传入的参数。

ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletResponse response = servletRequestAttributes.getResponse();
<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请求中的动态参数。
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 上了。