spring mvc 自动 映射方法名
?
spring mvc 默认 映射路径为 方法名
?
spring mvc 的具体用法 就不具体说明了。
首先是 用@Controller 标明类 ?@RequestMapping来标名具体的请求路径
?
每个Controller 上面写标记一下 @Controller 和?@RequestMapping 问题不大
?
Controller的每个方法都需要标注?@RequestMapping也没什么问题
问题是每个@RequestMapping必须指定路径。
个人觉得这个有些不必要。特殊情况下可以指定,非特殊情况下我觉得可以提供默认规则。
方法上指定一个@RequestMapping即可。如果没有指定value 那么 直接映射成方法名
?
spring mvc 不比struts ?struts所有的描述 都在xml中。命名规则再混乱 ?页面上找到请求去xml中搜寻 怎么样都能很快的找到对应的action及方法
而spring mvc 不同。所有的配置都是分散的。指定在controller及具体的方法上。如果路径的映射不规范,前台一个请求出错了难以找到对应的处理类及方法
?
下面看具体的实现。我这里用的是spring mvc 4
?
很简单。重写一下RequestMappingHandlerMapping即可
?
具体代码
?
import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition; import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition; import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition; import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition; import org.springframework.web.servlet.mvc.condition.RequestCondition; import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; public class MyRequestMappingHandlerMapping extends RequestMappingHandlerMapping { private boolean useSuffixPatternMatch = true; private boolean useTrailingSlashMatch = true; private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager(); private final List<String> fileExtensions = new ArrayList<String>(); @Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = null; RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (methodAnnotation != null) { RequestCondition<?> methodCondition = getCustomMethodCondition(method); info = createRequestMappingInfo(methodAnnotation, methodCondition,method); RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class); if (typeAnnotation != null) { RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType); info = createRequestMappingInfo(typeAnnotation, typeCondition,method).combine(info); } } return info; } protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition,Method method) { String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value()); if(patterns!=null && (patterns.length == 0)){ patterns= new String[]{method.getName().toLowerCase()}; } return new RequestMappingInfo( new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.fileExtensions), new RequestMethodsRequestCondition(annotation.method()), new ParamsRequestCondition(annotation.params()), new HeadersRequestCondition(annotation.headers()), new ConsumesRequestCondition(annotation.consumes(), annotation.headers()), new ProducesRequestCondition(annotation.produces(), annotation.headers(), this.contentNegotiationManager), customCondition); } }
?
? ? 老配置文件
? ?
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> </bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>text/json;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
?
? ? 新配置文件
? ?
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> </bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>text/json;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <bean name=‘handlerMapping‘ class=‘com.idresschina.idress.model.web.MyRequestMappingHandlerMapping‘> <!-- <property name=‘useTrailingSlashMatch‘ value=‘false‘></property> --> </bean>
?
? ?将老的配置文件替换成新配置文件
? ??
@RequestMapping("index1") public String index1(){ return "index1"; } @RequestMapping @ResponseBody public String index2(){ return "index2"; }
? ?/index1 对应 index1()
? /index2 对应 index2() ??
?
? ?2015-01-08 14:03:14,342 INFO [MyRequestMappingHandlerMapping] - <Mapped "{[/admin/index],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String web.ctrl.AppCtrl.index()>
2015-01-08 14:03:14,354 INFO [MyRequestMappingHandlerMapping] - <Mapped "{[/admin/index2],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String web.ctrl.AppCtrl.index2()>
?
? spring3.1之后?? ?<mvc:annotation-driven> ?标签默认给你干了两件事
? ?注册?RequestMappingHandlerMapping和RequestMappingHandlerAdapter
? ?
? spring3.1之前为DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter ?这两个类以不推荐使用
? ??
?
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。