SpringMVC的注解方式配置
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的注解方式配置的更多相关文章
- 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 ...
- atitit.ajax bp dwr 3.的注解方式配置使用流程总结.....
atitit.ajax bp dwr 3.的注解方式配置使用流程总结..... 1. 下载 dwr.jar 1M 1 2. 配置注解方式..web.xml 1 3. Class 配置 2 4. 测试 ...
- SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- SpringMVC的注解方式
mvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
随机推荐
- netstat 问题处理
MySQL SQL Writing to net Creating sort index StatisticsPreparing
- Struts2国际化——完整实例代码
一.介绍 国际化Internationalization的首末字符I和N之间字符数是18,简称I18N “国际化”是指一个应用程序在运行时,能够根据客户端请求所来自国家或地区语言的不同,而显示不同的语 ...
- 宇宙最帅叉叉——第五周博客 for 测试与发布(Alpha版本)
Alpha版本测试报告 1.在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? a.修复的BUG UDP传输 recvfrom 当没有消息来的时候一直循环等待因其阻塞 ,时间戳无效了. ...
- SharePoint 企业搜索-PowerShell
1. 显示企业搜索服务信息 Get-SPEnterpriseSear1chService 2. 显示企业搜索服务实例 Get-SPEnterpriseSearchServiceInstance 3. ...
- centos7.5 安装 redis-4.0.11
1.下载redis包 wget wget http://download.redis.io/releases/redis-4.0.11.tar.gz 2.解压安装 #解压 .tar.gz #安装 cd ...
- [LeetCode&Python] Problem 867. Transpose Matrix
Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...
- Visual Studio 2015 编译错误 File 的值+乱码的解决方法
======================================== VS2015调试项目时,会报莫名奇妙的错误,如下图所示: 程序编译,提示有错误:Visual Studio 2015 ...
- Flask第31课——include标签
我们在上一节代码基础上增加一些代码,样式: 文件名index.html,代码: {% from 'macros/forms.html' import input %} <!DOCTYPE htm ...
- js代码中碰到的函数
第一个--->字符串的截取substring()方法 substring(a,b)--->[a,b)区间截取字符.下标从0开始.从a下标开始,截取到b下标的前一个字符.返回一个新的字符串 ...
- MHA之Binlog Dump (GTID)僵尸进程清理
master存活的状态下切换 masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_mas ...