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. JDBC事务控制管理(转载)

    JDBC事务控制管理 转载于 2018年01月26日 15:46:11 1.事务 (1)事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐, ...

  2. try catch finally return运行顺序

    首先让我们搞懂两组概念:try catch finally和return 1.try catch finally 首先说try catch, (1)try语句 ,try语句用来包围可能出现异常的代码片 ...

  3. apache中的RewriteCond、RewriteRule

    Rewirte主要的功能就是实现URL的跳转.可基于服务器级的(httpd.conf)和目录级的(.htaccess) 两种方式.如果要想用到rewrite模块,必须先安装或加载rewrite模块. ...

  4. SSH项目搭建(三)——Maven多模块搭建项目

    多模块开发,大致的思想就是把一个项目按某种方式分成多个模块,再把模块们连接成一个整体,我们在开发的时候,可以很清晰的操作每一个模块,可以大大提高开发的效率. Java web项目,最常见的就是按代码的 ...

  5. JQuery - Tab Control

    http://jqueryui.com/tabs/ <!doctype html> <html lang="en"> <head> <me ...

  6. 【c++基础】多个txt文件合并到一个文件的几种方式

    参考 1.windows命令: 2.linux-command; 完

  7. chapter02 三种决策树模型:单一决策树、随机森林、GBDT(梯度提升决策树) 预测泰坦尼克号乘客生还情况

    单一标准的决策树:会根每维特征对预测结果的影响程度进行排序,进而决定不同特征从上至下构建分类节点的顺序.Random Forest Classifier:使用相同的训练样本同时搭建多个独立的分类模型, ...

  8. PC端体验效果最佳epub阅读器——iRead爱读书

    官网:http://www.ireadhome.com/

  9. Spring MVC 学习)——控制器与@RequestMapping详解

    Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解 一.控制器定义 控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现. 控制器解析用户的请求 ...

  10. Loj 2536 解锁屏幕

    Loj 2536 解锁屏幕 状态比较显然的状压 \(dp\) ,设 \(f[S][i]\) 表示连接 \(S\) 集合中的点,最后到的点是 \(i\) 的方案数. 转移时,枚举一个 \(j\notin ...