zoukankan      html  css  js  c++  java
  • 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,页面效果是和上面一样的。

  • 相关阅读:
    BZOJ 2400: Spoj 839 Optimal Marks (按位最小割)
    bzoj4873: [Shoi2017]寿司餐厅(最大权闭合子图)
    bzoj1497: [NOI2006]最大获利(最大权闭合子图)
    bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
    bzoj1024: [SCOI2009]生日快乐
    bzoj2761: [JLOI2011]不重复数字
    bzoj1257: [CQOI2007]余数之和sum
    bzoj2456: mode
    bzoj1831: [AHOI2008]逆序对(DP+双精bzoj1786)
    bzoj2431: [HAOI2009]逆序对数列
  • 原文地址:https://www.cnblogs.com/mycifeng/p/6367697.html
Copyright © 2011-2022 走看看