- 浏览: 110419 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (87)
- SQL语句优化技术分析 (3)
- SQL SERVER查询优化 (2)
- JDBC查询优化 (1)
- 服务器调优 (1)
- 100 Best Quotes On Leadership (1)
- Java NIO Chinese Tutorial (1)
- spring security (1)
- JAVA线程池的分析和使用 (1)
- Java并发 (1)
- Java NIO框架Mina (1)
- Netty (1)
- Grizzly比较 (1)
- What Factors Justify the Use of Apache Hadoop? (1)
- Instagram工程师教你如何改善App的性能 (1)
- The Ubuntu's Directory Tree (1)
- JVM调优总结 -Xms -Xmx -Xmn -Xss (1)
- Tomcat configuration recommendations (1)
- Java 7 并发编程指南 (1)
- Java完整并发教程 (1)
- Java公钥私钥签名工具包应用实例 (1)
- Java集群解决方案 (1)
- Memcache和Redis比较 (1)
- 大型网站系统架构的演化 (1)
- 天猫浏览型应用的CDN静态化架构演变 (1)
- 淘宝的架构 (1)
- 大规模网站架构的缓存机制和几何分形学 (1)
- 敏捷开发 (1)
- 什么时候用MongoDB? (1)
- Java算法 (1)
- OAuth 2.0概览 (1)
- 全面理解OAuth 2.0 (1)
- Java工程师面试必问题目 (1)
最新评论
-
zhanggeng22:
[b][i][u]引用[list]
[*][img][url] ...
tomcat 的server.xml配置详解,看完你100%熟练配置tomcat -
yangsong158:
好文章,必需顶一个
大规模网站架构的缓存机制和几何分形学 -
卖火柴的老特工:
request.getparameter是获取通过get或则p ...
Request的getParameter()和getAttribute()方法的区别 -
zhuzhiguosnail:
终于能恢复了
十三个价值连城的成功习惯 -
liuwenbo200285:
sturts1 action是单例模式,线程是安全的。 ...
Java常碰面试题
昨天对Spring注解有了一个整体认识,至少完成了一个简单的web应用搭建。当然,还不完善,这仅仅只是个开始!
今天看了Spring 3.0的注解,我感觉自己被颠覆了。多年前,为了减少代码依赖我们用配置文件进行模块间耦合,降低模块之间的黏度。现如今,所有可配置的内容都塞进了代码中,我只能说:这多少有点顾此失彼,有点倒退的意思!使用注解的好处是:代码通读性增强。这既是优势也是劣势!如果我要改一段配置,就要打开代码逐行扫描;如果恰巧这是别人封装的jar包,那我只好反编译;如果碰巧遇上这个jar包经过了混淆,那我只好求助于AOP了。 为了这么一个配置,我的代码观几乎将要被颠覆!
言归正传,研究一下注解下的控制层。
我习惯于使用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
spring-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.springframework.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.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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 = RequestMethod.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");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
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.springframework.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.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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 = RequestMethod.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");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
/**
* 2010-1-23
*/
package org.zlex.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.service.AccountService;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping("/account.do")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(method = RequestMethod.GET)
public void hello(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = ServletRequestUtils.getRequiredStringParameter(
request, "username");
String password = ServletRequestUtils.getRequiredStringParameter(
request, "password");
System.out.println(accountService.verify(username, password));
}
}
先说注解@RequestMapping
这里使用注解@RequestMapping(method = RequestMethod.GET)指定这个方法为get请求时调用。同样,我们可以使用注解@RequestMapping(method = RequestMethod.POST)指定该方法接受post请求。
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(method = RequestMethod.GET)
6. public void get() {
7. }
8.
9. @RequestMapping(method = RequestMethod.POST)
10. public void post() {
11. }
12.}
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(method = RequestMethod.GET)
6. public void get() {
7. }
8.
9. @RequestMapping(method = RequestMethod.POST)
10. public void post() {
11. }
12.}
@Controller
@RequestMapping("/account.do")
public class AccountController {
@RequestMapping(method = RequestMethod.GET)
public void get() {
}
@RequestMapping(method = RequestMethod.POST)
public void post() {
}
}
这与我们久别的Servlet很相像,类似于doGet()和doPost()方法!
我们也可以将其改造为多动作控制器,如下代码所示:
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(params = "method=login")
6. public void login() {
7. }
8.
9. @RequestMapping(params = "method=logout")
10. public void logout() {
11. }
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(params = "method=login")
6. public void login() {
7. }
8.
9. @RequestMapping(params = "method=logout")
10. public void logout() {
11. }
@Controller
@RequestMapping("/account.do")
public class AccountController {
@RequestMapping(params = "method=login")
public void login() {
}
@RequestMapping(params = "method=logout")
public void logout() {
}
这样,我们可以通过参数“method”指定不同的参数值从而通过请求("/account.do?method=login"和"/account.do?method=logout")调用不同的方法!
注意:使用多动作控制器必须在配置文件中加入注解支持!
Xml代码
1.<bean
2. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
Xml代码
1.<bean
2. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
当然,我们也可以将注解@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.}
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.}
@Controller
public class AccountController {
@RequestMapping("/a.do")
public void a() {}
@RequestMapping("/b.do")
public void b() {}
}
这样,请求“a.do”和“b.do”将对应不同的方法a() 和b()。这使得一个控制器可以同时承载多个请求!
@RequestMapping("/account.do")是@RequestMapping(value="/account.do")的简写!
再说输入参数!
这里的方法名可以随意定义,但是参数和返回值却又要求!
为什么?直接看源代码,我们就能找到答案!
AnnotationMethodHandlerAdapter.java部分源代码——有关参数部分:
Java代码
1.@Override
2.protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)
3. throws Exception {
4.
5. HttpServletRequest request = (HttpServletRequest) 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.responseArgumentUsed = 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)) {
22. 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(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.}
Java代码
1.@Override
2.protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)
3. throws Exception {
4.
5. HttpServletRequest request = (HttpServletRequest) 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.responseArgumentUsed = 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)) {
22. 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(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.}
@Override
protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)
throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse();
if (ServletRequest.class.isAssignableFrom(parameterType)) {
return request;
}
else if (ServletResponse.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response;
}
else if (HttpSession.class.isAssignableFrom(parameterType)) {
return request.getSession();
}
else if (Principal.class.isAssignableFrom(parameterType)) {
return request.getUserPrincipal();
}
else if (Locale.class.equals(parameterType)) {
return RequestContextUtils.getLocale(request);
}
else if (InputStream.class.isAssignableFrom(parameterType)) {
return request.getInputStream();
}
else if (Reader.class.isAssignableFrom(parameterType)) {
return request.getReader();
}
else if (OutputStream.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response.getOutputStream();
}
else if (Writer.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response.getWriter();
}
return super.resolveStandardArgument(parameterType, webRequest);
}
也就是说,如果我们想要在自定义的方法中获得一些个“标准”输入参数,参数类型必须包含在以下类型中:
引用
ServletRequest
ServletResponse
HttpSession
Principal
Locale
InputStream
OutputStream
Reader
Writer
当然,上述接口其实都是对于HttpServletRequest和HttpServletResponse的扩展。
此外,我们还可以定义自己的参数。
注意:自定义参数必须是实现类,绝非接口!Spring容器将帮你完成对象初始化工作!
比如说上文中,我们需要参数username和password。我们可以这么写:
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(String username,String password) {
3. System.out.println(accountService.verify(username, password));
4.}
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(String username,String password) {
3. System.out.println(accountService.verify(username, password));
4.}
@RequestMapping(method = RequestMethod.GET)
public void hello(String username,String password) {
System.out.println(accountService.verify(username, password));
}
如果参数名不能与这里的变量名保持一致,那么我们可以使用注解@RequestParam进行强制绑定,代码如下所示:
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(@RequestParam("username") String u,
3. @RequestParam("password") String p) {
4. System.out.println(accountService.verify(u, p));
5.}
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(@RequestParam("username") String u,
3. @RequestParam("password") String p) {
4. System.out.println(accountService.verify(u, p));
5.}
@RequestMapping(method = RequestMethod.GET)
public void hello(@RequestParam("username") String u,
@RequestParam("password") String p) {
System.out.println(accountService.verify(u, p));
}
这比起我们之前写的代码有所简洁:
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(HttpServletRequest request, HttpServletResponse response)
3. throws Exception {
4.
5. String username = ServletRequestUtils.getRequiredStringParameter(
6. request, "username");
7. String password = ServletRequestUtils.getRequiredStringParameter(
8. request, "password");
9. System.out.println(accountService.verify(username, password));
10.}
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(HttpServletRequest request, HttpServletResponse response)
3. throws Exception {
4.
5. String username = ServletRequestUtils.getRequiredStringParameter(
6. request, "username");
7. String password = ServletRequestUtils.getRequiredStringParameter(
8. request, "password");
9. System.out.println(accountService.verify(username, password));
10.}
@RequestMapping(method = RequestMethod.GET)
public void hello(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = ServletRequestUtils.getRequiredStringParameter(
request, "username");
String password = ServletRequestUtils.getRequiredStringParameter(
request, "password");
System.out.println(accountService.verify(username, password));
}
ServletRequestUtils类的工作已经由Spring底层实现了,我们只需要把参数名定义一致即可,其内部取参无需关心!
除了传入参数,我们还可以定义即将传出的参数,如加入ModelMap参数:
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public Map hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return model;
10.}
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public Map hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return model;
10.}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public Map hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return model;
}
这时,我们没有定义页面名称,Spring容器将根据请求名指定同名view,即如果是jap页面,则account.do->account.jsp!
不得不承认,这样写起来的确减少了代码量!
接着说输出参数!
通过ModelMap,我们可以绑定输出到的页面的参数,但最终我们将要返回到何种页面呢?再次查看AnnotationMethodHandlerAdapter源代码!
AnnotationMethodHandlerAdapter.java部分源代码——有关返回值部分:
Java代码
1.@SuppressWarnings("unchecked")
2.public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
3. ExtendedModelMap implicitModel, ServletWebRequest webRequest) {
4.
5. if (returnValue instanceof ModelAndView) {
6. ModelAndView mav = (ModelAndView) returnValue;
7. mav.getModelMap().mergeAttributes(implicitModel);
8. return mav;
9. }
10. else if (returnValue instanceof Model) {
11. return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
12. }
13. else if (returnValue instanceof Map) {
14. return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
15. }
16. else if (returnValue instanceof View) {
17. return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
18. }
19. else if (returnValue instanceof String) {
20. return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
21. }
22. else if (returnValue == null) {
23. // Either returned null or was 'void' return.
24. if (this.responseArgumentUsed || webRequest.isNotModified()) {
25. return null;
26. }
27. else {
28. // Assuming view name translation...
29. return new ModelAndView().addAllObjects(implicitModel);
30. }
31. }
32. else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
33. // Assume a single model attribute...
34. ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
35. String attrName = (attr != null ? attr.value() : "");
36. ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);
37. if ("".equals(attrName)) {
38. Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
39. attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
40. }
41. return mav.addObject(attrName, returnValue);
42. }
43. else {
44. throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
45. }
46.}
Java代码
1.@SuppressWarnings("unchecked")
2.public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
3. ExtendedModelMap implicitModel, ServletWebRequest webRequest) {
4.
5. if (returnValue instanceof ModelAndView) {
6. ModelAndView mav = (ModelAndView) returnValue;
7. mav.getModelMap().mergeAttributes(implicitModel);
8. return mav;
9. }
10. else if (returnValue instanceof Model) {
11. return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
12. }
13. else if (returnValue instanceof Map) {
14. return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
15. }
16. else if (returnValue instanceof View) {
17. return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
18. }
19. else if (returnValue instanceof String) {
20. return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
21. }
22. else if (returnValue == null) {
23. // Either returned null or was 'void' return.
24. if (this.responseArgumentUsed || webRequest.isNotModified()) {
25. return null;
26. }
27. else {
28. // Assuming view name translation...
29. return new ModelAndView().addAllObjects(implicitModel);
30. }
31. }
32. else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
33. // Assume a single model attribute...
34. ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
35. String attrName = (attr != null ? attr.value() : "");
36. ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);
37. if ("".equals(attrName)) {
38. Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
39. attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
40. }
41. return mav.addObject(attrName, returnValue);
42. }
43. else {
44. throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
45. }
46.}
@SuppressWarnings("unchecked")
public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
ExtendedModelMap implicitModel, ServletWebRequest webRequest) {
if (returnValue instanceof ModelAndView) {
ModelAndView mav = (ModelAndView) returnValue;
mav.getModelMap().mergeAttributes(implicitModel);
return mav;
}
else if (returnValue instanceof Model) {
return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
}
else if (returnValue instanceof Map) {
return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
}
else if (returnValue instanceof View) {
return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
}
else if (returnValue instanceof String) {
return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
}
else if (returnValue == null) {
// Either returned null or was 'void' return.
if (this.responseArgumentUsed || webRequest.isNotModified()) {
return null;
}
else {
// Assuming view name translation...
return new ModelAndView().addAllObjects(implicitModel);
}
}
else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
// Assume a single model attribute...
ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
String attrName = (attr != null ? attr.value() : "");
ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);
if ("".equals(attrName)) {
Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
}
return mav.addObject(attrName, returnValue);
}
else {
throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
}
}
}
返回值的定义十分庞大,或者说可怕的if-else多少有点让我觉得厌恶!
我们可以定义以下类型的返回值:
引用
ModelAndView
Model
View
Map
String
null
ModelAndView、Model和View都是Spring之前版本所特有的元素,Map对应于传入参数ModelMap,String定义页面名称,null即对应void类型方法!
最常用的实现方式如下:
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public String hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return "account";
10.}
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public String hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return "account";
10.}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public String hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return "account";
}
当然,对于我来说在返回值中写入这么一个字符串多少有点不能接受,于是我还是乐于使用输入参数ModelMap+输出参数Map的方式。
给出一个完整的AccountController实现:
AccountController.java
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import java.util.Map;
7.
8.import org.springframework.beans.factory.annotation.Autowired;
9.import org.springframework.stereotype.Controller;
10.import org.springframework.ui.ModelMap;
11.import org.springframework.web.bind.annotation.RequestMapping;
12.import org.springframework.web.bind.annotation.RequestMethod;
13.import org.zlex.spring.service.AccountService;
14.
15./**
16. *
17. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
18. * @version 1.0
19. * @since 1.0
20. */
21.@Controller
22.@RequestMapping("/account.do")
23.public class AccountController {
24.
25. @Autowired
26. private AccountService accountService;
27.
28. @SuppressWarnings("unchecked")
29. @RequestMapping(method = RequestMethod.GET)
30. public Map hello(String username, String password, ModelMap model) {
31.
32. System.out.println(accountService.verify(username, password));
33.
34. model.put("msg", username);
35. return model;
36. }
37.}
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import java.util.Map;
7.
8.import org.springframework.beans.factory.annotation.Autowired;
9.import org.springframework.stereotype.Controller;
10.import org.springframework.ui.ModelMap;
11.import org.springframework.web.bind.annotation.RequestMapping;
12.import org.springframework.web.bind.annotation.RequestMethod;
13.import org.zlex.spring.service.AccountService;
14.
15./**
16. *
17. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
18. * @version 1.0
19. * @since 1.0
20. */
21.@Controller
22.@RequestMapping("/account.do")
23.public class AccountController {
24.
25. @Autowired
26. private AccountService accountService;
27.
28. @SuppressWarnings("unchecked")
29. @RequestMapping(method = RequestMethod.GET)
30. public Map hello(String username, String password, ModelMap model) {
31.
32. System.out.println(accountService.verify(username, password));
33.
34. model.put("msg", username);
35. return model;
36. }
37.}
/**
* 2010-1-23
*/
package org.zlex.spring.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
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.zlex.spring.service.AccountService;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping("/account.do")
public class AccountController {
@Autowired
private AccountService accountService;
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public Map hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return model;
}
}
最后说注解@Session
如果想将某个ModelMap中的参数指定到Session中,可以使用@Session注解,将其绑定为Session熟悉,代码如下所示:
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.@SessionAttributes("msg")
4.public class AccountController {
5.
6. @Autowired
7. private AccountService accountService;
8.
9. @SuppressWarnings("unchecked")
10. @RequestMapping(method = RequestMethod.GET)
11. public Map hello(String username, String password, ModelMap model) {
12.
13. System.out.println(accountService.verify(username, password));
14.
15. model.put("msg", username);
16. return model;
17. }
18.
19.}
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.@SessionAttributes("msg")
4.public class AccountController {
5.
6. @Autowired
7. private AccountService accountService;
8.
9. @SuppressWarnings("unchecked")
10. @RequestMapping(method = RequestMethod.GET)
11. public Map hello(String username, String password, ModelMap model) {
12.
13. System.out.println(accountService.verify(username, password));
14.
15. model.put("msg", username);
16. return model;
17. }
18.
19.}
@Controller
@RequestMapping("/account.do")
@SessionAttributes("msg")
public class AccountController {
@Autowired
private AccountService accountService;
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public Map hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return model;
}
}
当然,我们还需要配置一下对应的视图解析器,给出完整配置:
servelt.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xmlns:context="http://www.springframework.org/schema/context"
7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9. <context:component-scan
10. base-package="org.zlex.spring.controller" />
11. <bean
12. id="urlMapping"
13. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
14. <bean
15. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
16. <bean
17. id="jstlViewResolver"
18. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
19. p:viewClass="org.springframework.web.servlet.view.JstlView"
20. p:prefix="/WEB-INF/page/"
21. p:suffix=".jsp" />
22.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xmlns:context="http://www.springframework.org/schema/context"
7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9. <context:component-scan
10. base-package="org.zlex.spring.controller" />
11. <bean
12. id="urlMapping"
13. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
14. <bean
15. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
16. <bean
17. id="jstlViewResolver"
18. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
19. p:viewClass="org.springframework.web.servlet.view.JstlView"
20. p:prefix="/WEB-INF/page/"
21. p:suffix=".jsp" />
22.</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="org.zlex.spring.controller" />
<bean
id="urlMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
id="jstlViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/page/"
p:suffix=".jsp" />
</beans>
这里使用了JstlView作为视图解析器。同时,指定前缀路径为"/WEB-INF/page/",后缀路径为".jsp"。也就是说,Spring容器将会在这个路径中寻找匹配的jsp文件!
注意加入xmlns:p="http://www.springframework.org/schema/p"命名空间!
再给出页面内容:
taglib.jsp
Jsp代码
1.<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2.<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
3.<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
4.<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
5.<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
6.<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
7.<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
Jsp代码
1.<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2.<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
3.<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
4.<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
5.<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
6.<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
7.<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
account.jap
Jsp代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ include file="/WEB-INF/page/taglib.jsp"%>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>Account</title>
9.</head>
10.<body>
11.<c:out value="${msg}"></c:out>
12.</body>
13.</html>
Jsp代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ include file="/WEB-INF/page/taglib.jsp"%>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>Account</title>
9.</head>
10.<body>
11.<c:out value="${msg}"></c:out>
12.</body>
13.</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Account</title>
</head>
<body>
<c:out value="${msg}"></c:out>
</body>
</html>
目录结构如图所示:
启动应用,最后将得到一个带有内容的页面,如图:
代码见附件!
今天看了Spring 3.0的注解,我感觉自己被颠覆了。多年前,为了减少代码依赖我们用配置文件进行模块间耦合,降低模块之间的黏度。现如今,所有可配置的内容都塞进了代码中,我只能说:这多少有点顾此失彼,有点倒退的意思!使用注解的好处是:代码通读性增强。这既是优势也是劣势!如果我要改一段配置,就要打开代码逐行扫描;如果恰巧这是别人封装的jar包,那我只好反编译;如果碰巧遇上这个jar包经过了混淆,那我只好求助于AOP了。 为了这么一个配置,我的代码观几乎将要被颠覆!
言归正传,研究一下注解下的控制层。
我习惯于使用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
spring-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.springframework.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.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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 = RequestMethod.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");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
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.springframework.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.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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 = RequestMethod.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");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
/**
* 2010-1-23
*/
package org.zlex.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.service.AccountService;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping("/account.do")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(method = RequestMethod.GET)
public void hello(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = ServletRequestUtils.getRequiredStringParameter(
request, "username");
String password = ServletRequestUtils.getRequiredStringParameter(
request, "password");
System.out.println(accountService.verify(username, password));
}
}
先说注解@RequestMapping
这里使用注解@RequestMapping(method = RequestMethod.GET)指定这个方法为get请求时调用。同样,我们可以使用注解@RequestMapping(method = RequestMethod.POST)指定该方法接受post请求。
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(method = RequestMethod.GET)
6. public void get() {
7. }
8.
9. @RequestMapping(method = RequestMethod.POST)
10. public void post() {
11. }
12.}
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(method = RequestMethod.GET)
6. public void get() {
7. }
8.
9. @RequestMapping(method = RequestMethod.POST)
10. public void post() {
11. }
12.}
@Controller
@RequestMapping("/account.do")
public class AccountController {
@RequestMapping(method = RequestMethod.GET)
public void get() {
}
@RequestMapping(method = RequestMethod.POST)
public void post() {
}
}
这与我们久别的Servlet很相像,类似于doGet()和doPost()方法!
我们也可以将其改造为多动作控制器,如下代码所示:
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(params = "method=login")
6. public void login() {
7. }
8.
9. @RequestMapping(params = "method=logout")
10. public void logout() {
11. }
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.public class AccountController {
4.
5. @RequestMapping(params = "method=login")
6. public void login() {
7. }
8.
9. @RequestMapping(params = "method=logout")
10. public void logout() {
11. }
@Controller
@RequestMapping("/account.do")
public class AccountController {
@RequestMapping(params = "method=login")
public void login() {
}
@RequestMapping(params = "method=logout")
public void logout() {
}
这样,我们可以通过参数“method”指定不同的参数值从而通过请求("/account.do?method=login"和"/account.do?method=logout")调用不同的方法!
注意:使用多动作控制器必须在配置文件中加入注解支持!
Xml代码
1.<bean
2. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
Xml代码
1.<bean
2. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
当然,我们也可以将注解@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.}
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.}
@Controller
public class AccountController {
@RequestMapping("/a.do")
public void a() {}
@RequestMapping("/b.do")
public void b() {}
}
这样,请求“a.do”和“b.do”将对应不同的方法a() 和b()。这使得一个控制器可以同时承载多个请求!
@RequestMapping("/account.do")是@RequestMapping(value="/account.do")的简写!
再说输入参数!
这里的方法名可以随意定义,但是参数和返回值却又要求!
为什么?直接看源代码,我们就能找到答案!
AnnotationMethodHandlerAdapter.java部分源代码——有关参数部分:
Java代码
1.@Override
2.protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)
3. throws Exception {
4.
5. HttpServletRequest request = (HttpServletRequest) 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.responseArgumentUsed = 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)) {
22. 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(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.}
Java代码
1.@Override
2.protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)
3. throws Exception {
4.
5. HttpServletRequest request = (HttpServletRequest) 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.responseArgumentUsed = 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)) {
22. 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(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.}
@Override
protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)
throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse();
if (ServletRequest.class.isAssignableFrom(parameterType)) {
return request;
}
else if (ServletResponse.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response;
}
else if (HttpSession.class.isAssignableFrom(parameterType)) {
return request.getSession();
}
else if (Principal.class.isAssignableFrom(parameterType)) {
return request.getUserPrincipal();
}
else if (Locale.class.equals(parameterType)) {
return RequestContextUtils.getLocale(request);
}
else if (InputStream.class.isAssignableFrom(parameterType)) {
return request.getInputStream();
}
else if (Reader.class.isAssignableFrom(parameterType)) {
return request.getReader();
}
else if (OutputStream.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response.getOutputStream();
}
else if (Writer.class.isAssignableFrom(parameterType)) {
this.responseArgumentUsed = true;
return response.getWriter();
}
return super.resolveStandardArgument(parameterType, webRequest);
}
也就是说,如果我们想要在自定义的方法中获得一些个“标准”输入参数,参数类型必须包含在以下类型中:
引用
ServletRequest
ServletResponse
HttpSession
Principal
Locale
InputStream
OutputStream
Reader
Writer
当然,上述接口其实都是对于HttpServletRequest和HttpServletResponse的扩展。
此外,我们还可以定义自己的参数。
注意:自定义参数必须是实现类,绝非接口!Spring容器将帮你完成对象初始化工作!
比如说上文中,我们需要参数username和password。我们可以这么写:
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(String username,String password) {
3. System.out.println(accountService.verify(username, password));
4.}
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(String username,String password) {
3. System.out.println(accountService.verify(username, password));
4.}
@RequestMapping(method = RequestMethod.GET)
public void hello(String username,String password) {
System.out.println(accountService.verify(username, password));
}
如果参数名不能与这里的变量名保持一致,那么我们可以使用注解@RequestParam进行强制绑定,代码如下所示:
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(@RequestParam("username") String u,
3. @RequestParam("password") String p) {
4. System.out.println(accountService.verify(u, p));
5.}
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(@RequestParam("username") String u,
3. @RequestParam("password") String p) {
4. System.out.println(accountService.verify(u, p));
5.}
@RequestMapping(method = RequestMethod.GET)
public void hello(@RequestParam("username") String u,
@RequestParam("password") String p) {
System.out.println(accountService.verify(u, p));
}
这比起我们之前写的代码有所简洁:
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(HttpServletRequest request, HttpServletResponse response)
3. throws Exception {
4.
5. String username = ServletRequestUtils.getRequiredStringParameter(
6. request, "username");
7. String password = ServletRequestUtils.getRequiredStringParameter(
8. request, "password");
9. System.out.println(accountService.verify(username, password));
10.}
Java代码
1.@RequestMapping(method = RequestMethod.GET)
2.public void hello(HttpServletRequest request, HttpServletResponse response)
3. throws Exception {
4.
5. String username = ServletRequestUtils.getRequiredStringParameter(
6. request, "username");
7. String password = ServletRequestUtils.getRequiredStringParameter(
8. request, "password");
9. System.out.println(accountService.verify(username, password));
10.}
@RequestMapping(method = RequestMethod.GET)
public void hello(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = ServletRequestUtils.getRequiredStringParameter(
request, "username");
String password = ServletRequestUtils.getRequiredStringParameter(
request, "password");
System.out.println(accountService.verify(username, password));
}
ServletRequestUtils类的工作已经由Spring底层实现了,我们只需要把参数名定义一致即可,其内部取参无需关心!
除了传入参数,我们还可以定义即将传出的参数,如加入ModelMap参数:
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public Map hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return model;
10.}
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public Map hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return model;
10.}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public Map hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return model;
}
这时,我们没有定义页面名称,Spring容器将根据请求名指定同名view,即如果是jap页面,则account.do->account.jsp!
不得不承认,这样写起来的确减少了代码量!
接着说输出参数!
通过ModelMap,我们可以绑定输出到的页面的参数,但最终我们将要返回到何种页面呢?再次查看AnnotationMethodHandlerAdapter源代码!
AnnotationMethodHandlerAdapter.java部分源代码——有关返回值部分:
Java代码
1.@SuppressWarnings("unchecked")
2.public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
3. ExtendedModelMap implicitModel, ServletWebRequest webRequest) {
4.
5. if (returnValue instanceof ModelAndView) {
6. ModelAndView mav = (ModelAndView) returnValue;
7. mav.getModelMap().mergeAttributes(implicitModel);
8. return mav;
9. }
10. else if (returnValue instanceof Model) {
11. return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
12. }
13. else if (returnValue instanceof Map) {
14. return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
15. }
16. else if (returnValue instanceof View) {
17. return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
18. }
19. else if (returnValue instanceof String) {
20. return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
21. }
22. else if (returnValue == null) {
23. // Either returned null or was 'void' return.
24. if (this.responseArgumentUsed || webRequest.isNotModified()) {
25. return null;
26. }
27. else {
28. // Assuming view name translation...
29. return new ModelAndView().addAllObjects(implicitModel);
30. }
31. }
32. else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
33. // Assume a single model attribute...
34. ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
35. String attrName = (attr != null ? attr.value() : "");
36. ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);
37. if ("".equals(attrName)) {
38. Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
39. attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
40. }
41. return mav.addObject(attrName, returnValue);
42. }
43. else {
44. throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
45. }
46.}
Java代码
1.@SuppressWarnings("unchecked")
2.public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
3. ExtendedModelMap implicitModel, ServletWebRequest webRequest) {
4.
5. if (returnValue instanceof ModelAndView) {
6. ModelAndView mav = (ModelAndView) returnValue;
7. mav.getModelMap().mergeAttributes(implicitModel);
8. return mav;
9. }
10. else if (returnValue instanceof Model) {
11. return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
12. }
13. else if (returnValue instanceof Map) {
14. return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
15. }
16. else if (returnValue instanceof View) {
17. return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
18. }
19. else if (returnValue instanceof String) {
20. return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
21. }
22. else if (returnValue == null) {
23. // Either returned null or was 'void' return.
24. if (this.responseArgumentUsed || webRequest.isNotModified()) {
25. return null;
26. }
27. else {
28. // Assuming view name translation...
29. return new ModelAndView().addAllObjects(implicitModel);
30. }
31. }
32. else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
33. // Assume a single model attribute...
34. ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
35. String attrName = (attr != null ? attr.value() : "");
36. ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);
37. if ("".equals(attrName)) {
38. Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
39. attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
40. }
41. return mav.addObject(attrName, returnValue);
42. }
43. else {
44. throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
45. }
46.}
@SuppressWarnings("unchecked")
public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
ExtendedModelMap implicitModel, ServletWebRequest webRequest) {
if (returnValue instanceof ModelAndView) {
ModelAndView mav = (ModelAndView) returnValue;
mav.getModelMap().mergeAttributes(implicitModel);
return mav;
}
else if (returnValue instanceof Model) {
return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
}
else if (returnValue instanceof Map) {
return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
}
else if (returnValue instanceof View) {
return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
}
else if (returnValue instanceof String) {
return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
}
else if (returnValue == null) {
// Either returned null or was 'void' return.
if (this.responseArgumentUsed || webRequest.isNotModified()) {
return null;
}
else {
// Assuming view name translation...
return new ModelAndView().addAllObjects(implicitModel);
}
}
else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
// Assume a single model attribute...
ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
String attrName = (attr != null ? attr.value() : "");
ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);
if ("".equals(attrName)) {
Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
}
return mav.addObject(attrName, returnValue);
}
else {
throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
}
}
}
返回值的定义十分庞大,或者说可怕的if-else多少有点让我觉得厌恶!
我们可以定义以下类型的返回值:
引用
ModelAndView
Model
View
Map
String
null
ModelAndView、Model和View都是Spring之前版本所特有的元素,Map对应于传入参数ModelMap,String定义页面名称,null即对应void类型方法!
最常用的实现方式如下:
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public String hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return "account";
10.}
Java代码
1.@SuppressWarnings("unchecked")
2.@RequestMapping(method = RequestMethod.GET)
3.public String hello(String username, String password, ModelMap model) {
4.
5. System.out.println(accountService.verify(username, password));
6.
7. model.put("msg", username);
8.
9. return "account";
10.}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public String hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return "account";
}
当然,对于我来说在返回值中写入这么一个字符串多少有点不能接受,于是我还是乐于使用输入参数ModelMap+输出参数Map的方式。
给出一个完整的AccountController实现:
AccountController.java
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import java.util.Map;
7.
8.import org.springframework.beans.factory.annotation.Autowired;
9.import org.springframework.stereotype.Controller;
10.import org.springframework.ui.ModelMap;
11.import org.springframework.web.bind.annotation.RequestMapping;
12.import org.springframework.web.bind.annotation.RequestMethod;
13.import org.zlex.spring.service.AccountService;
14.
15./**
16. *
17. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
18. * @version 1.0
19. * @since 1.0
20. */
21.@Controller
22.@RequestMapping("/account.do")
23.public class AccountController {
24.
25. @Autowired
26. private AccountService accountService;
27.
28. @SuppressWarnings("unchecked")
29. @RequestMapping(method = RequestMethod.GET)
30. public Map hello(String username, String password, ModelMap model) {
31.
32. System.out.println(accountService.verify(username, password));
33.
34. model.put("msg", username);
35. return model;
36. }
37.}
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import java.util.Map;
7.
8.import org.springframework.beans.factory.annotation.Autowired;
9.import org.springframework.stereotype.Controller;
10.import org.springframework.ui.ModelMap;
11.import org.springframework.web.bind.annotation.RequestMapping;
12.import org.springframework.web.bind.annotation.RequestMethod;
13.import org.zlex.spring.service.AccountService;
14.
15./**
16. *
17. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
18. * @version 1.0
19. * @since 1.0
20. */
21.@Controller
22.@RequestMapping("/account.do")
23.public class AccountController {
24.
25. @Autowired
26. private AccountService accountService;
27.
28. @SuppressWarnings("unchecked")
29. @RequestMapping(method = RequestMethod.GET)
30. public Map hello(String username, String password, ModelMap model) {
31.
32. System.out.println(accountService.verify(username, password));
33.
34. model.put("msg", username);
35. return model;
36. }
37.}
/**
* 2010-1-23
*/
package org.zlex.spring.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
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.zlex.spring.service.AccountService;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping("/account.do")
public class AccountController {
@Autowired
private AccountService accountService;
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public Map hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return model;
}
}
最后说注解@Session
如果想将某个ModelMap中的参数指定到Session中,可以使用@Session注解,将其绑定为Session熟悉,代码如下所示:
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.@SessionAttributes("msg")
4.public class AccountController {
5.
6. @Autowired
7. private AccountService accountService;
8.
9. @SuppressWarnings("unchecked")
10. @RequestMapping(method = RequestMethod.GET)
11. public Map hello(String username, String password, ModelMap model) {
12.
13. System.out.println(accountService.verify(username, password));
14.
15. model.put("msg", username);
16. return model;
17. }
18.
19.}
Java代码
1.@Controller
2.@RequestMapping("/account.do")
3.@SessionAttributes("msg")
4.public class AccountController {
5.
6. @Autowired
7. private AccountService accountService;
8.
9. @SuppressWarnings("unchecked")
10. @RequestMapping(method = RequestMethod.GET)
11. public Map hello(String username, String password, ModelMap model) {
12.
13. System.out.println(accountService.verify(username, password));
14.
15. model.put("msg", username);
16. return model;
17. }
18.
19.}
@Controller
@RequestMapping("/account.do")
@SessionAttributes("msg")
public class AccountController {
@Autowired
private AccountService accountService;
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET)
public Map hello(String username, String password, ModelMap model) {
System.out.println(accountService.verify(username, password));
model.put("msg", username);
return model;
}
}
当然,我们还需要配置一下对应的视图解析器,给出完整配置:
servelt.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xmlns:context="http://www.springframework.org/schema/context"
7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9. <context:component-scan
10. base-package="org.zlex.spring.controller" />
11. <bean
12. id="urlMapping"
13. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
14. <bean
15. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
16. <bean
17. id="jstlViewResolver"
18. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
19. p:viewClass="org.springframework.web.servlet.view.JstlView"
20. p:prefix="/WEB-INF/page/"
21. p:suffix=".jsp" />
22.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xmlns:context="http://www.springframework.org/schema/context"
7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9. <context:component-scan
10. base-package="org.zlex.spring.controller" />
11. <bean
12. id="urlMapping"
13. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
14. <bean
15. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
16. <bean
17. id="jstlViewResolver"
18. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
19. p:viewClass="org.springframework.web.servlet.view.JstlView"
20. p:prefix="/WEB-INF/page/"
21. p:suffix=".jsp" />
22.</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="org.zlex.spring.controller" />
<bean
id="urlMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
id="jstlViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/page/"
p:suffix=".jsp" />
</beans>
这里使用了JstlView作为视图解析器。同时,指定前缀路径为"/WEB-INF/page/",后缀路径为".jsp"。也就是说,Spring容器将会在这个路径中寻找匹配的jsp文件!
注意加入xmlns:p="http://www.springframework.org/schema/p"命名空间!
再给出页面内容:
taglib.jsp
Jsp代码
1.<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2.<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
3.<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
4.<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
5.<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
6.<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
7.<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
Jsp代码
1.<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2.<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
3.<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
4.<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
5.<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
6.<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
7.<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
account.jap
Jsp代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ include file="/WEB-INF/page/taglib.jsp"%>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>Account</title>
9.</head>
10.<body>
11.<c:out value="${msg}"></c:out>
12.</body>
13.</html>
Jsp代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ include file="/WEB-INF/page/taglib.jsp"%>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>Account</title>
9.</head>
10.<body>
11.<c:out value="${msg}"></c:out>
12.</body>
13.</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Account</title>
</head>
<body>
<c:out value="${msg}"></c:out>
</body>
</html>
目录结构如图所示:
启动应用,最后将得到一个带有内容的页面,如图:
代码见附件!
- spring注解2.rar (2.6 MB)
- 下载次数: 152
相关推荐
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
IT学习者Spring MVC注解实例.pdf
在提供的压缩包文件"spring_10"中,很可能包含了使用了上述注解的示例代码。这些代码可能包括了配置类、实体类、服务类和控制器类。通过阅读源代码,你可以看到如何使用注解来声明和注入bean。运行这个例子,观察...
本实例将深入探讨Spring MVC中常见的注解及其应用。 1. `@Controller` 注解:这个注解用于标记一个类作为Spring MVC的控制器。当Spring容器启动时,会扫描带有@Controller的类,并将这些类实例化,用于处理HTTP请求...
本实例将深入探讨Spring中的注解使用,特别是如何创建一个最简单的Spring注解实例。 首先,我们需要了解Spring的核心组件——Spring容器,也称为ApplicationContext。这个容器负责管理应用程序中的bean,包括它们的...
下面我们将深入探讨"非注解SpringMVC+Spring+Hibernate入门实例"中的关键知识点。 首先,让我们从SpringMVC开始。SpringMVC是一个Model-View-Controller架构模式的实现,用于构建Web应用程序。在非注解方式下,我们...
在这个注解实例中,我们将深入探讨Spring MVC中的核心注解以及如何利用它们实现对数据库表的增删改查(CRUD)操作。拦截器是Spring MVC中的另一个重要概念,它允许我们在请求处理前后执行自定义逻辑。 首先,让我们...
在这个实例中,我们将讨论如何使用注解来配置 Spring MVC。 首先,我们需要在 `web.xml` 文件中进行基本的配置。这个文件是 Java Web 应用的标准部署描述符,用来定义应用的启动和运行规则。在给出的 `web.xml` ...
对spring mvc注解的实例
在本项目实例中,我们探讨的是如何使用Spring MVC框架结合注解进行开发,以及如何实现对数据库表的CRUD(创建、读取、更新、删除)操作。Spring MVC是Spring框架的一部分,它专注于Web应用程序的模型-视图-控制器...
spring3整合EhCache注解实例
在Spring框架中,注解是实现依赖注入和配置的核心机制之一。本文将深入探讨Spring注解的基本原理,包括它们如何被解析、处理以及如何影响应用程序的生命周期。 首先,我们需要了解注解在Java语言中的本质。注解是一...
**Spring AOP 注解例子详解** 在 Spring 框架中,面向切面编程(Aspect Oriented Programming,AOP)是一种强大的设计模式,它允许我们分离关注点,将业务逻辑与系统服务(如日志、事务管理等)解耦。在 Spring AOP...
# Spring 3 注解详解 在 Spring 3 中,注解成为了主要的配置方式,极大地简化了应用程序的配置。本文将详细介绍几个关键的注解,包括 @Autowired、@Qualifier、@Resource 和 @PostConstruct,以及它们在实际开发中...
在Spring框架中,自动检测注解(Autowired)是核心特性之一,它极大地简化了依赖注入的过程,使得开发者能够更加专注于业务逻辑,而不是繁琐的配置。本文将深入探讨Spring中的自动检测注解及其工作原理。 首先,`@...
本篇文章将深入探讨如何通过Spring的注解方式实现AOP的细节。 首先,我们需要了解AOP的基本概念。AOP的核心是切面(Aspect),它封装了跨越多个对象的行为或责任。切点(Pointcut)定义了哪些方法会被通知(Advice...
本教程将围绕"Spring3注解初级实例"进行详细讲解,帮助初学者了解如何在Spring3中使用注解进行开发。 首先,让我们了解一下什么是注解(Annotation)。注解是Java编程语言的一种元数据,它可以提供有关代码的额外...
#### 二、Spring中的Java注解 Spring框架充分利用了Java注解的功能,简化了配置过程,并提供了许多内置的注解来支持依赖注入、事务管理等功能。以下是一些常用的Spring注解: 1. **@Component** - 用于标记一个类...
SSH是Java Web开发中的一个经典框架组合,包括Struts、Hibernate和Spring三个核心组件。这个框架结合了MVC设计模式、对象关系映射以及依赖注入等技术,为开发者提供了高效且灵活的开发环境。 **Struts** 是一个开源...
在这个实例中,我们将重点讨论如何使用Spring MVC的注解进行数据的增删改操作,并结合Hibernate作为ORM(对象关系映射)工具。 首先,`@Controller`注解标记了一个类作为Spring MVC的控制器,负责处理HTTP请求。在...