春天mvc4中相关注解的详细讲解教程

  

  

在开始本文之前要说明以下,首先我是一个初学springmvc,抱着去加深印象的目的去整理相关springmvc4的相关注解,同时也希望给需要相关查阅的读者带来帮助,好了,下面话就不多说了,一起来看看详细的介绍吧。

  


  

  

控制器控制器是通过服务接口定义的提供访问应用程序的一种行为,它解释用户的输入,将其转换成一个模型然后将试图呈献给用户.Spring MVC使用@ Controller定义控制器,它还允许自动检测定义在类路径下的组件并自动注册。如想自动检测生效,需在xml头文件下引入spring上下文:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;豆类xmlns=" http://www.springframework.org/schema/beans " xmlns: p=" http://www.springframework.org/schema/p "   xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance "   xmlns:上下文=" http://www.springframework.org/schema/context " xmlns: mvc=" http://www.springframework.org/schema/mvc "   xsi: schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd”比;   & lt;上下文:component-scan基础包=" com。陈”/比;   & lt;/beans>      


  

  

RequestMapping注解将类似“/admin”这样的URL映射到整个类或特定的处理方法上。一般来说,类级别的注解映射特定的请求路径到表单控制器上,而方法级别的注解只是映射为一个特定的HTTP方法请求(“会”、“文章”等)或HTTP请求参数。

        进口org.springframework.stereotype.Controller;   进口org.springframework.web.bind.annotation.RequestMapping;   进口org.springframework.web.bind.annotation.RequestMethod;      @ controller   @RequestMapping (“admin”)   公开课LoginController {      @RequestMapping (value=" https://www.yisu.com/zixun/login "=RequestMethod方法。,消耗=" text/html ")   公共字符串toLoginPage () {   返回“login . jsp/web - inf/jsp/?   }   }      

上述url的访问地址应该是:localhost: 8080/项目/admin/登录。html
  

  

消耗——指定处理请求的提交内容类型- type,例如application/json, text/html。
  

  

生产——指定返回的内容类型,仅当请求请求头中的(接受)类型中包含该指定类型才返回。
  

  

价值——指定请求的实际地址,指定的地址可以是URI模板模式

  

,,,,一)可以指定为普通的具体值;
  

  

,,,,B)可以指定为含有某变量的一类值(URI模板的模式与路径变量),
  

  

,,,,C)可以指定为含正则表达式的一类值(URI模板模式用正则表达式),

  

<>强如下示例:

        进口org.springframework.stereotype.Controller;   进口org.springframework.web.bind.annotation.PathVariable;   进口org.springframework.web.bind.annotation.RequestMapping;   进口org.springframework.web.bind.annotation.RequestMethod;      @ controller   公开课BlogController {      @RequestMapping(值=" https://www.yisu.com/zixun/blog/{尼克}/{20年://d{2}}/{月:1 | 1[0]}/{:[12][0 - 9]30 | |(1 - 9)}”,方法=RequestMethod.GET)   公共字符串toBlogPage (@PathVariable字符串尼克,   @PathVariable整数,@PathVariable整数,@PathVariable整数天){   返回"/web - inf/jsp/blog.jsp”;   }   }      

参数——指定请求中必须包含某些参数值是,才让该方法处理。
  

  

页眉——指定请求中必须包含某些指定的头值,才能让该方法处理请求。

  

<>强如下示例:

        进口org.springframework.stereotype.Controller;   进口org.springframework.web.bind.annotation.PathVariable;   进口org.springframework.web.bind.annotation.RequestMapping;   进口org.springframework.web.bind.annotation.RequestMethod;      @ controller   公开课BlogController {//仅处理请求的头中包含了指定“参考”请求头和对应值为“http://www.ttyouni.com/钡那肭?   @RequestMapping (value=" https://www.yisu.com/zixun/image ",头=巴萍鋈?http://www.ttyouni.com/?   公共字符串getImage () {   返回"/web - inf/jsp/image.jsp”;   }   }

春天mvc4中相关注解的详细讲解教程