•  

      http://www.blogbus.com/wanping-logs/235898637.html

      使用注解的原因

      最方便的还是启用注解

      注解方便,而且项目中很流行。

      配置文件尽量减少,主要使用注解方式。

      Springmvc的注解是在2.5版本后有了注解,如何开启注解?

      修改springmvc配置文件

      Web.xml文件中不需要修改,只修改springmvc配置文件

      新建一个springmvc的配置文件,取名为springAnnotation-servlet.xml

      删除掉之前文件中的bean和多方法的配置,springAnnotation-servlet.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:p="http://www.springframework.org/schema/p"

      xmlns:mvc="http://www.springframework.org/schema/mvc"

      xmlns:context="http://www.springframework.org/schema/context"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

       
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

                  http://www.springframework.org/schema/context

             
      http://www.springframework.org/schema/context/spring-context-3.0.xsd 

                  http://www.springframework.org/schema/mvc

                 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
      >

      <!-- 静态资源访问
      -->

      <mvc:resources location="/img/"
      mapping="/img/**"/>

      <mvc:resources
      location="/js/" mapping="/js/**"/>

      <bean
      id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <property name="prefix"
      value="/"></property>

      <property
      name="suffix"
      value=".jsp"></property>

      </bean>

      </beans>

      配置分两步

      1)配置扫描包

      这个配置文件的书写需要一个扫描包。

      在springAnnotation-servlet.xml中配置

      <context:component-scan
      base-package="com.tgb.web.controller.annotation"></context:component-scan>

      这个配置的意思是:

      Spring再启动的时候,会默认扫描自动扫描包下的所有的类,为每个注解分配一个mapping。

      上面的配置是,在启动的时候会扫描com.tgb.web.controller.annotation下所有的包

      2)配置spring注解的两个bean

      <bean
      class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>

      <bean
      class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>

      这两个bean的功能

      1)AnnotationMethodHandlerAdapter

      是方法映射的,不同方法有不同url请求,根基类找方法。

      2)DefaultAnnotationHandlerMapping

      DefaultAnnotationHandlerMapping根据扫描的包下面找类,即通过url找类

      注意:

      一定要把包找对,我就犯过错,两个包都在org.springframework.web.portlet目录下找的类,我用的是两个包分别是

      org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter

      org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping

      其实应该是org.springframework.web.servlet目录下找类。

      两个包分别是:

      org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

      org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

      修改web.xml中的springmvc的配置文件

      修改web.xml文件,使其使用新建的springmvc配置文件springAnnotation-servlet.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:p="http://www.springframework.org/schema/p"

      xmlns:mvc="http://www.springframework.org/schema/mvc"

      xmlns:context="http://www.springframework.org/schema/context"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context-3.0.xsd

      http://www.springframework.org/schema/mvc

      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

      <!-- 注解扫描包 -->

      <context:component-scan base-package="com.tgb.web.controller.annotation"
      />

      <!-- 开启注解 -->

      <!--  bean
      class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
      -->

      <bean
      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>

      <bean
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>

      <!-- 静态资源访问 -->

      <mvc:resources location="/img/" mapping="/img/**"/>

      <mvc:resources location="/js/" mapping="/js/**"/>

      <bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <property name="prefix" value="/"></property>

      <property name="suffix" value=".jsp"></property>

      </bean>

      </beans>

      新建一个类(userController)来实现注解

      新建包

      新建一个包,下面的controller都使用注解

      新建controller的包,在这个包下的controller都使用配置文件。

      选择com.tgb.web.controller,邮件点击new-other-package,填新建的包名为annotation。

      新建类userController(GET请求)

      注解打开后,看看注解如何使用。

      userController类实现如下:

      package
      com.tgb.web.controller.annotation;

      import
      org.springframework.stereotype.Controller;

      import
      org.springframework.web.bind.annotation.RequestMapping;

      import
      org.springframework.web.bind.annotation.RequestMethod;

      import
      org.springframework.web.servlet.ModelAndView;

      @Controller

      public
      class UserController {

      @RequestMapping(value="/user/addUser",method=RequestMethod.GET)

      public ModelAndView adduser(){

      String result = "this is adduser------";

      return new
      ModelAndView("/annotation","result",result);

      }

      @RequestMapping(value="/user/delUser",method=RequestMethod.GET)

      public ModelAndView delUser(){

      String result = "this is delUser------";

      return new
      ModelAndView("/annotation","result",result);

      }

      }

      Spring常用注解:

      <!--[if !supportLists]-->1.      
      <!--[endif]-->类的注解

      类注解用到了@Controller

      @Controller表明下面是个Controller类

      org.springframework.stereotype.Controller

      @Component
      @Target(value={TYPE})
      @Retention(value=RUNTIME)
      @Documented

      <!--[if !supportLists]-->2.      
      <!--[endif]-->方法注解

      方法注解用到了@RequestMapping

      @RequestMapping表明下面的方法是个Controller的方法。

      org.springframework.web.bind.annotation.RequestMapping

      @Mapping
      @Target(value={METHOD, TYPE})
      @Retention(value=RUNTIME)
      由上面的信息可以看出,注解类RequestMapping的返回值是Mapping,方法参数有2个,第一个是Method,第二个是Type,即Method是请求的方法,Type是请求的类似是get还是post。

      @RequestMapping(value="/user/delUser",method=RequestMethod.GET)

      新建一个view——annotation.jsp

      新建一个view来实现userController的效果

      <%@ page language="java" contentType="text/html;
      charset=UTF-8"

      pageEncoding="UTF-8"%>

      <%@ taglib prefix="c"
      uri="http://java.sun.com/jsp/jstl/core"%>

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">

      <html>

      <head>

      <script type="text/javascript"
      src="../js/jquery-1.7.min.js"></script>

Springmvc注解启用的更多相关文章

  1. springMVC注解启用及优化

    使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用注解方式. Springmvc的注解是在2.5版本后有了注解,如何开启注解配置文件 Web.xml文件中不 ...

  2. 6.SpringMVC注解启用

    SpringMVC注解可以帮助我们快速地注入 属性和参数 提高开发效率. 由于 有相当一部分人讨厌xml配置方式 注解可以覆盖 xml则不能 使用注解比xml规范化,因为很多注解都是java的规范的范 ...

  3. springMVC(注解版笔记)

    springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...

  4. springMVC 注解版

    http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...

  5. SpringMVC注释启用

    这篇文章是我学习的网络视频SpringMVC写的过程. 谢谢公布各位前辈的视频 以下评论SpringMVC几个关键步骤,注意事项启用: 首先需要加载配置文件(假设请使用自定义路径) <? xml ...

  6. SpringMVC注解HelloWorld

    今天整理一下SpringMVC注解 欢迎拍砖 @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是 ...

  7. SpringInAction--Spring Web应用之SpringMvc 注解配置

    Spring MVC 是当前Web服务器中常用的结构,今天就来学习这相关的知识,首先上图——Spring请求的时候所经历的坎坷之路: (书上原话,算是解释..) 在请求离开浏览器时① ,会带有用户所请 ...

  8. springMVC注解初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  9. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

随机推荐

  1. redis配置不当可导致服务器被控制

    服务器配置不当包括三个部分:1.Redis服务使用ROOT账号启动2.Redis服务无密码认证或者使用的是弱口令进行认证3.服务器开放了SSH服务,而且允许使用密钥登录 简单的写下过程 测试环境vic ...

  2. vscode - 选中多行操作

    选中Alt+鼠标左键即可,按ESC退出 ,具体演示效果如下

  3. S4:装饰模式 Decorator

    动态的给一个对象添加额外的一些职责,就增加功能而言,比继承更具灵活性. 如果仅有一个ConcreateComponent,也可以让Decorator继承ConcreateComponent来实现装饰功 ...

  4. Eclipse 使用 SVN 插件后改动用户方法汇总

    判定 SVN 插件是哪个 JavaH 的处理方法 SVNKit 的处理方法 工具自带改动功能 删除缓存的秘钥文件 其他发表地点 判定 SVN 插件是哪个 常见的 Eclipse SVN 插件我知道的一 ...

  5. oc 把view添加到rootcontrollerview控制的view

    在当前活跃的window 添加一个view [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] add ...

  6. C#中FileStream和StreamWriter/StreamReader的区别

    首先致谢!转自:http://blog.sina.com.cn/s/blog_67299aec0100snk4.html   本篇可能不是非常深入,但是胜在清晰明了   FileStream对象表示在 ...

  7. atititi.soa  微服务 区别 联系 优缺点.doc

    atititi.soa  微服务 区别 联系 优缺点.doc 1. 应用微服务的动机,跟传统巨石应用的比较1 2. 面向服务架构(SOA)  esb2 3. 微服务架构(Microservices)2 ...

  8. scala 遇到过的问题

    1:在我安装完scala的插件后,在打开方法的实现类(open implementactions)的时候,抛出这个异常,后来发现这个异常是因为我的scala的插件跟我eclipse版本不兼容导致的. ...

  9. 使用Office 365前,企业必须要知道的10件事

    目前的市场上充斥着很多关于微软Office 365的炒作,相信厂商.客户或者企业的都有自己不同的考虑.Office 365是微软云版本的Office,用户可以通过互联网创建一个帐户,付款.下载应用安装 ...

  10. CSS3 - 鼠标移入移出时改变样式

    1,使用伪类实现样式切换伪类是CSS2.1时出现的新特性,让许多原本需要JavaScript才能做出来的效果使用CSS就能实现.比如实现下面的鼠标悬停效果,只要为:hover伪类应用一组新样式即可.当 ...