[@Controller]1 基于@注释的控制器配置
2012-09-03 11:45
369 查看
基于注释的控制器配置需要Java 5以上的版本支持。这种注释支持servlet MVC和Portlet MVC。通过这种方式实现的控制器不需要继承特定的基础类,或实现特定的接口。
A、Dispatcher配置文件
DispatcherServlet和DispatcherPortlet都默认支持注释配置控制器。以DispatcherServlet为例,它默认支持实现HandlerMapping接口的DefaultAnnotationHandlerMapping和实现HandlerAdapter接口的AnnotationMethodHandlerAdapter来支持注释配置控制器。所有我们无需在Dispatcher配置文件中进行显示配置,就可以支持注释配置控制器。当然我们也可以自定义HandlerMapping和HandlerAdapter来支持。
下面是显示定义的例子:
<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数据。
A、Dispatcher配置文件
DispatcherServlet和DispatcherPortlet都默认支持注释配置控制器。以DispatcherServlet为例,它默认支持实现HandlerMapping接口的DefaultAnnotationHandlerMapping和实现HandlerAdapter接口的AnnotationMethodHandlerAdapter来支持注释配置控制器。所有我们无需在Dispatcher配置文件中进行显示配置,就可以支持注释配置控制器。当然我们也可以自定义HandlerMapping和HandlerAdapter来支持。
下面是显示定义的例子:
<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数据。
相关文章推荐
- [@Controller]1 基于@注释的控制器配置
- [@Controller] 1 基于@注释的控制器配置
- @Controller(1)基于@注释的控制器配置
- 基于注释的AOP的一个配置文件!
- spring-基于注释(Annotation)的配置
- spring2中基于注释的配置方式浅析
- 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)
- spring 基于注解的控制器配置
- Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法
- 基于TarBarController控制器的跳转方法
- spring2中基于注释的配置方式浅析
- spring-基于注释(Annotation)的配置
- 基于RYU控制器(controller)上的simple-switch 的APP做的測试-SDN/OpenFlow
- DWR3基于Spring MVC配置Controller的方法
- 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器
- 基于RYU控制器(controller)上的simple-switch 的APP做的测试-SDN/OpenFlow
- spring-基于注释(Annotation)的配置(转) 4
- 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器
- spring-基于注释(Annotation)的配置(转)----1
- spring-基于注释(Annotation)的配置(转)---2