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 ...
随机推荐
- 20145328 《Java程序设计》第10周学习总结
20145328 <Java程序设计>第10周学习总结 资料学习内容总结 网络编程 13.1 网络概述 网络编程技术是当前一种主流的编程技术,随着联网趋势的逐步增强以及网络应用程序的大量出 ...
- 百度console输出
try{ if(window.console&&window.console.log) { console.log("一张网页,要经历怎样的过程,才能抵达用户面前?\n一位新 ...
- [翻译]CSS模块-未来的编码方式
前言 这是Glen Maddern发布于2015年8月19日的一篇文章,主要是之前翻译的文章<理解CSS模块方法>里提到这篇文章,现在算是顺藤摸瓜跟进来看看. 这里的翻译都是根据我自己的理 ...
- vSphere SDK for Java 示例
示例代码: package com.vmware.event.connect; import java.net.MalformedURLException; import java.net.URL; ...
- [BZOJ1217]消防局的设立
Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来 连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成 ...
- bootstrap && bagging && 决策树 && 随机森林
看了一篇介绍这几个概念的文章,整理一点点笔记在这里,原文链接: https://machinelearningmastery.com/bagging-and-random-forest-ensembl ...
- Understanding and Creating OWIN Middlewares - Part 1
In my previous article, What is OWIN? A Beginners Guide we learned the basics of OWIN and the benefi ...
- 直播P2P技术2-低延迟模型
低延迟模型 由上一篇文章我们知道:网状拓扑虽最大化利用了所有节点的资源却无法降低数据延迟,而树状拓扑尽管数据传输效率高,延迟低,但只利用了少部分节点的带宽资源,不适应高码率的直播P2P网络. 那么如何 ...
- SSH两种验证方式原理
本帖转自 http://www.cnblogs.com/hukey/p/6248468.html SSH验证方式有两种,分别为用户密码认证以及密钥认证. 1.用户密码认证方式 说明: (1) 当客户端 ...
- 值得推荐的10本PHP书籍(转)
值得推荐的10本PHP书籍(转) 一.总结 一句话总结: 二.值得推荐的10本PHP书籍 本篇文章的目的是想较全面地推荐10本PHP书籍,暂不讨论Linux/NGINX/Mysql等其他丛书. 前言 ...