springmvc基础篇—使用注解方式为前台提供数据

一、新建一个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,页面效果是和上面一样的。

posted @ 2017-02-05 15:11  刺风  阅读(376)  评论(0编辑  收藏  举报