First, create a new controller
PackageCn.cfs.springmvc.service;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportOrg.springframework.stereotype.Service;ImportCn.cfs.springmvc.domain.User; @Service Public classUserService {Private StaticMap<integer, user> map =NewHashmap<integer, user>(); Static{User User=NULL; for(inti = 1; I < 11; i++) {User=NewUser (); User.setid (i); User.setusername ("Cifeng" +i); User.setpassword ("88888888"); Map.put (i, user); } } PublicList<user>list () {return NewArraylist<user>(Map.values ()); }}
UserService
PackageCn.cfs.springmvc.controller;ImportJava.util.Map;ImportJavax.annotation.Resource;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportCn.cfs.springmvc.service.UserService; @Controller @requestmapping (Value={"/user"}) Public classUsercontroller {@ResourcePrivateuserservice us; @RequestMapping (Value={"/list"}) PublicString List (map<string,object>model) { //Preparing DataModel.put ("Users", Us.list ()); return"Juserlist"; } @RequestMapping (Value={"/list2"}) PublicString listbyattr (model model) {//Preparing Datamodel.addallattributes (Us.list ()); return"Juserlist"; } }
Usercontroller
Second, the JSP page to get the object deposited
<%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Inquire</title></Head><Body> <Table> <TR> <TD>Id</TD> <TD>Name</TD> <TD>Password</TD> </TR> <C:foreachItems= "${users}"var= "U"> <TR> <TD>${u.id}</TD> <TD>${u.username}</TD> <TD>${u.password}</TD> </TR> </C:foreach> </Table> </Body></HTML>
Note: The following pages can be obtained by accessing/user/list.action:
But what if we can get the value by/user/list2.action?
You only need to change the value of items to UserList, which is a convention, with an incoming entity object +list For example, if I pass in a list<user> collection, the entity class is 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Inquire</title></Head><Body> <Table> <TR> <TD>Id</TD> <TD>Name</TD> <TD>Password</TD> </TR> <C:foreachItems= "${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, the page effect is the same as above.
Springmvc Basics-Using annotations to provide data to the foreground