SpringMVC---其它常用注解
常用注解
- PathVariable
@RequestMapping注解中使用占位符的情况下,需要使用@PathVariable注解指定占位符参数。即指定占位符中的值与方法中哪一个参数进行匹配。如果方法中的参数名与url中{}里面(占位符)的名字一样,则可以省略@PathVariable(“userId”)中的userId,即@ PathVariable String userId
@RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET) public String getLogin(@PathVariable("userId") String userId){ System.out.println("User Id : " + userId); return "hello"; }
- RequestParam
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter(“name”),另外一种是用注解@RequestParam直接获取。@RequestParam有三个属性value(参数名)、required(是否必须有该参数值)、defaultValue(默认值)。在方法中使用,如果在方法中某参数使用了该注解,那么在请求中必须包含该参数,否则不会触发。在请求中即使没有方法中注解的参数也要触发该方法,则可以通过设置required属性为false即可,默认为true。如果参数是int/boolean数据类型的值,则不传递该参数会报错,因为int/boolean类型不能赋null值。所以建议使用Integer/Boolean
public String getLogin(@RequestParam (value="userId", required=true,defaultValue=0) String userId, @RequestParam String userName){ System.out.println("User Id : " + userId); return "hello"; }
- CookieValue
读取Cookies中的值,并且赋值给变量。有三个属性value、required、defaultValue,用法与RequestParam用法类似
- SessionAttributes
如果需要在多个请求之间共用某个数据,那么我们通常将数据保存在session中。如果希望在多个请求之间共用某个模型属性数据,则可以在控制器类标注一个@SessionAttributes,SpringMVC会将模型中对应的属性暂时保存到HttpSession中。除了使用SessionAttributes,还可以使用request.getSession()来处理session数据。
- ResponseBody
用于将Controller的方法返回的对象,通过适当的HttpMessageConverter(转换器)转换为指定格式后,写入到Response对象的body数据区,一般在返回值不是某页面时使用,如返回json、xml等时使用,使用ResponseBody将会跳过视图处理的部分。HttpMessageConverter接口负责将请求转换成一个对象,并将对象输出为响应信息,spring-mvc.xml配置文件中<mvc:annotation-driven />开启之后,会给AnnotationMethodHandlerAdapter初始化7个转换器,可以通过调用AnnotationMethodHandlerAdapter的getMessageConverts()方法来获取转换器的一个集合 List<HttpMessageConverter>
常用的转换器
ByteArrayHttpMessageConverter | 读写二进制数据 |
StringHttpMessageConverter | 将请求信息转换为字符串 |
ResourceHttpMessageConverter | 读写org.springframework.core.io.Resource对象 |
SourceHttpMessageConverter | 读写javax.xml.transform.Source类型的数据 |
XMLAwareFormHttpMessageConverter | 处理表单中的xml数据 |
Jaxb2RootElementHttpMessageConverter | 通过JAXB2读写XML消息,将请求消息转换到标准XmlRootElement和XmlType的注解类中 |
MappingJacksonHttpMessageConverter | 读写JSON数据 |
后面三个分别用于处理xml数据和json数据,每次请求SpringMVC会从List<HttpMessageConverter>中挑选一个来处理数据,首先获取注册的所有HttpMessageConverter一个集合,然后会从客户的请求header中寻找客户端可接收的类型,比如application/json或者application/xml等组成一个集合,所有的HttpMessageConverter都有canRead()和canWrite()方法,返回的值都是布尔类型,看这个HttpMessageConverter是否支持当前请求的读与写,读对应的RequestBody注解,写对应的ResponseBody注解,RequestBody注解与RequestParam类似,主要用于读取请求的body数据。最后会遍历HttpMessageConverter集合,与前面获取可接受类型进行匹配,如果匹配,那么直接使用当前第一个匹配的HttpMessageConverter,然后return
例子:
在spring-mvc.xml配置
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" >
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"></property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBound">
<list>
<value>com.jikexueyuan.demo.springmvc.model.User</value>
</list>
</property>
</bean>
通过使用ResponseBody注解,根据请求header中Accept参数值的不同,对同一地址请求分别来呈现一个实体的json与xml结果,首先在配置文件中,使用mvc:annotation-driven定义需要用到的messageConverter,这里我们用到了4个转换器,分别用来处理json数据、String数据、xml数据,在定义xml数据处理过程中需要使用Jaxb2Marshaller这个类来定义我们的实体类,比如User类,需要标明xmlRootElement注解
java中converter实现
// 使用ResponseBody注解进行标注,直接返回需要转换的对象
@ResponseBody
@RequestMapping(value="/user/{userid}", method = RequestMethod.GET)
public User queryUser(@PathVariable("userid") long userId) {
User u = new User();
u.setUnick("zhangsan");
u.setLastLoginDateTime(new Date());
return u;
}
- RequestHeader
@RequestHeader注解,可以把Request请求header部分的值绑定到方法的参数上。使用于方法参数上@RequestHeader(“Keep-Alive”) long keepAlive
header数据包括
Host localhost:8080
Accept text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
SpringMVC---其它常用注解的更多相关文章
- (三)SpringMVC之常用注解
SpringMVC的常用注解 注解 说明 @Controller 用于说明这个类是一个控制器 @RequestMapping 用于注释一个控制器类或者控制器类的方法 @RequestParam 用于将 ...
- SpringMVC的常用注解
在SpringMVC中常用的注解主要都是用于Controller上,所以下面的四大不同类型的注解都是根据它们处理的request的不同内容部分来区分的: 处理requ ...
- springMvc之常用注解介绍
@requestbody和@requestparam的用法 获取请求参数的方法 get请求: 直接获取request 如: public String getHtml(HttpServletR ...
- SpringMVC相关常用注解
@Controller: @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象 @RequestMapping: RequestMappin ...
- Spring和SpringMVC的常用注解
Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package=" ...
- springMVC的常用注解有哪些?
1.@Controller @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否 ...
- springmvc:常用注解
一.RequestParam注解 作用: 把请求中指定名称的参数给控制器中的形参赋值. 属性: value:请求参数中的名称. required:请求参数中是否必须提供此参数.默认值:true.表示必 ...
- SpringMVC框架——常用注解
@RequestMapping Spring MVC 通过 @RequestMapping 注解将请求与业务方法进行映射,在方法定义处,在类定义都可以添加该注解. 常用参数: 1.value:指定请求 ...
- SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC常用注解實例詳解2:@ModelAttribute
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
随机推荐
- time和datetime模块
在Python中,通常有这几种方式来表示时间: 1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素. 由于Python的time模块实现主要调用C库,所以各个平台可能有 ...
- bootstrap文件上传fileupload插件
Bootstrap FileInput中文API整理:https://blog.csdn.net/u012526194/article/details/69937741 SpringMVC + boo ...
- 【CSS】等高布局
1. 负margin: margin-bottom:-3000px; padding-bottom:3000px; 再配合父标签的overflow:hidden属性即可实现高度自动相等的效果. ...
- PS:将一个图片变成圆形
1.选择一张正方形图片并放置到PS软件中 2.选择“选框工具”->右击选择"椭圆选框工具” 3.画出你要圆形内的范围: 4.选择两种方式中的一种,实际上效果是一毛一样的 5.右下角就会 ...
- leetcode: 哈希——two-sum,3sum,4sum
1). two-sum Given an array of integers, find two numbers such that they add up to a specific target ...
- 【PHP 基础类库】Prototype 原型版教学文章!
前言 大家好我是:石不易,今天我为大家带来了PHP基础类库原型版的教学文章,至此本人的作品线已分为三大类,分别是:JavaScript前端框架(封装库).PHP模板引擎.以及PHP基础类库.该类库历时 ...
- 进程管理—进程描述符(task_struct)
http://blog.csdn.net/qq_26768741/article/details/54348586 当把一个程序加载到内存当中,此时,这个时候就有了进程,关于进程,有一个相关的叫做进程 ...
- Codeforces 760A Petr and a calendar
题目链接:http://codeforces.com/problemset/problem/760/A 题意:日历需要多少列. #include <bits/stdc++.h> using ...
- 广搜破解密码(HDU1195)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 解题报告: #include<iostream> #include<cstdi ...
- 【转】Android xml资源文件中@、@android:type、@*、?、@+含义和区别
一.@代表引用资源 1.引用自定义资源.格式:@[package:]type/name android:text="@string/hello" 2.引用系统资源.格式:@andr ...