springmvc4 相关注解的详细讲解
首先我是一个初学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配置文件漫天飞的年代,它让程序拥有更高的可读性,可配置性与灵活性。给人一种更简洁明了的感觉。
springmvc4 相关注解的详细讲解的更多相关文章
- Spring MVC 3.0 深入及对注解的详细讲解
核心原理 1. 用户发送请求给服务器.url:user.do 2. 服务器收到请求.发现Dispatchservlet可以处理.于是调用DispatchServlet. 3. ...
- Spring MVC 3.0 深入及对注解的详细讲解[转载]
http://blog.csdn.net/jzhf2012/article/details/8463783 核心原理 1. 用户发送请求给服务器.url:user.do 2. ...
- Java堆、栈和常量池以及相关String的详细讲解(经典中的经典) (转)
原文链接 : http://www.cnblogs.com/xiohao/p/4296088.html 一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的 ...
- Java堆、栈和常量池以及相关String的详细讲解
一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...
- Java堆、栈和常量池以及相关String的详细讲解(转)
一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...
- Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解
前言 大家好,给大家带来Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解的概述,希望你们喜欢 Activity是什么 作为一个Activ ...
- SpringMVC_总结_03_SpringMVC相关注解
一.前言 在前面的小节中,我们配置了注解驱动和自动扫描包,然后就可以使用SpringMVC相关注解了. 二.@Controller @Controller用来修饰类,源码如下: package org ...
- Android webservice的用法详细讲解
Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...
- 详细讲解Hadoop源码阅读工程(以hadoop-2.6.0-src.tar.gz和hadoop-2.6.0-cdh5.4.5-src.tar.gz为代表)
首先,说的是,本人到现在为止,已经玩过. 对于,这样的软件,博友,可以去看我博客的相关博文.在此,不一一赘述! Eclipse *版本 Eclipse *下载 Jd ...
随机推荐
- 关于super关键字与继承
super它只是一个限定词,当用super引用时,它也是引用当前对象本身,只是super只是限定了访问当前对象从父类那里继承得到成员变量或方法. import java.util.Date; publ ...
- Acrobat_8_Pro_SC 激活老是提示你输入的授权码无效
假如安装了Adobe Acrobat Professional 8 的时候无法激活, 或在恢复安装时 Adobe Acrobat Professional 8 需要重新激活, 激活的时候,总是提示你输 ...
- First App on Phonegap | Cordova
Phonegap简介 PhoneGap是一能够让你用普通的web技术编写出能够轻松调用api接口和进入应用商店的 html5应用开发平台,是唯一支持7个平台的开源移动框架. 优势: 1.兼容性:多平台 ...
- JAVA篇之环境安装(Windows)
一.JAVA 安装两个重要概念 1.JRE::英文Java Development Kit ,记住英文,深入理解就去看相关文章. 2.JDK:英文 Java Runtime Environment,记 ...
- redis内网无法连接的问题
1.修改redis服务器的配置文件 vi redis.conf 注释以下绑定的主机地址 # bind 127.0.0.1 2.修改redis服务器的参数配置 修改redis的守护进程为no ,不启用 ...
- 两款Mongodb可视化工具
在某些场景下,相比传统的关系数据库和NoSQL数据库,Mongodb拥有不可替代的优势. 例如,最近我需要为收集的大量网站进行分类.实际情况是,一个网站可能同时有多个标签,想象一下新浪网,它既是门户站 ...
- 群晖Synology
简介 群晖是做的最好的一家NAS公司,声称是买软件卖硬件,软件有丰富的功能. 白群晖就是指从正规渠道买软件+硬件,价格昂贵,性价比低. 黑群晖是指自己搭建或购买单独的硬件(可以是电脑主机.可以是其他厂 ...
- flutter 保存图片到本地
f'lutter 图片的保存 分为俩步: 1.开启存储图片权限开启权限需要用到permission_handler pubspec 添加 permission_handler: ^3.0.1下载包就可 ...
- 【OCP-12c】CUUG最新考试原题整理及答案(071-9)
9.(5-5) choose the best answerView the Exhibit and examine the structure of the SALES and STORES tab ...
- [转] 翻译130+VIM基本命令
基础 :e filename 在编辑器中打开一个文件 :w 保存文件 :q 退出vim :q! 退出但不保存 :x 写文件(如果有做修改)并退出 :sav filename 保存为 . 在正常模式中重 ...