Springmvc @RequestMapping里的name参数是做什么用的?

请问下面的name项是什么意思?

@RequestMapping(value="/getProduct",name="产品中心",method=RequestMethod.GET)

阅读 13.2k
6 个回答

SpringMVC 4.0开始支持基于name值来构建访问的url,需要通过配置AbstractHandlerMethodMapping.,具体接口是HandlerMethodMappingNamingStrategy。
你可以自定义name属性,默认制是通过类的大写字符+#+方法名构建的,比如TestController的getUser方法,name值默认为TC#getUser。
使用上,官方说明是主要可以通过使用Spring jsp tag包里面的mvcUrl,来生成jsp到controller的链接

@RequestMapping("/people")
 class PersonController {

   @RequestMapping("/{id}")
   public HttpEntity getPerson(@PathVariable String id) { ... }

 }

JSP页面

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>

 <a href="${s:mvcUrl('PC#getPerson').arg(0,"123").build()}">Get Person</a>

感觉上比之前自己拼装URL(pageContext.request.contextPath)简单靠谱一点

就是一个名字,没有特别意思

新手上路,请多包涵

Assign a name to this mapping.(给这个mapping分配一个名称,没有实质性的作用)

类似于注释,对程序没什么影响,但是对人友好,因为有些url是拼音的,隔了几个月没有注释的话可能不知道这个url干吗的

新手上路,请多包涵

接口名字。

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {

    /**
     * Assign a name to this mapping.
     * <p><b>Supported at the type level as well as at the method level!</b>
     * When used on both levels, a combined name is derived by concatenation
     * with "#" as separator.
     * @see org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder
     * @see org.springframework.web.servlet.handler.HandlerMethodMappingNamingStrategy
     */
    String name() default "";
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题