一、新建一个Controller

package cn.cfs.springmvc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import cn.cfs.springmvc.domain.User; @Service
public class UserService {
private static Map<Integer, User> map = new HashMap<Integer, User>();
static {
User user = null;
for (int i = 1; i < 11; i++) {
user = new User();
user.setId(i);
user.setUsername("cifeng" + i);
user.setPassword("88888888");
map.put(i, user);
}
} public List<User> list() {
return new ArrayList<User>(map.values());
} }

UserService

package cn.cfs.springmvc.controller;

import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.cfs.springmvc.service.UserService; @Controller
@RequestMapping(value={"/user"})
public class UserController {
@Resource
private UserService us; @RequestMapping(value={"/list"})
public String list(Map<String,Object> model){
//准备数据
model.put("users", us.list());
return "jUserList";
} @RequestMapping(value={"/list2"})
public String listByAttr(Model model){
//准备数据
model.addAllAttributes(us.list());
return "jUserList";
} }

UserController

二、jsp页面获取存入的对象

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>查询</title>
</head>
<body>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>password</td>
</tr>
<c:forEach items="${users }" var="u">
<tr>
<td>${u.id }</td>
<td>${u.username }</td>
<td>${u.password }</td>
</tr>
</c:forEach>
</table> </body>
</html>

注意:通过访问/user/list.action 可以得到如下页面:

但是如果通过/user/list2.action该如何取值呢?

只需要将items的值users 换成userList即可,这是一种约定,用传入的实体对象+List即可 例如我传入的是list<User> 集合 那么 实体类是user+List

<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>查询</title>
</head>
<body>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>password</td>
</tr>
<c:forEach items="${userList }" var="u">
<tr>
<td>${u.id }</td>
<td>${u.username }</td>
<td>${u.password }</td>
</tr>
</c:forEach>
</table> </body>
</html>

/user/list2

ok,页面效果是和上面一样的。

最新文章

  1. Encountered an unexpected error when attempting to resolve tag helper directive &#39;@addTagHelper&#39; with value &#39;&quot;*, Microsoft.AspNet.Mvc.TagHelpers&quot;&#39;
  2. 1.UI初认识
  3. 每天一个linux命令(28):tar命令
  4. IOS高级编程之三:IOS 多线程编程
  5. Json字符串反序列化
  6. 关于ubuntu的sources.list总结
  7. [转]深入理解学习GIT工作流
  8. BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler错误的解决方法
  9. php 支持断点续传的文件下载类
  10. FZU 2082 过路费 (树链剖分 修改单边权)
  11. Hadoop学习历程(三、第一个程序)
  12. MVC 5 的 EF6 Code First 入门 系列:排序、筛选和分页
  13. HDU3371--Connect the Cities(最小生成树)
  14. HDU 5621 KK&#39;s Point
  15. JS解决通过按钮切换图片的问题
  16. vijos1080题解
  17. 热门开源项目:Guns-后台管理系统
  18. gitlab 注册runner
  19. Ubuntu16.04 git上网速度慢的解决方法.
  20. mysql官方驱动jar

热门文章

  1. [SCOI2014]方伯伯运椰子
  2. [POI2008]STA-Station
  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ&#39;s Salesman 【离散化+树状数组维护区间最大值】
  4. 2018.12.14 Mac的Matlab2018a软件及其安装步骤
  5. Angular 引用库
  6. 【luogu P3373 线段树2】 模板
  7. 【题解】洛谷P3399 丝绸之路
  8. PLSQL 禁用所有约束,启用约束,索引,触发器等
  9. Spring-boot官方案例分析之data-jpa
  10. CRS