SpringMVC支持使用注解方式配置,比配置文件方式更加灵活易用,是SpringMVC使用的主流模式。

1.在配置文件中开启SpringMVC的注解

    <!-- 开启包扫描 -->
<context:component-scan base-package="cn.tedu.springmvc.controller"></context:component-scan> <!-- 开启SpringMVC的注解形式 -->
<mvc:annotation-driven/>
<?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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 开启包扫描 -->
<context:component-scan base-package="cn.tedu.springmvc.controller"></context:component-scan> <!-- 开启SpringMVC的注解形式 -->
<mvc:annotation-driven/> <!-- 配置处理器映射器的路径和控制器的映射关系 -->
<!-- <bean name="/hello.action" class="cn.tedu.springmvc.controller.Hello"></bean> --> <!-- 配置视图解析器中 视图名称 和 真正视图页面的映射关系 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

xml

2.使用注解开发Controller

package cn.tedu.springmvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; // 使当前类被Spring管理
@Controller
public class Hello {
// 将当前方法注册为一个Controller,
// 当访问指定路径时,当前类的方法执行处理
@RequestMapping("/hello.action")
public String hello(ModelMap model){
model.addAttribute("msg", "hello springMvc,hello World~");
return "hello";
}
// 一个Controller可以配置多个Controller方法,
// 指定不用的路径,访问不同路径执行不同方法
@RequestMapping("/hello2.action")
public String hello2(Model model){
model.addAttribute("msg", "hello springMvc2,hello World2~");
return "hello";
}
}

3.发布应用,通过浏览器进行访问

4.SpringMVC注解方式工作原理

(1)当服务器启动时,会加载web.xml,之后通过引入核心配置文件加载springmvc-servlet.xml就会 解析该xml配置文件。

(2)当解析到包扫描时,根据指定的包将含有@Controller注解的类,=围棋创建对象。

(3)当解析到<mvc:annotation-driven/>开始匹配MVC注解。

(4)之后解析@RequestMapping("/hello.action")之后会将value值进行保存,将方法进行绑定。

(5)当用户发出请求时,处理器映射器就是匹配用户的url和保存号的value值,如果匹配成功,则会执行方法;如果匹配不成功,则报404。

SpringMVC的注解方式配置的更多相关文章

  1. atitit.ajax bp dwr 3.的注解方式配置使用流程总结 VO9o.....

    atitit.ajax bp dwr 3.的注解方式配置使用流程总结 VO9o..... 1. 安装配置 1 1.1. 下载  dwr.jar 1M 1 1.2. 配置注解方式..web.xml 1 ...

  2. atitit.ajax bp dwr 3.的注解方式配置使用流程总结.....

    atitit.ajax bp dwr 3.的注解方式配置使用流程总结..... 1. 下载  dwr.jar 1M 1 2. 配置注解方式..web.xml 1 3. Class 配置 2 4. 测试 ...

  3. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  4. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  5. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  6. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

  7. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

  8. SpringMVC的注解方式

    mvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  9. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

随机推荐

  1. fluent nhibernate映射的数值类型问题

    fluent nhibernate中,数值类型设置不当,就可能会引发一些意想不到错误. 一.引发映射错误 比如,oracle数据库中,字段ID类型是number,结果用codesmith生成代码,实体 ...

  2. C高级第一次PTA作业 要求三

    要求一.要求二 内容链接:http://www.cnblogs.com/X-JY/p/8550457.html 一.PTA作业中的知识点总结 1.6-1 计算两数的和与差(10 分) (1)*在程序中 ...

  3. 如何从github上clone项目源码-linux

    前言 github是目前较为流行的代码托管网站,linux系统是目前开发人员较为常用的操作系统.项目实现的过程中用到一些经典好用的源代码,可以从github上clone,本文主要介绍linux系统命令 ...

  4. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

  5. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  6. (研)for循环的一个bug以及3个while循环的快排

    在这个for循环中,只要有一次不满足,这个for循环将break掉 while(p->score>=90&&i<5) count++ //若有一次不满足的话,那么整个 ...

  7. xsl -fo 了解

    XSL-FO是用于格式化XML数据的语言,全称为Extensible Stylesheet Language Formatting Objects(格式化对象的可扩展样式表语言),是W3C参考标准,现 ...

  8. Mysql ON子句和USING子句

    Mysql ON子句和USING子句   Mysql 中联接SQL语句中,ON子句的语法格式为:table1.column_name = table2.column_name. 当模式设计对联接表的列 ...

  9. Linux下编译安装nginx并且监控

    一.安装Nginx 使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 安装make: yum -y install gcc automake ...

  10. C语言面试题3

    编程题 1.读文件file1.txt的内容(例如): 123456 输出到file2.txt: 563412 #include <stdio.h> #include <stdlib. ...