springmvc基础篇—使用注解方式为前台提供数据
2024-09-29 03:31:02
一、新建一个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,页面效果是和上面一样的。
最新文章
- Encountered an unexpected error when attempting to resolve tag helper directive &#39;@addTagHelper&#39; with value &#39;";*, Microsoft.AspNet.Mvc.TagHelpers";&#39;
- 1.UI初认识
- 每天一个linux命令(28):tar命令
- IOS高级编程之三:IOS 多线程编程
- Json字符串反序列化
- 关于ubuntu的sources.list总结
- [转]深入理解学习GIT工作流
- BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler错误的解决方法
- php 支持断点续传的文件下载类
- FZU 2082 过路费 (树链剖分 修改单边权)
- Hadoop学习历程(三、第一个程序)
- MVC 5 的 EF6 Code First 入门 系列:排序、筛选和分页
- HDU3371--Connect the Cities(最小生成树)
- HDU 5621 KK&#39;s Point
- JS解决通过按钮切换图片的问题
- vijos1080题解
- 热门开源项目:Guns-后台管理系统
- gitlab 注册runner
- Ubuntu16.04 git上网速度慢的解决方法.
- mysql官方驱动jar