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 ...
随机推荐
- C#正则表达式获取网址的域名(IP)
代码如下: string p = @"(http|https)://(?<domain>[^(:|/]*)"; Regex reg = new Regex(p, Reg ...
- redhat7.3忘记root密码后如何重置root密码
redhat7系如果忘记root密码,重置密码方法与redhat6系不同! 1.开机启动系统,在grub选择启动内核项时 按‘e’进入编辑模式 2.这时看到的参数并不全,要按上下键滚动显示, 3.在l ...
- Vim-命令合集
命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...
- 编译安装 mysql 5.5,运行 cmake报错Curses library not found
是因为 curses库没有安装,执行下面的语句即可 yum -y install ncurses-devel 如果上述命令的结果是no package,则使用下面的命令安装 apt-get insta ...
- redis redis的连接
昨天2017年12月26日,我刚刚从网上下载了redis.经过一天的摸索,踩了不少坑.昨天下午,比较磕磕巴巴,今天早上 终于比较完善地完成了一次小操作. 使用cmd的重要步骤 1.输入redis-se ...
- Spring Boot的Maven插件Spring Boot Maven plugin详解
Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...
- centos7安装python3和ipython
CentOS7下默认系统自带python2.X的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装 ...
- MySQL 中while loop repeat 的基本用法
-- MySQL中的三中循环 while . loop .repeat 求 1-n 的和 -- 第一种 while 循环 -- 求 1-n 的和 /* while循环语法: while 条件 DO 循 ...
- P3391 【模板】文艺平衡树(Splay)
Splay #include<cstdio> #include<algorithm> #include<iostream> using namespace std; ...
- 二、OC的构造方法和descriprtion方法
二.构造方法和description方法 1.构造方法的定义 - (id)initWithAge:(int)newAge andNo:(int)newNo; 2.实现构造方法 - (id)initWi ...