SPRINGMVC Basics-use annotations to provide data to the foreground

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.