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 ...
随机推荐
- flume从log4j收集日志输出到kafka
1. flume安装 (1)下载:wget http://archive.cloudera.com/cdh5/cdh/5/flume-ng-1.6.0-cdh5.7.1.tar.gz (2)解压:ta ...
- 20145328 《Java程序设计》第10周学习总结
20145328 <Java程序设计>第10周学习总结 资料学习内容总结 网络编程 13.1 网络概述 网络编程技术是当前一种主流的编程技术,随着联网趋势的逐步增强以及网络应用程序的大量出 ...
- 201453131《Java程序设计》实验三实验报告
实验三 敏捷开发与XP实践 实验内容 •下载并学会使用git上传代码: •与同学结对,相互下载并更改对方代码,并上传: 实验步骤 下载并用git上传代码: •1.下载并安装好git,在cmd中输入gi ...
- 4.9版本linux内核的ina220电流检测芯片源码在哪里
答:在drivers/hwmon/ina2xx.c中,内核配置项为CONFIG_SENSORS_INA2XX Location: -> Device Drivers -> Hardware ...
- PHP中的_FILE_和_DIR_的区别
<?php$dir = dirname(__FILE__);?>在PHP5.3中,增加了一个新的常量__DIR__,指向当前执行的PHP脚本所在的目录.例如当前执行的PHP文件为 /www ...
- RabbitMQ入门(1)——Hello World
这系列是官网的翻译和一些博客的参考,仅供自己复习使用. 介绍 官网定义: RabbitMQ is the most widely deployed open source message broker ...
- linux怎么上真正的国际互联网
1.安装git yum install -y git 2.执行git clone git@github.com:XX-net/XX-Net.git 3.升级python到python 2.7(妈妈说p ...
- Build hadoop 2.5.2 with Java8
mvn clean package -Pdist,native -DskipTests -Dtar -Dmaven.javadoc.skip=true
- 从git获取项目代码
1.先复制项目的SSH链接备用 2.在你要放置项目的地方git bash here 3.按照以下步骤走: $ git clone YourSSHAddress // clone项目 $ ls // 查 ...
- vue-router之学习笔记
用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js ,我们已经可以通过组合组件来组成应用程序,当你要把 vue-router 添加进来,我们需要做的是,将组件( ...