[@Controller]1 基于@注释的控制器配置

来源:互联网 发布:体育网络电视 编辑:程序博客网 时间:2024/06/05 21:00

[@Controller]1 基于@注释的控制器配置

 (2012-06-14 15:20:33)
转载
标签: 

spring

 

mvc

 

controller

 

注释

 

控制器

 

it

分类: JavaSpring

基于注释的控制器配置需要Java 5以上的版本支持。这种注释支持servlet MVCPortlet MVC。通过这种方式实现的控制器不需要继承特定的基础类,或实现特定的接口。

 

ADispatcher配置文件

DispatcherServletDispatcherPortlet都默认支持注释配置控制器。以DispatcherServlet为例,它默认支持实现HandlerMapping接口的DefaultAnnotationHandlerMapping和实现HandlerAdapter接口的AnnotationMethodHandlerAdapter来支持注释配置控制器。所有我们无需在Dispatcher配置文件中进行显示配置,就可以支持注释配置控制器。当然我们也可以自定义HandlerMappingHandlerAdapter来支持。

下面是显示定义的例子:

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>

A.1、自动探测注释控制器

在原来的方式中Controller是被显示定义在Dispatcher配置文件中的一个bean。并且这个Controller实例显示实现Controller接口。在注释配置控制器时,这个Controller不再扩展控制器基类或应用Servlet API。注释控制器可以作为一个bean显示的定义在Dispatcher配置文件中,也可以不显示定义让Dispatcher自动探测。

要实现注释控制器自动探测,需要在配置文件中加入探测组件。

自动探测注释控制器的例子

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="org.xxx" />

    <!—将自动探测org.xxx包中有控制器注释的类-->

</beans>

 

B、基于注释的Controller

基于注释的@Controller它不需要在类中显式的实现Controller,但是它需要Spring 2.5+Java 5+的支持。

范例1

Controllers provide access to the application behavior that you typically define through a service

interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.

一个最简单的范例,什么是控制器,控制器它提供了访问程序的行为,我们通常用一个Servlet来实现。控制器它解释用户输入,并把用户输入转换为一个模型,并通过一个视图显示这个模型。

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class HelloWorldController {

    @RequestMapping("/helloWorld")

    public String helloWorld(Model model) {

       model.addAttribute("message", "Hello World!");

       return "helloWorld";

    }

}

@Controller表示HelloWorldController这个类是一个控制器。我们发现这个类并没有像原来那样继承一些Controller的基类或直接实现Controller接口。@RequestMapping("/helloWorld")表示把/helloWorld请求映射到这个类的helloWorld方法上。这个方法接受一个模型Model,并返回一个String类型的视图名,后续可以根据这个视图名,以特定的视图技术显示,这个视图可以访问模型Model数据。





0 0