• Spring中Controller和RequestMapping的详解


    先看一个简单的实例:

    @Controller
    @RequestMapping("/hello")
    public class anyTypeController{
        @RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
        public String processWebRequest(){
        	return "hello";
        }
    }

    上述例子中简单了阐明了Controller以及RequestMapping的用法。含义是

    将这个class申明为一个bean,当web请求为/hello时,跳转到这个class进行处理。


    Controller的原型分析:
    既然上文说了,此处的不需要再配置文件中配置例如<bean id="xxx" class="com.xxx">的语句,但是我们的Spring如何知道哪些class是bean文件,哪些class文件不是bean文件呢? 按照Spring教材上所讲,此处所有配置了@Controller 的class文件回通过java的反射机制进行读取,因此在这里Spring2.5官方的 org.springframework.web.servlet.mvc.annotation.defaultAnnotationHandlerMapping 这个类进行处理。Ps:这里想进一步学习的同学可以查看源码进行分析。这个类的作用是:首先扫描Classpath获取注解了@Controller的对 象(扫面通过语句:<context:component-sacn/>进行扫描)。 之后通过反射机制进行绑定。

    自定义用于基于注解的HandlerAdapter:

    上面我们已经通过注解的方式配置好了bean,但是我们bean中方法不只是一个,我们怎么知道每次访问的是Controller的哪一个具体的方 法呢?具体方法是使用@RequestMapping()注解,然后利用 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 的反射机制,将Controller中的具体方法和请求方法名绑定起来。Ps:想了解更深的同学请看Spring的源码。


    @RequestMapping

    由于在网上搜集到的@RequestMapping的资料很不齐全,看完云里雾里,于是找了本书总结了一下。

    @RequestMapping可以被标注类型申明和方法申明这两种方式上。

    申明在类型定义上,那么@RequestMapping() 括号里面的东西通常为请求的地址

    申明在方法定义上,那么它用来表明当面方法定义是一个Web请求处理方法。(Ps:这是书上讲的,个人理解的是加上具体的映射条件,使映射不会出错)

    下来看个例子,详细说明:

    @Controller
    @RequestMapping("/hello")
    public class MyController{
    	@RequestMapping(method=RequestMethod.GET)
    	public String function1(..){..}
    	@RequestMapping(method=RequestMethod.POST)
    	public String function2(..){..}
    }


    类型定义上标注的@RequestMapping("/hello")的意思是,所有提交到/hello的web请求都有MyController处理。 但是问题来了,假设这个bean里面有很多个函数,到底决定由哪一个来处理呢?接下来会使用在方法定义上的@RequestMapping()进行进一步 的缩小范围。如次所以,所有GET方法请求都会被function1处理,POST请求被function2处理。除了method可以区分,我们还有 param可以区分(因为一个bean里面也不仅仅只有两个函数。GET和POST并不能使用于所有的地方)。

    params属性的两种表达式形式:

    1.“参数名=参数值”,通常参数名相同,后面的参数值用来区分并且界定请求的处理范围。实例:

    @Controller
    @RequestMapping("/hello")
    public class MyController{
    	@RequestMapping(params="locale=en",{method=RequestMethod.POST})
    	public String function1(..){..}
    	@RequestMapping(params="locale=ch",method={RequestMethod.POST})
    	public String function2(..){..}
    }


    这里,当请求为http://xxx/xxx/hello?locale=en  时,调用function1(..)函数

    当请求是http://xxx/xxx/hello?locale=ch时,调用function2()函数


    2.“paramter” 形式的表达式。 这里判断请求中时候存在某一个参数来决定当前请求交个哪个方法处理。

    @Controller
    @RequestMapping("/hello")
    public class MyController{
    	@RequestMapping(params="delete",{method=RequestMethod.POST})
    	public String function1(..){..}
    	@RequestMapping(params="update",method={RequestMethod.POST})
    	public String function2(..){..}
    }

    这里请求可以是http://xxxxxxx/hell?delete  就会访问function1

    @RequestMapping还有一一种全部用于方法级别的绑定方式.例:注意,这里用的是value

    @Controller
    public class MyController{
    	@RequestMapping("/hello1")
    	public String function1(..){...}
    	@RequestMapping(value="/hello2",{method=RequestMethod.POST})
    	public String function2(..){..}
    	@RequestMapping(value="/hello3",method={RequestMethod.POST})
    	public String function3(..){..}
    }


    上述仅仅讲了映射的相关知识。下来看如果带有参数的请求应该如何处理。

    请求参数到方法参数的绑定

    1.默认绑定行为

    假设有如下请求:<a href="/hello?age=10?name=jack">

    @Controller
    public class MyController{
    	@RequestMapping("/hello")
    	pulbic String function1(int age ,String name){
    		System.out.println(age + name);
    		return "success";
    	}
    }
    


    在这里会发现,age 和name自动赋给了函数参数中的age和name。 这是以为默认绑定行为是根据名称匹配原则进行的函数绑定。当请求中的参数名与方法中的参数名一致,相应的参数值将被绑定到对应的方法参数上(这里框架类保证了请求参数完成了类型转换)。

    2.使用@RequestParam明确的指定绑定关系.同样是上面的例子.

    @Controller
    public class MyController{
    	@RequestMapping("/hello")
    	pulbic String function1(@RequestParam("ageOfJack") int age ,String name){
    		System.out.println(age + name);
    		return "success";
    	}
    }
    


    这时候同样是刚才的web请求,已经可以将age的值赋给了ageOfJack参数。默认情况下,如果@RequestParam请求的数据不存在,会抛 出异常。这是因为他的required属性决定的。他的默认值是true.假设将他设置为false,即该参数不存在时不会报错,对应方法将获取到该类型 的默认值。

    pulbic String function1(@RequestParam("ageOfJack",required=false) int age ,String name)

    3.添加自定义数据绑定规则。

    Spring框架提供的数据绑定才用JavaBean规范的PropertyEditor机制进行数据转换。在基于注解的Controller中,可以用下面两种方式达到这一目的.

    1.使用@InitBinder(针对一个Controller)

    2.指定自定义的WebBindingInitialize。(这个是针对多个Controller)

    这个感觉用的比较少,留下记号,遇到再看。

  • 相关阅读:
    RestKit ,一个用于更好支持RESTful风格服务器接口的iOS库
    Pop–实现任意iOS对象的任意属性的动态变化
    界面传值失败
    UIImagePickerController
    NSURLSession
    iOS图标尺寸
    cocoapods
    duplicate symbol _OBJC_IVAR
    MAC升级openssl
    Mac
  • 原文地址:https://www.cnblogs.com/ChrisMurphy/p/4877763.html
Copyright © 2020-2023  润新知