srping MVC requestmapping Implementation

Source: Internet
Author: User

The URL that defines the request in spring MVC only needs to be annotated on the method: @RequestMapping ("Aa.mvc") to define the URL address of the access. But have you ever thought about why you can add this annotation to the definition of URL access address? The following analysis of his implementation principle!

First define the annotations requestmapping

@Retention (Retentionpolicy.runtime)
@Target (value = {elementtype.method, elementtype.type})
Public @interface requestmapping
{
Public String value ();
}

In MVC, it is often necessary to verify the validity of the input values, so the annotations of the checksum are also defined Myvalid

@Retention (Retentionpolicy.runtime)
@Target (Elementtype.parameter)//use in Parameters
Public @interface Myvalid
{
Public String value ();
}


In the test class:

Package COM.CML.MVC;


Import java.lang.annotation.Annotation;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Type;


public class Parameters
{
public static void Main (string[] args) throws Exception
{
Class CLZ = Parameters.class;
Method[] methods = Clz.getdeclaredmethods ();


for (Method method:methods)
{
Is there an annotation on the method @requestmapping
if (Method.isannotationpresent (Requestmapping.class))
{
requestmapping re = method.getannotation (Requestmapping.class);
System.out.println ("Requested URL:" + re.value ());
Get all annotations on a method
annotation[][] annotations = method.getparameterannotations ();


For (annotation[] parameters:annotations)
{
for (Annotation an:parameters)
{
if (an.annotationtype () = = Myvalid.class)
{
System.out.println ("Enter custom check! ");
}
}
}
}
}
}


@RequestMapping ("Aa.mvc")
public void Test (@MyValid ("test") String name, int type)
{
}
}

The main use of knowledge or reflection of the content, so there is no reflection on the majority of the framework!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.