[学习笔记]基于注解的spring3.0.x MVC学习笔记(四)

好久没有更新关于spring3.0.x的学习笔记了,这笔记除了平时工作上用的代码之外还有部分代码是根据我的老师学习下来后我再重新理解所整理的,接着上期第三部分的内容:

上次写到了requestMapping对返回集合的用法,使用集合返回的话则需要根据requestmapping中values的值进行命名jsp页面,而整个路径地址是根据视图模型的返回地址来确定.这次主要介绍的是返回类型为Model,List.Collection,Set,Object,先看返回类型为Model,代码如下:

1 /** 2 * @see 返回一个Model接口.通过创建ExtendedModelMap可以使用,方法同modelMap一样 3 * */ 4 @RequestMapping("/model") 5 public Model model() { 6 Model model = new ExtendedModelMap(); 7 model.addAllAttributes(temp()); 8 return model; 9 10 }

temp方法代码如下:

   1:  /**
   2:       * @see 临时类
   3:       * @return 返回一个map类型
   4:       * */
   5:      public Map<String, UserBean> temp() {
   6:          Map<String, UserBean> map1 = new HashMap<String, UserBean>();
   7:          UserBean bean = new UserBean();
   8:          bean.setId(1);
   9:          bean.setUsername("Edward Lau");
  10:          bean.setPassword("edward");
  11:          map1.put("user", bean);
  12:          // map1.put("hello", "world key");
  13:          UserBean bean1 = new UserBean();
  14:          bean1.setId(2);
  15:          bean1.setUsername("Edward Lau2");
  16:          bean1.setPassword("edward");
  17:          map1.put("user1", bean1);
  18:          System.out.println(map1);
  19:          return map1;
  20:      }

通过model的addAllAttributes可以把一个map集合的内容放到model对象中,通过jsp中读取指定的数据,相当于读取request.getAttributes一样

jsp代码如下图:

D463C593-A6ED-4494-B3C1-2D073C302433

效果如下图:

615314E5-6D15-4778-A1F2-BD9B8E58E56D

对于List,Collection,Set这3种类型返回的方式都是根据requestmapping的value标记进行读取指定页面,但是在jsp页面中读取方式值的方式相对有些不同,首先是对List:

对于List代码只是把返回类型改为了List之外,在jsp中我们需要根据代码第一个add的数据类型进行保存,代码如下:

   1:  /**
   2:       * @see 返回集合类型的默认名称 默认值为: 第一个存入对象的Object.getClass().getSimpleName()+List组成
   3:       * @see 返回list集合的话可以使用request.getAttribute(默认值)取出值,或者使用el 表达式${默认值}
   4:       *      <b>如果需要取某一个序列的话可以使用${默认值[下标]}</b>
   5:       * */
   6:      @RequestMapping("/list")
   7:      public List listReturn() {
   8:          List toys = new ArrayList();
   9:          toys.add("gg5555555");
  10:          toys.add(new UserBean(1, "a", "b"));
  11:          toys.add(new UserBean(2, "PSP", "2100"));
  12:   
  13:          return toys;
  14:      }

对应的jsp如下图:

F7BD8C33-7944-43D2-A3E5-B1AA82AE764D

其中1显示的是集合第1个元素的内容,从代码上看下标元素为1的对象是UserBean,但是在前台调用request.getAttribute方法是根据list第一个add元素的clasas类型的simplename+上集合类型名字组成的,效果如下图:

67B347B8-B203-4072-8A0D-DE2AA5EC02A9

对于Set来说,由于Set也是属于集合的一种所以读取方式也是根据List一样.

对于Collection他则有点特别他是根据返回所属Collection的类型来决定的,代码如下:

   1:  @RequestMapping("/collection")
   2:      public Collection collection() {
   3:          logger.info("collection is runing");
   4:  //        return temp().values();
   5:          List toys = new ArrayList();
   6:          toys.add("gg5555555");
   7:          toys.add(new UserBean(1, "a", "b"));
   8:          toys.add(new UserBean(2, "PSP", "2100"));
   9:   
  10:          return Collections.singletonList(toys);
  11:      }

如果返回的集合类型是list的话该Collection而没有定义泛型的话则以返回的list的实例对象作为命名的model type则arrayList+List,对应的jsp如下:

   1:  <%@ page language="java" contentType="text/html; charset=UTF-8"
   2:      pageEncoding="UTF-8"%>
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
   4:  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   5:   
   6:  <%@page import="java.util.List"%><html>
   7:  <head>
   8:  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   9:  <title>Collection</title>
  10:  </head>
  11:  <body>
  12:   
  13:  姓名:${arrayListList}
  14:  <c:forEach var="user" items="${arrayListList}"><br>
  15:  ${user[0] }<br>
  16:  ${user[1].username }<br>
  17:  ${user[1].password }<br>
  18:  </c:forEach>
  19:   
  20:   
  21:  </body>
  22:  </html>

这样可以得到结果如下图:

D225E1E4-08EC-426D-B98F-090C54B139A5

如果返回类型是List并且有定义泛型的话则以泛型名字+List进行返回,Set集合效果同List一样.

对于返回Object类型的话则按照创新的对象类型返回指定的model type 代码如下:

   1:  /**
   2:       * @see 返回对象在页面中以${返回对象名称}取值,返回
   3:       * @return Object 返回对象的类型,如返回Userbean则输入返回的对象为Userbean
   4:       * */
   5:      @RequestMapping("/object")
   6:      public Object object() {
   7:          return new UserBean(1, "a", "b");
   8:      }

对应的jsp与效果如下图:

35C6F509-3EBD-4B87-960D-2E81C8D68B91

6646B2B0-7A9B-4EEA-9720-F03D535061C8

posted @ 2011-01-03 23:42  EdwardLau  阅读(5913)  评论(1编辑  收藏  举报