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 ...
随机推荐
- java内存解析
ass BirthDate{ private int day; private int month; private int year; public BirthDate(int d;int m,in ...
- HAL编译问题
1 make:进入目录'/opt/FriendlyARM/tiny4412/android/android-4.1.2'make: *** 没有规则可以创建“out/target/product/ge ...
- Sublime 快捷键及使用技巧
(1)打开刚刚关闭的窗口:ctrl+shift+T 默认设置. (2)全屏显示:F11 默认设置. (3)多处选择相同的词:Ctrl+D,回退选择Ctrl+U 默认设置,非常有用,可以试试. (4)取 ...
- js 代码执行时间
<html> <head> </script> <script> var sTime=new Date().getTime(); alert(&qu ...
- [BZOJ4552]排序
Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...
- MySQL日常维护
删除MySQL 账号 use mysql delete from user where user='xiewenming'; 授权账号密码 GRANT SELECT,INSERT,UPDATE,DEL ...
- Spring Boot 中yml配置文件
步骤一:yml格式 现在大家发现,在springboot里还是要用到配置文件的. 除了使用.properties外,springboot还支持 yml格式. 个人觉得yml格式的可读性和..prope ...
- Package Manager Console的使用
Find-Package PM> Find-Package autofac https://docs.microsoft.com/en-us/nuget/tools/ps-ref-find-pa ...
- POJ 1236 Network of School
http://poj.org/problem?id=1236 题意: 给出一个图,至少要选多少个点才能遍历全图和至少需要添加多少边使得整个图是强连通. 思路: 强连通计算连通分量后缩点,计算入度为0的 ...
- GridControl 史上最全的资料(一)
GridControl详解(一)原汁原味的表格展示 Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多半借助Demo和英文帮助 ...