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. focusSNS学习笔记

    FocusSNS是一个社交类型的网站架构 系统的加载过程 所有的分发都从RouteController开始 @RequestMapping(value={"/", "/h ...

  2. iOS 9 通用链接(Universal Links)

    什么是Universal Links? 在iOS9之前,对于从各种从浏览器.Safari中唤醒APP的需求,我们通常只能使用scheme.但是这种方式需要提前判断系统中是否安装了能够响应此scheme ...

  3. myeclipse10 破解版安装

    安装包请看百度云: step1 step2 step3 step4 step5 step6 安装 svn,可以参考: http://www.cnblogs.com/OnlyCT/p/6061134.h ...

  4. 20165210 Java第四周学习总结

    20165210 Java第四周学习总结 教材学习内容 第五章学习总结 子类与父类: 子类: class 子类名 extends 父类名 { ... } 类的树形结构 子类的继承性: 子类和父类在同一 ...

  5. mysql-5.6.17-win32安装

    下载免安装压缩文件http://dev.mysql.com/downloads/mysql/ 解压到自定义目录,我这里演示的是D:\wamp\mysql\   复制根目录下的my-default.in ...

  6. 求a^b

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求a^b 由于结果可能很大,我们现在只需要知道这个值 mod 1012就可以了(为什么是1012?我的生日) ...

  7. 如何设置鼠标右键单击返回ppt上一页

    点击“powerpoint选项”,选择“高级” 将“幻灯片放映”选项下“鼠标右键单击时显示菜单(E)”前面的钩去掉.图为处理过的.

  8. 大家一起做训练 第一场 G CD

    题目来源:UVA 624 题目的意思就是:我现在需要从 t 张CD中拿出一部分来,尽可能的凑出接近 N 这么久的音乐,但是不能超过 N. CD不超过20张,每张长度不超过 N ,不能重复选. 一个很简 ...

  9. 字符串匹配--扩展KMP模板

    对于一个字符串 s 以及子串 t ,扩展KMP可以用来求 t 与 s 的每个子串的最长公共前缀 ext [ i ],当然,如果有某个 ext 值等于 t 串的长度 lent ,那么就说明从其对应的 i ...

  10. ubuntu编译python源码的坑

    在linux平台下编译python时只需要3条命令. 编译步骤 在github或https://www.python.org/downloads/中下载python源码 解压压缩包进路目录执行 1. ...