spring 注解学习手札(二) 控制层梳理

上传人:第*** 文档编号:32697479 上传时间:2018-02-12 格式:DOCX 页数:13 大小:80.99KB
返回 下载 相关 举报
spring 注解学习手札(二) 控制层梳理_第1页
第1页 / 共13页
spring 注解学习手札(二) 控制层梳理_第2页
第2页 / 共13页
spring 注解学习手札(二) 控制层梳理_第3页
第3页 / 共13页
spring 注解学习手札(二) 控制层梳理_第4页
第4页 / 共13页
spring 注解学习手札(二) 控制层梳理_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《spring 注解学习手札(二) 控制层梳理》由会员分享,可在线阅读,更多相关《spring 注解学习手札(二) 控制层梳理(13页珍藏版)》请在金锄头文库上搜索。

1、言归正传,研究一下注解下的控制层。 我习惯于使用 JSTL 展示页面,因此需要在原 lib 基础上增加 jstl.jar 和standard.jar,详细 lib 依赖如下: 引用aopalliance-1.0.jar commons-logging-1.1.1.jar log4j-1.2.15.jar spring-beans-2.5.6.jar spring-context-2.5.6.jar spring-context-support-2.5.6.jar spring-core-2.5.6.jar spring-tx-2.5.6.jar spring-web-2.5.6.jar spr

2、ing-webmvc-2.5.6.jar standard.jar jstl.jar 上一篇文中,我们定义了控制器 AccountController: AccountController.java Java代码 1. /* 2. * 2010-1-23 3. */ 4. package org.zlex.spring.controller; 5. 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. 9. import org.springfr

3、amework.beans.factory.annotation.Autowired; 10. import org.springframework.stereotype.Controller; 11. import org.springframework.web.bind.ServletRequestUtils; 12. import org.springframework.web.bind.annotation.RequestMapping; 13. import org.springframework.web.bind.annotation.RequestMethod; 14. impo

4、rt org.zlex.spring.service.AccountService; 15. 16. /* 17. * 18. * author 梁栋19. * version 1.0 20. * since 1.0 21. */ 22. Controller 23. RequestMapping(/account.do) 24. public class AccountController 25. 26. Autowired 27. private AccountService accountService; 28. 29. RequestMapping(method = RequestMe

5、thod.GET) 30. public void hello(HttpServletRequest request, HttpServletResponse response) 31. throws Exception 32. 33. String username = ServletRequestUtils.getRequiredStringParameter( 34. request, username); 35. String password = ServletRequestUtils.getRequiredStringParameter( 36. request, password

6、); 37. System.out.println(accountService.verify(username, password); 38. 39. 先说注解RequestMapping 这里使用注解RequestMapping(method = RequestMethod.GET)指定这个方法为get 请求时调用。同样,我们可以使用注解RequestMapping(method = RequestMethod.POST)指定该方法接受 post 请求。 Java代码 1. Controller 2. RequestMapping(/account.do) 3. public class

7、AccountController 4. 5. RequestMapping(method = RequestMethod.GET) 6. public void get() 7. 8. 9. RequestMapping(method = RequestMethod.POST) 10. public void post() 11. 12. 这与我们久别的 Servlet 很相像,类似于 doGet()和 doPost()方法! 我们也可以将其改造为多动作控制器,如下代码所示: Java代码 1. Controller 2. RequestMapping(/account.do) 3. pub

8、lic class AccountController 4. 5. RequestMapping(params = method=login) 6. public void login() 7. 8. 9. RequestMapping(params = method=logout) 10. public void logout() 11. 这样,我们可以通过参数“method”指定不同的参数值从而通过请求(/account.do?method=login和/account.do?method=logout )调用不同的方法! 注意:使用多动作控制器必须在配置文件中加入注解支持! Xml代码

9、1. 当然,我们也可以将注解RequestMapping 指定到某一个方法上,如: Java代码 1. Controller 2. public class AccountController 3. 4. RequestMapping(/a.do) 5. public void a() 6. 7. RequestMapping(/b.do) 8. public void b() 9. 这样,请求“a.do”和“b.do”将对应不同的方法 a() 和 b()。这使得一个控制器可以同时承载多个请求! RequestMapping(/account.do)是RequestMapping(value=

10、/account.do)的简写! 再说输入参数! 这里的方法名可以随意定义,但是参数和返回值却又要求! 为什么?直接看源代码,我们就能找到答案! AnnotationMethodHandlerAdapter.java 部分源代码 有关参数部分: Java代码 1. Override 2. protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest) 3. throws Exception 4. 5. HttpServletRequest request = (HttpServ

11、letRequest) webRequest.getNativeRequest(); 6. HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse(); 7. 8. if (ServletRequest.class.isAssignableFrom(parameterType) 9. return request; 10. 11. else if (ServletResponse.class.isAssignableFrom(parameterType) 12. this.respons

12、eArgumentUsed = true; 13. return response; 14. 15. else if (HttpSession.class.isAssignableFrom(parameterType) 16. return request.getSession(); 17. 18. else if (Principal.class.isAssignableFrom(parameterType) 19. return request.getUserPrincipal(); 20. 21. else if (Locale.class.equals(parameterType) 2

13、2. return RequestContextUtils.getLocale(request); 23. 24. else if (InputStream.class.isAssignableFrom(parameterType) 25. return request.getInputStream(); 26. 27. else if (Reader.class.isAssignableFrom(parameterType) 28. return request.getReader(); 29. 30. else if (OutputStream.class.isAssignableFrom

14、(parameterType) 31. this.responseArgumentUsed = true; 32. return response.getOutputStream(); 33. 34. else if (Writer.class.isAssignableFrom(parameterType) 35. this.responseArgumentUsed = true; 36. return response.getWriter(); 37. 38. return super.resolveStandardArgument(parameterType, webRequest); 39. 也就是说,如果我们想要在自定义的方法中获得一些个“标准”输入参数,参数类型必须包含在以下类型中: 引用ServletRequest ServletResponse HttpSession Principal Locale InputStr

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号