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. python关于时间的计算,time模块

    import time, datetime # time.time 返回当前时间的时间戳(1970纪元后经过的浮点秒数)(格林尼治时间). # 1分钟60秒,1小时3600秒,1天86400秒. pr ...

  2. ORA-01034:Oracle not available

    ORA-01034:Oracle not available 问题描述:ora-01034常与ora-27101同时出现,都是在登录数据库的时候报该错误 错误原因:出现ORA-01034和ORA-27 ...

  3. SharePoint Foundation 搜索-PowerShell

    1. 显示搜索服务信息 Get-SPSearchService 2. 显示搜索服务实例 Get-SPSearchServiceInstance 3. 获取指定搜索服务实例 $ssInstance = ...

  4. OracleParameter.UdtTypeName的值必须是全大写!

    不然找不到Type. 垃圾Oracle浪费哥大半天时间.

  5. iOS-UIImage变为NSData并进行压缩

    <iOS>UIImage变为NSData并进行压缩   //sdk中提供了方法可以直接调用 UIImage *img = [UIImage imageNamed:@"some.p ...

  6. HDU 3488

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 原来写过的一道题,今天重新看费用流又做了一遍 题意:给一个图,求环的并(权值和最小) 思路:每个点只能走一 ...

  7. XDocument 使用

    摘要: 正文: 1.引入XDocument的命名空间 using System.Xml.Linq; 2. List<CourseItem> to XML doc //List<Cou ...

  8. Redis的持久化策略

    Redis 持久化: 提供了多种不同级别的持久化方式:一种是RDB,另一种是AOF. RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AO ...

  9. 如何用移动硬盘安装win7 系统

    身边没有U盘和光盘,就只有一个移动硬盘.移动硬盘安装系统是怎么进行的.在这里小毛孩来给大家上一课. 前期准备: 1.移动硬盘. 2.win7 32位的操作系统(*.iso). 3.有系统且可开机的电脑 ...

  10. URAL - 1397:Points Game (博弈,贪心)

    Two students are playing the following game. There are 2· n points on the plane, given with their co ...