当前位置: 移动技术网 > IT编程>开发语言>Java > spring mvc4中相关注解的详细讲解教程

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

前言

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

1.@controller

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

<?xml version="1.0" encoding="utf-8"?>
<beans 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:context="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">
 <context:component-scan base-package="com.chen" />
</beans>

2.@requestmapping

requestmapping 注解将类似 "/admin"这样的url映射到整个类或特定的处理方法上。一般来说,类级别的注解映射特定的请求路径到表单控制器上,而方法级别的注解只是映射 为一个特定的http方法请求("get","post"等)或http请求参数。

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;

@controller
@requestmapping("admin")
public class logincontroller {
 
 @requestmapping(value = "login" , method = requestmethod.get , consumes = "text/html")
 public string tologinpage(){
 return "/web-inf/jsp/login.jsp";
 }
}

上述url的访问地址应该是:localhost:8080/proj/admin/login.html

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

produces-指定返回的内容类型,仅当request请求头中的(accept)类型中包含该指定类型才返回。

value-指定请求的实际地址,指定的地址可以是uri template 模式

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

     b) 可以指定为含有某变量的一类值(uri template patterns with path variables);

     c) 可以指定为含正则表达式的一类值( uri template patterns with regular expressions);

如下示例:

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;

@controller
public class blogcontroller {
 
 @requestmapping(value = "blog/{nick}/{year:20\\d{2}}/{month:1|1[0-2]}/{day:[12][0-9]|30|[1-9]}" , method = requestmethod.get)
 public string toblogpage(@pathvariable string nick,
  @pathvariable integer year,@pathvariable integer month,@pathvariable integer day){
 return "/web-inf/jsp/blog.jsp";
 }
}

params-指定request中必须包含某些参数值是,才让该方法处理。

headers-指定request中必须包含某些指定的header值,才能让该方法处理请求。

如下示例:

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;

@controller
public class blogcontroller {
 
 //仅处理request的header中包含了指定“refer”请求头和对应值为“http://www.ttyouni.com/”的请求
 @requestmapping(value = "image", headers="referer=http://www.ttyouni.com/" )
 public string getimage(){
 return "/web-inf/jsp/image.jsp";
 }
}

3.@rathvariable

在spring mvc中,可以使用 @pathvariable 注解方法参数并将其绑定到uri模板变量的值上,之前示例中也有相关体现。

4.@requestparam

@requestparam将请求的参数绑定到方法中的参数上。其实即使不配置该参数,注解也会默认使用该参数。如果想自定义指定参数的话,可以将@requestparam的 required 属性设置为false。

5.@requestbody

@requestbody是指方法参数应该被绑定到http请求body上。

6.@sessionattibutes

@sessionattibutes可以通过modelmap对象的put操作设置相关的session同时在attibute对象也会有该对象。

示例如下:

import javax.annotation.resource;
import javax.servlet.http.httpservletrequest;

import org.springframework.stereotype.controller;
import org.springframework.ui.modelmap;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.sessionattributes;

import com.chen.proj.service.userservice;

@controller
@requestmapping("admin")
@sessionattributes("user")
public class logincontroller {
 
 @resource
 userservice service;
 
 @requestmapping(value = "dologin", method = requestmethod.post)
 public string dologin(@requestparam string username , @requestparam string password, httpservletrequest request, 
            modelmap map ){ 
 try {
  user user = service.dologin(username, password); 
  map.put("user", user);
 } catch (exception e) {
  request.setattribute("error", e.getmessage());
  return "/web-inf/jsp/login.jsp";
 } 
 return "/web-inf/jsp/loginsuccess.jsp";
 }
 
}

7.@responsebody

@responsebody与@requestbody类似,它的作用是将返回类型直接输入到http response body中。@responsebody在输出json格式的数据时会用到。

示例如下:

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;

import com.chen.proj.bean.user;

@controller
public class jsoncontroller {

 @responsebody
 @requestmapping("/getjson")
 public user getuserinfo(){
 user user = new user();
 user.setpassword("1234");
 user.setusername("jsontest");
 return user;
 } 
}

8.@restcontroller

 我们经常见到一些控制器实现了rest的api,只为服务于json,xml或其它自定义的类型内容。@restcontroller用来创建rest类型的控制器,与@controller类型。@restcontroller就是这样一种类型,它避免了你重复的写@requestmapping与@responsebody

9.@modelattribute

@modelattribute可以作用在方法或方法参数上,当它作用在方法上时,标明该方法的目的是添加一个或多个模型属性。
当作用在方法参数上时,表明该参数可以在方法模型中检索到。如果该参数不在当前模型中,该参数先被实例化然后添加到模型中。一旦模型中有了该参数,该参数的字段应该填充所有请求参数匹配的名称中。这是spring mvc中重要的数据绑定机制,它省去了单独解析每个表单字段的时间。

import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;

import com.chen.proj.bean.user;

@controller
public class usercontroller {

 @modelattribute
 public user adduser(@requestparam string number) {
 return service.finduser(number);
 }

 @modelattribute
 public void populatemodel(@requestparam string number, model model) {
 model.addattribute(service.finduser(number)); 
 // add more ...
 }
}

注解的出现终结了xml配置文件漫天飞的年代,它让程序拥有更高的可读性,可配置性与灵活性。给人一种更简洁明了的感觉。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网