- 浏览: 318453 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (186)
- nginx/apache (17)
- mysql / mycat 数据库 (13)
- tomcat/jdk部署相关 (15)
- java/开源框架 (28)
- spring(cloud)集成应用 (12)
- docker/kubernetes (9)
- centos/linux/ubuntu相关 (26)
- Rubygem/ruby/rails (1)
- dubbox/微服务相关 (1)
- 移动(前)端 (1)
- 开发(版本)环境工具 (23)
- 消息中间件 (3)
- Nosql (15)
- 全文检索/ELK (9)
- 牛人博客杂烩 (6)
- 文件系统相关 (5)
- 前端开发 (6)
- go体系相关 (4)
- 运维中间件配置安装 (4)
- 大数据/python (0)
最新评论
-
bo_hai:
感谢。总结的真好。
mongodb 配置
@Controller
标注在Bean的类定义处
@RequestMapping
真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping 这个注解
@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;
还可以标注在方法签名处,以便进一步对请求进行分流
配套的属性有:
value 需要跳转的地址
method 基于RestFul的跳转参数,有RequestMethod.get post put delete等
params 符合某个参数的时候才调用该方法
Headers 符合头信息的时候才调用
@SessionAttributes
将结果放入session内
@ModelAttribute
存储在响应内容ModelMap或者ModelAndView进行保存值传到前台,当如果你需要保存值比较少
的时候可以采用这种方式进行保存值并且保存到前台显示
在默认情况下,ModelMap 中的属性作用域是 request 级别,相当于HttpServletRequest中的request.setAttribute() 一样, 在 JSP 视图页面中通过 request.getAttribute(“attribute name”) 或者通过
${ attribute name } EL 表达式访问模型对象中的 属性对象
如果希望在ModelMap 的作用域范围为 session,可以有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现 如:
@Controller
@RequestMapping("/login.do")
@SessionAttributes("currUser")
public class BbtForumController {。。。。。}
@ResponseBody
标注后 返回String对象的结果为response内容体,不标注的话 作为dispatcher url使用
@PathVariable
允许将请求路径的制定内容当做求情的参数使用
返回类型
请求处理方法入参的可选类型 说明
void 此时逻辑视图名由请求处理方法对应的 URL 确定,如以下的方法:
@RequestMapping("/welcome.do")
public void welcomeHandler() {
}
对应的逻辑视图名为“welcome”
String 此时逻辑视图名为返回的字符,如以下的方法:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("ownerId") int ownerId, ModelMap model) {
Owner owner = this.clinic.loadOwner(ownerId);
model.addAttribute(owner);
return "ownerForm";
}
对应的逻辑视图名为“ownerForm”
ModelMap 和返回类型为 void 一样,逻辑视图名取决于对应请求的 URL,
如下面的例子:
@RequestMapping("/vets.do")
public ModelMap vetsHandler() {
return new ModelMap(this.clinic.getVets());
}
对应的逻辑视图名为“vets”,返回的 ModelMap 将被作为请求对应的模型对象,
可以在 JSP 视图页面中访问到。
ModelAndView
返回方式
1 使用无返回方法跳转,如果使用返回方法进行跳转的话,则会通过视图解析器进行以
prefix(前缀)+方法名+suffix(后缀)组成的页面文件名称.
2 使用一个返回的字符串方法作为跳转,使用字符串跳转的话好处就是在return的时候可
以自己指定返回的名字,JSP组成是prefix(前缀)+返回的字符串+suffix(后缀)
3 返回一个ModelAndView类型,使用setViewName方法则可以跳转到指定的页面.
路径匹配形式
1、单一Controller 对应 单一的请求路径
2、单一Controller 对应多个请求路径
3、单一Controller 对应多个请求路径,且路径内可以含有参数的形式
Demo code and UseCase
@Controller
@RequestMapping("/login.do")
public class SinglePathWithController {}
@Controller
@SessionAttributes(types = {UserBean.class,String.class},value={"currentUser","message"})
public class AdapterMultiPathController {}
@Controller
@RequestMapping(value = "/rest")
public class RestWithController {}
无返回
//无返回值 无参数返回的是根据 prefix前缀+@RequestMapping value +suffix
后缀组成
@RequestMapping("/springmvc/common")
public voidnovoid(HttpServletRequest request) {
request.setAttribute("message", "novoid方法被调用");
}
返回字符串
1、 作为视图路径方式
//根据路径直接匹配
@RequestMapping("/springmvc/multiReqPath1.do")
public String multiReqPath1(HttpServletRequest request){
request.setAttribute("message", "multiReqPath1方法被调用");
return "springmvc/common";
}
@RequestMapping("/springmvc/multiReqPath2.do")
public String multiReqPath2(HttpServletRequest request){
request.setAttribute("message", "multiReqPath2方法被调用");
return "/springmvc/common";
}
//根据参数匹配
@RequestMapping(params = "m=method1",method = RequestMethod.GET)
public String method1(){
return "login/success";
}
//有参数 参数名和请求url内的变量名一致
@RequestMapping(params = "m=method2")
public String method2(String name,String pwd){
return name;
}
//有参数 参数名和请求url内的变量名不一致
@RequestMapping(params = "m=method3",method = RequestMethod.GET)
public String method3(@RequestParam("loginName")String name,@RequestParam("loginPwd")String pwd,HttpServletRequest request){
request.setAttribute("message",(name + " " + pwd));
return "login/"+name;
}
2、 作为Response内容方式
//无参数
@ResponseBody
@RequestMapping(params = "m=method4")
public String method4(){
return "hello,guys";
}
//处理方法入参如何绑定 URL 参数
@ResponseBody
@RequestMapping(params = "m=method5",method = RequestMethod.GET)
public String method5(String name,String pwd,int delay){
return "name:"+name+","+"pwd:"+pwd+","+"delay:"+delay;
}
@ResponseBody
@RequestMapping(params = "m=method6",method = RequestMethod.GET)
public String method6(@RequestParam("userName")String name,DnTest test){
return "DnTest:"+test.toString();
}
URL 参数: userName参数将绑定到name上 其他与DnTest类内属性名称一致的参数将绑定到test的对应的属性上,如果参数不全 也不会报错
返回ModelAndView
@RequestMapping("/springmvc/modelAndView")
public ModelAndView modelAndView(){
ModelAndView mav = new ModelAndView();
mav.setViewName("/springmvc/common");
mav.addObject("message", "modelAndView 方法被调用");
return mav;
}
返回ModelMap
@RequestMapping("/springmvc/modelMap")
public ModelMap modelMap(ModelMap modMap){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("names", names);
modMap.put("message", "hello guys");
modMap.put("comment", "hello guys");
return modMap;
}
返回ModelMap
@RequestMapping("/springmvc/modelMap")
public ModelMap modelAndView(ModelMap modMap){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("hello", "hello guys");
modMap.put("names", names);
return modMap;
}
@SessionAttribute & ModMap
//注解方式
@Controller
@SessionAttributes(types = {UserBean.class,String.class},value={"currentUser","message"})
public class AdapterMultiPathController {}
//方法体
@RequestMapping("/springmvc/modelMap2")
public ModelMap modelMapWithSession(ModelMap modMap,HttpServletRequest request){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("names",names);
modMap.put("message", "hello guys");
modMap.put("comment", "hello guys");
UserBean user = new UserBean();
user.setName("Rick");
user.setMobile("18938900256");
user.setTelephone(request.getParameter("userPhone"));
user.setNumber(request.getParameter("userNumber"));
modMap.put("currentUser", user);
return modMap;
}
//初次请求
spring mvc & reverse ajax
@ResponseBody
@RequestMapping(params = "m=method7",method = RequestMethod.GET)
public String method7(String name,String pwd,int delay,HttpServletRequest req){
req.startAsync();
Date startTime = new Date();
try {
Thread.currentThread().sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date entTime = new Date();
return "name:"+name+","+"pwd:"+pwd+","+"delay:"+delay+",startTime:"+
DateUtils.formatDate(startTime, "yyyy-MM-dd HH:mm:ss:SSS")+",endTime:"+
DateUtils.formatDate(entTime, "yyyy-MM-dd HH:mm:ss:SSS");
}
RestFull
@Controller
@RequestMapping(value = "/rest")
public class RestWithController {}
@ResponseBody
@RequestMapping(value = "/{msg}", method = RequestMethod.GET)
public String restString(@PathVariable String msg) {
return msg;
}
@ResponseBody
@RequestMapping(value = "/{path}/{value}", method = RequestMethod.GET)
public String restXml(@PathVariable String path,@PathVariable String value) {
return "path:"+path+",value:"+value;
}
@ResponseBody
@RequestMapping(value = "/xml/{filename}", method = RequestMethod.GET)
public String restFile(@PathVariable String filename) {
if (filename!=null) {
ProjectInits init = ProjectInits.getInstance();
String dir = init.get("resource.dir", "C:/Projects/VoyagerWeb/resources");
FileUtility fUtil = new FileUtility();
String content = fUtil.readFile(dir+"/"+filename+".xml");
return content;
}
else
return "Invalid xml file name ["+filename+"]";
}
验证 是否支持Overload
方式一
//验证 是否支持Overload
@ResponseBody
@RequestMapping(value = "/validate/overload1", method = RequestMethod.GET)
public String overloadMethod(String name){
return name;
}
@ResponseBody
@RequestMapping(value = "/validate/overload2", method = RequestMethod.GET)
public String overloadMethod(String name,DnTest test){
return "DnTest:"+test.toString();
}
方式二
/验证 是否支持Overload
@ResponseBody
@RequestMapping(params = "m=method11")
public String method11(String name){
return name;
}
@ResponseBody
@RequestMapping(params = "m=method11")
public String method11(int age,DnTest test){
return "DnTest:"+test.toString();
}
标注在Bean的类定义处
@RequestMapping
真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping 这个注解
@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;
还可以标注在方法签名处,以便进一步对请求进行分流
配套的属性有:
value 需要跳转的地址
method 基于RestFul的跳转参数,有RequestMethod.get post put delete等
params 符合某个参数的时候才调用该方法
Headers 符合头信息的时候才调用
@SessionAttributes
将结果放入session内
@ModelAttribute
存储在响应内容ModelMap或者ModelAndView进行保存值传到前台,当如果你需要保存值比较少
的时候可以采用这种方式进行保存值并且保存到前台显示
在默认情况下,ModelMap 中的属性作用域是 request 级别,相当于HttpServletRequest中的request.setAttribute() 一样, 在 JSP 视图页面中通过 request.getAttribute(“attribute name”) 或者通过
${ attribute name } EL 表达式访问模型对象中的 属性对象
如果希望在ModelMap 的作用域范围为 session,可以有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现 如:
@Controller
@RequestMapping("/login.do")
@SessionAttributes("currUser")
public class BbtForumController {。。。。。}
@ResponseBody
标注后 返回String对象的结果为response内容体,不标注的话 作为dispatcher url使用
@PathVariable
允许将请求路径的制定内容当做求情的参数使用
返回类型
请求处理方法入参的可选类型 说明
void 此时逻辑视图名由请求处理方法对应的 URL 确定,如以下的方法:
@RequestMapping("/welcome.do")
public void welcomeHandler() {
}
对应的逻辑视图名为“welcome”
String 此时逻辑视图名为返回的字符,如以下的方法:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("ownerId") int ownerId, ModelMap model) {
Owner owner = this.clinic.loadOwner(ownerId);
model.addAttribute(owner);
return "ownerForm";
}
对应的逻辑视图名为“ownerForm”
ModelMap 和返回类型为 void 一样,逻辑视图名取决于对应请求的 URL,
如下面的例子:
@RequestMapping("/vets.do")
public ModelMap vetsHandler() {
return new ModelMap(this.clinic.getVets());
}
对应的逻辑视图名为“vets”,返回的 ModelMap 将被作为请求对应的模型对象,
可以在 JSP 视图页面中访问到。
ModelAndView
返回方式
1 使用无返回方法跳转,如果使用返回方法进行跳转的话,则会通过视图解析器进行以
prefix(前缀)+方法名+suffix(后缀)组成的页面文件名称.
2 使用一个返回的字符串方法作为跳转,使用字符串跳转的话好处就是在return的时候可
以自己指定返回的名字,JSP组成是prefix(前缀)+返回的字符串+suffix(后缀)
3 返回一个ModelAndView类型,使用setViewName方法则可以跳转到指定的页面.
路径匹配形式
1、单一Controller 对应 单一的请求路径
2、单一Controller 对应多个请求路径
3、单一Controller 对应多个请求路径,且路径内可以含有参数的形式
Demo code and UseCase
@Controller
@RequestMapping("/login.do")
public class SinglePathWithController {}
@Controller
@SessionAttributes(types = {UserBean.class,String.class},value={"currentUser","message"})
public class AdapterMultiPathController {}
@Controller
@RequestMapping(value = "/rest")
public class RestWithController {}
无返回
//无返回值 无参数返回的是根据 prefix前缀+@RequestMapping value +suffix
后缀组成
@RequestMapping("/springmvc/common")
public voidnovoid(HttpServletRequest request) {
request.setAttribute("message", "novoid方法被调用");
}
返回字符串
1、 作为视图路径方式
//根据路径直接匹配
@RequestMapping("/springmvc/multiReqPath1.do")
public String multiReqPath1(HttpServletRequest request){
request.setAttribute("message", "multiReqPath1方法被调用");
return "springmvc/common";
}
@RequestMapping("/springmvc/multiReqPath2.do")
public String multiReqPath2(HttpServletRequest request){
request.setAttribute("message", "multiReqPath2方法被调用");
return "/springmvc/common";
}
//根据参数匹配
@RequestMapping(params = "m=method1",method = RequestMethod.GET)
public String method1(){
return "login/success";
}
//有参数 参数名和请求url内的变量名一致
@RequestMapping(params = "m=method2")
public String method2(String name,String pwd){
return name;
}
//有参数 参数名和请求url内的变量名不一致
@RequestMapping(params = "m=method3",method = RequestMethod.GET)
public String method3(@RequestParam("loginName")String name,@RequestParam("loginPwd")String pwd,HttpServletRequest request){
request.setAttribute("message",(name + " " + pwd));
return "login/"+name;
}
2、 作为Response内容方式
//无参数
@ResponseBody
@RequestMapping(params = "m=method4")
public String method4(){
return "hello,guys";
}
//处理方法入参如何绑定 URL 参数
@ResponseBody
@RequestMapping(params = "m=method5",method = RequestMethod.GET)
public String method5(String name,String pwd,int delay){
return "name:"+name+","+"pwd:"+pwd+","+"delay:"+delay;
}
@ResponseBody
@RequestMapping(params = "m=method6",method = RequestMethod.GET)
public String method6(@RequestParam("userName")String name,DnTest test){
return "DnTest:"+test.toString();
}
URL 参数: userName参数将绑定到name上 其他与DnTest类内属性名称一致的参数将绑定到test的对应的属性上,如果参数不全 也不会报错
返回ModelAndView
@RequestMapping("/springmvc/modelAndView")
public ModelAndView modelAndView(){
ModelAndView mav = new ModelAndView();
mav.setViewName("/springmvc/common");
mav.addObject("message", "modelAndView 方法被调用");
return mav;
}
返回ModelMap
@RequestMapping("/springmvc/modelMap")
public ModelMap modelMap(ModelMap modMap){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("names", names);
modMap.put("message", "hello guys");
modMap.put("comment", "hello guys");
return modMap;
}
返回ModelMap
@RequestMapping("/springmvc/modelMap")
public ModelMap modelAndView(ModelMap modMap){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("hello", "hello guys");
modMap.put("names", names);
return modMap;
}
@SessionAttribute & ModMap
//注解方式
@Controller
@SessionAttributes(types = {UserBean.class,String.class},value={"currentUser","message"})
public class AdapterMultiPathController {}
//方法体
@RequestMapping("/springmvc/modelMap2")
public ModelMap modelMapWithSession(ModelMap modMap,HttpServletRequest request){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("names",names);
modMap.put("message", "hello guys");
modMap.put("comment", "hello guys");
UserBean user = new UserBean();
user.setName("Rick");
user.setMobile("18938900256");
user.setTelephone(request.getParameter("userPhone"));
user.setNumber(request.getParameter("userNumber"));
modMap.put("currentUser", user);
return modMap;
}
//初次请求
spring mvc & reverse ajax
@ResponseBody
@RequestMapping(params = "m=method7",method = RequestMethod.GET)
public String method7(String name,String pwd,int delay,HttpServletRequest req){
req.startAsync();
Date startTime = new Date();
try {
Thread.currentThread().sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date entTime = new Date();
return "name:"+name+","+"pwd:"+pwd+","+"delay:"+delay+",startTime:"+
DateUtils.formatDate(startTime, "yyyy-MM-dd HH:mm:ss:SSS")+",endTime:"+
DateUtils.formatDate(entTime, "yyyy-MM-dd HH:mm:ss:SSS");
}
RestFull
@Controller
@RequestMapping(value = "/rest")
public class RestWithController {}
@ResponseBody
@RequestMapping(value = "/{msg}", method = RequestMethod.GET)
public String restString(@PathVariable String msg) {
return msg;
}
@ResponseBody
@RequestMapping(value = "/{path}/{value}", method = RequestMethod.GET)
public String restXml(@PathVariable String path,@PathVariable String value) {
return "path:"+path+",value:"+value;
}
@ResponseBody
@RequestMapping(value = "/xml/{filename}", method = RequestMethod.GET)
public String restFile(@PathVariable String filename) {
if (filename!=null) {
ProjectInits init = ProjectInits.getInstance();
String dir = init.get("resource.dir", "C:/Projects/VoyagerWeb/resources");
FileUtility fUtil = new FileUtility();
String content = fUtil.readFile(dir+"/"+filename+".xml");
return content;
}
else
return "Invalid xml file name ["+filename+"]";
}
验证 是否支持Overload
方式一
//验证 是否支持Overload
@ResponseBody
@RequestMapping(value = "/validate/overload1", method = RequestMethod.GET)
public String overloadMethod(String name){
return name;
}
@ResponseBody
@RequestMapping(value = "/validate/overload2", method = RequestMethod.GET)
public String overloadMethod(String name,DnTest test){
return "DnTest:"+test.toString();
}
方式二
/验证 是否支持Overload
@ResponseBody
@RequestMapping(params = "m=method11")
public String method11(String name){
return name;
}
@ResponseBody
@RequestMapping(params = "m=method11")
public String method11(int age,DnTest test){
return "DnTest:"+test.toString();
}
发表评论
-
springCloud相关
2017-06-09 11:01 1128http://www.cnblogs.com/skyblo ... -
Spring相关文件收录
2017-02-17 15:39 267http://blog.csdn.net/ufo291062 ... -
shiro 集成相关说明
2016-12-09 09:29 1024最近,将一个单一的maven项目拆分为多maven模 ... -
spring boot 系列
2016-12-06 18:03 4531、spring boot 系列教程 http://bl ... -
spring的Scheduled(定时任务)和多线程
2016-07-30 15:16 1631spring的Scheduled(定时任务)和多线程 ... -
Spring 事务机制详解
2016-06-03 08:57 1116原文:http://mp.weixin.qq.c ... -
深入分析Spring 与 Spring MVC容器
2016-05-31 12:58 1440原文: http://mp.weixin ... -
Spring集成Quartz定时任务
2016-01-26 12:04 1371原文:http://stevex.blog.51cto.co ... -
spring 多数据源的配置
2015-04-01 11:18 643参考:http://blog.csdn.net/wangp ... -
深入浅出学Spring Data JPA
2014-11-07 09:41 1114原文:http://sishuok.com/forum/ ... -
10分钟教会你Apache Shiro
2014-09-28 21:00 871参考:http://www.cnblogs.com/ibo ...
相关推荐
springmvc基础 包含代码+知识点+详细解释 1. 什么是springmvc? 2. springmvc框架原理 前端控制器、处理器映射器、处理器适配器、视图解析器 3. springmvc入门程序 目的:对前端控制器、处理器映射器、处理器...
在本篇里我们给大家总结了关于springmvc常用5种注解相关知识点以及实例代码,需要的朋友们参考下。
Java中常用的框架,SSH、SSM、SpringMVC运行原理、SpringMVC常用注解及工作原理等等。
面向对象:封装、继承、多态 Java基础:变量、数据类型、运算符、条件语句、循环语句、数组、集合、异常...常用的框架:Spring、SpringMVC、MyBatis、Hibernate等 分布式相关技术:Dubbo、Zookeeper、Redis、RocketMQ等
3. SpringMVC 常用注解都有哪些? 4. Spring 的常用注解 5. 如何开启注解处理器和适配器? 6. 如何解决 get 和 post 乱码问题? 7. 谈谈你对 Spring 的理解 (8条) 8. Spring 中的设计模式(解释) 9. 简单介绍...
本教程完整地讲解了Spring MVC的每个知识点,包括独立运行的Demo示例,欢迎一起交流学习。Java学习交流群:239126529 教程软件版本说明: 1)Spring MVC框架:spring-framework-5.0.2.RELEASE 2)开发工具:IntelliJ...
4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行...
熟练掌握java基础知识,具有良好的编码习惯; 熟悉java高级特性,比如多线程编程、网络编程、反射、注解等; 熟练掌握javaweb技术,具有独立的开发能力,熟悉http协议,EL,JSTL表达式; 熟悉html、css...
4.3 Spring MVC 的常用注解 82 4.3.1 点睛 82 4.3.2 示例 83 4.4 Spring MVC 基本配置 87 4.4.1 静态资源映射 88 4.4.2 拦截器配置 89 4.4.3 @ControllerAdvice 91 4.4.4 其他配置 94 4.5 Spring MVC 的高级配置 98 ...
以后不再维护,转移到笔记总览(source目录)反弹:Java笔记基础java知识点,基本全是代码+注释反射,注解,jdbc,javaweb,redis基本使用java并发基础,完善中。替代设计原理。 杂项,日常学习中遇见的一些问题,...