转载 SpringMVC详解(三)------基于注解的入门实例
目录
- 1、在 web.xml 文件中配置前端处理器
- 2、在 springmvc.xml 文件中配置处理器映射器,处理器适配器,视图解析器
- 3、编写 Handler
- 4、编写 视图 index.jsp
- 5、在浏览器中输入:http://localhost:8080/SpringMVC_03/hello
前两篇博客我们讲解了基于XML 的入门实例,以及SpringMVC运行的详细流程。但是我们发现基于 XML 的配置还是比较麻烦的,而且,每个 Handler 类只能有一个方法,在实际开发中肯定是不可能这样来进行开发的。那么这篇博客我们就讲解实际开发中用的最多的基于注解配置的SpringMVC配置。
本篇博客源码下载链接:http://pan.baidu.com/s/1dESLgv3 密码:vkuy
项目结构为:
1、在 web.xml 文件中配置前端处理器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<? xml version="1.0" encoding="UTF-8"?>
< web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
< display-name >SpringMVC_01</ display-name >
<!-- 配置前端控制器DispatcherServlet -->
< servlet >
< servlet-name >springmvc</ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
<!--springmvc.xml 是自己创建的SpringMVC全局配置文件,用contextConfigLocation作为参数名来加载
如果不配置 contextConfigLocation,那么默认加载的是/WEB-INF/servlet名称-servlet.xml,在这里也就是 springmvc-servlet.xml
-->
< init-param >
< param-name >contextConfigLocation</ param-name >
< param-value >classpath:springmvc.xml</ param-value >
</ init-param >
</ servlet >
< servlet-mapping >
< servlet-name >springmvc</ servlet-name >
<!--第一种配置:*.do,还可以写*.action等等,表示以.do结尾的或者以.action结尾的URL都由前端控制器DispatcherServlet来解析
第二种配置:/,所有访问的 URL 都由DispatcherServlet来解析,但是这里最好配置静态文件不由DispatcherServlet来解析
错误配置:/*,注意这里是不能这样配置的,应为如果这样写,最后转发到 jsp 页面的时候,仍然会由DispatcherServlet进行解析,
而这时候会找不到对应的Handler,从而报错!!!
-->
< url-pattern >/</ url-pattern >
</ servlet-mapping >
</ web-app >
|
2、在 springmvc.xml 文件中配置处理器映射器,处理器适配器,视图解析器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<? 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--注解处理器映射器 -->
< bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></ bean >
<!--注解处理器适配器 -->
< bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></ bean >
<!--使用mvc:annotation-driven可以代替上面的映射器和适配器
这里面会默认加载很多参数绑定方法,比如json转换解析器就默认加载,所以优先使用下面的配置
-->
<!-- <mvc:annotation-driven></mvc:annotation-driven> -->
<!--单个配置Handler -->
<!-- <bean class="com.ys.controller.HelloController"></bean> -->
<!--批量配置Handler,指定扫描的包全称 -->
< context:component-scan base-package="com.ys.controller"></ context:component-scan >
<!--配置视图解析器 -->
< bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 返回视图页面的前缀 -->
< property name="prefix" value="/WEB-INF/view/"></ property >
<!-- 返回页面的后缀 -->
< property name="suffix" value=".jsp"></ property >
</ bean >
</ beans >
|
3、编写 Handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.ys.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//使用@Controller注解表示这个类是一个Handler
@Controller
public class HelloController {
//@RequestMapping注解括号里面的表示访问的URL
@RequestMapping("hello")
public ModelAndView hello(){
ModelAndView modelView = new ModelAndView();
//类似于 request.setAttribute()
modelView.addObject("name","张三");
//配置返回的视图名,由于我们在springmvc.xml中配置了前缀和后缀,这里直接写视图名就好
modelView.setViewName("index");
//modelView.setViewName("/WEB-INF/view/index.jsp");
return modelView;
}
}
|
注意@Controller注解和@RequestMapping注解的用法
4、编写 视图 index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< title >Insert title here</ title >
</ head >
< body >
hello:${name}
</ body >
</ html >
|
5、在浏览器中输入:http://localhost:8080/SpringMVC_03/hello
转载于:https://www.cnblogs.com/Jeely/p/10755584.html
转载 SpringMVC详解(三)------基于注解的入门实例相关推荐
- SpringAOP描述及实现_AspectJ详解_基于注解的AOP实现_SpringJdbcTemplate详解
AOP AOP特点: 面向切面编程, 利用AOP对业务逻辑的各个部分进行抽取公共代码, 降低耦合度, 提高代码重用性, 同时提高开发效率. 采取横向抽取, 取代传统纵向继承体系重复性代码 解决事务管理 ...
- springMVC详解以及注解说明
springMVC详解以及注解说明 基于注释(Annotation)的配置有越来越流行的趋势,Spring 2.5 顺应这种趋势,提供了完全基于注释配置 Bean.装配 Bean 的功能,您可以使用基 ...
- Android init.rc文件解析过程详解(三)
Android init.rc文件解析过程详解(三) 三.相关结构体 1.listnode listnode结构体用于建立双向链表,这种结构广泛用于kernel代码中, android源代码中定义了l ...
- P2P技术详解(三):P2P技术之STUN、TURN、ICE详解
本文是<P2P理论详解>系列文章中的第2篇,总目录如下: <P2P技术详解(一):NAT详解--详细原理.P2P简介> <P2P技术详解(二):P2P中的NAT穿越(打洞 ...
- Android Studio 插件开发详解三:翻译插件实战
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78113868 本文出自[赵彦军的博客] 系列目录 Android Gradle使用 ...
- 技术详解:基于人脸识别的 AI 弹幕
--------点击屏幕右侧或者屏幕底部"+订阅",关注我,随时分享机器智能最新行业动态及技术干货---------- 有时候,弹幕比剧情还精彩,那些脑洞大开.观点鲜明的弹幕,可以 ...
- SharePoint2007安装图文详解三:安装SqlServer2005
SharePoint2007 中的很多功能会用到数据库,如分析服务,报表服务等.本文介绍SqlServer2005的安装,数据库的安装很简单,基本上安装默认选项点击下一步即可,需要注意的地方在下面会提 ...
- PackageManagerService启动详解(三)之开始初始化阶段流程分析
PKMS启动详解(三)之BOOT_PROGRESS_PMS_START阶段流程分析 Android PackageManagerService系列博客目录: PKMS启动详解系列博客概要 PKMS ...
- HTTPS协议详解(三):PKI 体系
本文大部分内容摘自:http://www.wosign.com/faq/faq2016-0309-03.htm 尊重知识产权,转载注明Wosign -------------------------- ...
- Unicode编码详解(三):UTF-8编码
Unicode编码详解(三):UTF-8编码 本文为原创文章,转载请注明出处,或注明转载自"黄邦勇帅(原名:黄勇) 本文是对<C++语法详解>一书相关章节的增补,以增强读者对字符 ...
最新文章
- python环境搭建
- 户籍恢复需要体检吗_脑梗死后脚麻能恢复吗?需要多久能恢复呢?
- 白领夫妇白手起家 6年赚得两房两车
- 剖析VC中的文件操作
- some daily
- 在Excel中引用Visual Studio.NET 2008编写的COM组件
- 嵌入式智能国际大会图文直播:探索人工智能的前世今生
- 尚学堂科技_王勇_JAVA视频教程_Hibernate
- EF分组后把查询的字段具体映射到指定类里面的写法
- 随机森林进行特征选择
- iOS C语言~bzero函数、memset函数
- stylelint规则大全
- 三极管分压共射放大电路
- 浅谈计算机网络发展方向,浅谈计算机网络的发展方向
- 投影仪与电视的C位之争,电视会成为下一个被淘汰的电器吗?
- oracle odt.net,ODT .NET 详解之 SqlDataSource 访问 Oracle
- 华为nova8和华为nova8Plus哪个好-华为nova8和华为nova8Plus区别-哪个更值得入手-参数对比
- 大促中为什么需要可视化监控大屏?
- 石墨笔记,幕布和 Effie 哪个更适合公众号主?
- 使用canvas生成一个圆形的图片