Spring从2.5版本后开始引入注解,用户可以使用@Controller,@RequestMapping,@RequestParam,@ModelAttribute等类似这样的注解。

@Controller用于标记一个类,使用它标记的类就是一个SpringMVC Controller 对象,即一个控制器类。Spring 使用扫描机制查找应用程序中所有基于注解的控制器类。分发处理器会扫描使用该注解的类的方法,并检测该方法是否使用了@RequestMapping注解,而使用了@RequestMapping注解的方法才是真正处理请求的处理器。为保证Spring能找到控制器,需要完成两件事:

  >>在Spring MVC的配置文件的头文件中引入spring-context

  >>使用<context:component-scan>元素,该元素的功能为:启动包扫描功能,以便注册带有@Controller,@Service,@repository,@Component等注解的的类成为Spring的Bean

  >>base-pavckage 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理

·配置文件如下:

<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.fkit.controller"/>

应该把所有控制器类都放在基本包中,并且指定扫描该包,即org.fkit.controller,而不是指定扫描org.fkit包,以免Spring MVC扫描了无关的包

现在我们演示如何使用@Controller注解

package org.fkit.controller;

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";
} }

HelloWorldController是一个基于@Controller注解的控制器,@RequestMapping注解用于映射一个请求,value = "/helloWorld"表示请求由helloWorld方法来进行处理。helloWorld方法接收一个 org.springframework.ui.Model类型的参数,本例在model中添加了一个名为"message"的字符串对象。该对象可以在返回视图当中通过request对象获取。最后,方法返回一个字符串"hello world"作为视图名称。

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.fkit.controller"/> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix">
<value>/WEB-INF/content/</value>
</property>
<!-- 后缀 -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean> </beans>

由于使用了注解类型,因此不需要再在配置文件中使用XML扫描Bean,<context:component-scan base-package="org.fkit.controller"/>指定需要Spring扫描org.fkit.controller包及其子包下面的所有java文件。

配置视图解析器InternalResourceViewResolver来解析视图,将View呈现给用户 。prefix表示视图的前缀,suffix表示视图的后缀,返回的视图字符串是"helloWorld",经过视图解析器之后,则视图的完整路径为:/WEB-INF/content/helloWorld.jsp。

此处没有配置处理映射器和处理器适配器,当用户没有配置这两项时,Spring会使用默认的处理映射器和处理器适配器处理 请求。

测试URL:http://localhost:8080/ControllerTest/helloWorld

@Controller注解的更多相关文章

  1. 转 使用@Controller注解为什么要配置<mvc:annotation-driven />

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...

  2. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  3. SpringMVC自动扫描@Controller注解的bean

    若要对@Controller注解标注的bean进行自动扫描,必须将<context:component-scan base-package="包路径.controller"/ ...

  4. 使用@Controller注解为什么要配置<mvc:annotation-driven />

    自己看了官方文档,也到网上查了下,目前理解如下: <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和Annotat ...

  5. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  6. @RestController和@Controller注解的区别

    @RestController是@ResponseBody和@Controller注解的结合,当你return一个页面时,使用@Controller注解返回的是相应页面,使用@RestControll ...

  7. @Component注解、@Service注解、@Repository注解、@Controller注解区别

    --------------------------------------------------------------------------------------------------- ...

  8. springboot中的controller注解没有生效

    springboot中的controller注解没有生效  , 启动的Application类没有在controller的父目录或同级目录

  9. 使用STS加入controller注解后编写程序无法自动提示

    1.加入@Controller注解后编写程序无法自动提示,去掉了@Controller注解后就可以了! ​ ​ 2.解决方案:将@Controller替换为@RestController后,可以完美的 ...

随机推荐

  1. java实现 比较两个文本相似度-- java 中文版 simHash 实现 ,

    比较两个文本的相似度 这里采用 simHash 算法 ; 分词是 基于 http://hanlp.linrunsoft.com/ 的开源 中文分词包 来实现分词 ; 实现效果图: 直接上源码: htt ...

  2. TagHelper+Layui封装组件之Radio单选框

    TagHelper+Layui封装组件之Radio单选框 标签名称:cl-radio 标签属性: asp-for:绑定的字段,必须指定 asp-items:绑定单选项 类型为:IEnumerable& ...

  3. Codeforces Round #332 (Div. 2)_B. Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. 大区间素数筛选(POJ 2689)

    /* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...

  5. JS 监听浏览器各个标签间的切换

    以前看到过一些网页,在标签切换到其它地址时,网页上的标题上会发生变化,一直不知道这个是怎么做的,最近查了一些资料才发现有一个 visibilitychange 事件就可以搞定,这里将介绍一下页面可见性 ...

  6. vue vue-style-loader !css-loader错误

    最近在学习vue框架,使用webpack打包vue项目,在执行npm run start的时候 出现如下错误: This dependency was not found: * !!vue-style ...

  7. spring的applicationContext.xml没有自动提示(使用本地的文档)

    http://www.springframework.org/schema/beans/spring-beans.xsd Window>>preference>>搜索xml(X ...

  8. Sqoop介绍

    Sqoop介绍 http://sqoop.apache.org http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html   1.什么是Sqoop? ...

  9. ngRx 官方示例分析 - 1. 介绍

    ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book  API  来查询图书,并保存自己的精选书籍列表. 菜单有两 ...

  10. 【开发技术】storyboard和nib的差别

    在使用Storyboard管理的iOS应用中,它的组成部分为AppDelegate和ViewController这两个类以及MainStoryboard.storyboard文件组成.Storyboa ...