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!