SpringMVC札集(03)——基于注解的SpringMVC入门完整详细示例
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
嗯哼,上次写了个基于xml配置的SpringMVC的HelloWorld。今天来实现基于注解的SpringMVC的HelloWorld。总体思路和之前是一样的,只不过实现的方式不同。所以,在本篇博客中,非常细节的东西就不再重复;但是要注意的地方我会着重强调的。
环境搭建
在此,介绍基于注解的SpringMVC的开发环境
添加jar包
jar包和之前一样的,但是有一点请务必注意:在利用注解进行SpringMVC开发时,假若采用spring4.X那么请使用JDK1.7;假若采用spring3.X,则使用JDK1.6即可;否则极易报错。
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
和之前一样,在web.xml中配置DispatcherServlet
配置springmvc.xml
<?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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置自动扫描 -->
<context:component-scan base-package="cn.com"></context:component-scan>
<!-- 配置注解开发所需的处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解开发所需的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
- 配置springmvc的自动扫描,使得spring框架会自动处理指定包下的注解。在给类加上spring组件注解后,spring的扫描器就可实现bean的自动载入。
- 配置注解开发所需的处理器映射器xxx.annotation.RequestMappingHandlerMapping
- 配置注解方法所需的处理器适配器xxx.annotation.RequestMappingHandlerAdapter
- 关于处理器映射器和处理器适配器这两者的配置可用非常简单的一句
<mvc:annotation-driven />替代 - 配置视图解析器InternalResourceViewResolver
实现Controller
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年7月28日 上午9:58:56
* @info 描述信息:SpringMVC注解开发方式的入门示例
*/
package cn.com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AnnotationController {
@RequestMapping("hello")
public String helloSpringMVCAnnotation(){
return "test";
}
}
在Controller上加上注解@Controller,在方法上加上注解@RequestMapping(“hello”)。这相当于在springmvc.xml中利用bean配置了该Controller,当我们在浏览器中输入http://xxx.ooo.xxx/hello.do时就会调用到helloSpringMVCAnnotation(),该方法返回字符串test为逻辑视图,视图解析器会将其转换为物理视图返回至浏览器。其实,这和我们之前在xml中配置Controller非常类似:
<bean id="myController" name="/welcome.do" class="cn.com.MyController"></bean>
除此以外,还可以利用注解@RequestMapping的其他方式的写法,并指定请求方式
@RequestMapping(value="hello")
@RequestMapping(value="/hello")
@RequestMapping(value="/hello.do")
@RequestMapping(value="hello",method=RequestMethod.GET)
@RequestMapping(value="/hello",method=RequestMethod.GET)
@RequestMapping(value="/hello.do",method=RequestMethod.GET)
部署
浏览器中输入:http://localhost:8081/SpringMVC03/hello.do后回车即可
现在,我们来考虑这么一种情况:在模块A中(比如用户模块)有个方法叫helloSpringMVCAnnotation()巧合的是在模块B中(比如商品模块)也有一个方法叫helloSpringMVCAnnotation();这个时候该如何区分呢?此时,我们可用@RequestMapping()为Controller指定模块名称(相当于给某个方法添加了访问的父路径),比如刚才的示例,可以写为:
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年7月28日 上午9:58:56
* @info 描述信息:SpringMVC注解开发方式的入门示例
*/
package cn.com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class AnnotationController {
@RequestMapping("hello")
public String helloSpringMVCAnnotation(){
return "test";
}
}
利用@RequestMapping(“/user”)区分了模块,故访问的url相应的修改为:http://localhost:8081/SpringMVC03/user/hello.do
SpringMVC札集(03)——基于注解的SpringMVC入门完整详细示例的更多相关文章
- SpringMVC学习总结(四)——基于注解的SpringMVC简单介绍
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet负责转发每一个Request ...
- SpringMVC札集(01)——SpringMVC入门完整详细示例(上)
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(02)——SpringMVC入门完整详细示例(下)
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- 基于注解的springmvc开发
原理简析 1. 背景知识:org.springframework.web.ServletContainerInitializer接口 在基于注解的servlet开发中,ServletContainer ...
- 基于注解的SpringMVC简单介绍
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请 ...
- 【转载】基于注解的SpringMVC简单介绍
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请 ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- Spring基于注解及SpringMVC
1.使用注解 (1)组件扫描 指定一个包路径,Spring会自动扫描该包 及其子包所有组件类,当发现组件类定义前有 特定的注解标记时,就将该组件纳入到Spring 容器.等价于原有XML配置中的< ...
- SpringMVC札集(09)——拦截器
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
随机推荐
- idea+maven本地仓库更新问题
在POM文件中将dependecy填好,.m2/repository/文件夹下也有相应的包下载了.但idea External Libraries就是引用不到这个包. 怎么mvn clean inst ...
- uboot dm9000驱动故障
手头有一块6410开发板,已经有别人提供的uboot代码(基于2011.06),但是在检测dm9000时显示下面的输出: Net: No ethernet found. 当然其他网络命令例如ping等 ...
- HTML中table的td宽度无法固定问题
设置了 width="10%" 依然会被内容撑大, 加了 style="word-break:break-all;" 属性就好了.效果是内容自动回车. 此属性不 ...
- 监控控制台是否运行的bat
@echo offrem set secs=5set srvname="TRS.Export.Scheduler.exe" echo.echo ================== ...
- Java Collections Framework 汇总
1. Java Collections Framework Java集合框架概览 2. Java Collections Framework 之 RandomAccess接口 3. 关于ArrayLi ...
- 常见HTTP状态(304,)
一.1XX(临时响应) 表示临时响应并需要请求者继续执行操作的状态码. 100(继续) 请求者应当继续提出请求.服务器返回此代码表示:已经收到请求的第一部分,正在等待其余部分. 101(切换协议) 请 ...
- CSS 左边div固定,右边div自适应
有时候我们会有这样的需求,如图,aside是导航或者某些链接,右边的main是显示重要的内容,左边需要定宽,右边的main能够自适应剩余的宽度: <!DOCTYPE html PUBLIC &q ...
- exit()子程序终止函数与return()函数的差别
在main函数中我们通常使用return (0);这样的方式返回一个值. 但这是限定在非void情况下的也就是void main()这样的形式. exit()通常是用在子程序中用来终结程序用的,使用后 ...
- 分享海量 iOS 及 Mac 开源项目和学习资料
UI 下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITable ...
- AngularJS 教程 - CodePreject
http://www.codeproject.com/Articles/1065838/AngularJS-Tutorial Article Series Tutorial 1: Angular JS ...