SpringMVC札集(03)——基于注解的SpringMVC入门完整详细示例

来源:互联网 发布:地瓜网络嗅探器 编辑:程序博客网 时间:2024/06/15 18:01

自定义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;@Controllerpublic 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