SpringMVC(十五) RequestMapping map模型数据

SpringMVC中Map模型数据传递
本文介绍了一个具体的SpringMVC案例,展示了如何通过控制器中的Map模型将数据从后台传递到视图,并在视图中展示这些数据。具体包括创建用户对象及其地址信息,并将这些信息存入Map模型中,最后在页面上显示。

控制器中使用map模型数据,传送数据给视图。

 

控制器参考代码:

package com.tiekui.springmvc.handlers;

import java.util.Arrays;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
import com.tiekui.springmvc.pojo.Address;
import com.tiekui.springmvc.pojo.User;

@Controller
public class MapModel {

    @RequestMapping("testMapModel")
    public String testMapModel(Map<String, Object> map) {
        User userTk = new User();
        Address address = new Address();
        address.setCity("city");
        address.setProvince("province");
        userTk.setAge(19);
        userTk.setEmail("zhoutiekui@huawei.com");
        userTk.setPassword("test");
        userTk.setUsername("zhoutiekui");
        userTk.setAddress(address);
        
        map.put("user", userTk);
        map.put("names", Arrays.asList("zhoutiekui","chaoer"));
        
        return "mapView";
    }
    
    
}

返回视图参考代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


    user: ${requestScope.user} 
    names: ${requestScope.names} 

</body>
</html>

 

调用视图参考代码:

<a href="testMapModel">testMapModel video 15</a>

 

转载于:https://www.cnblogs.com/zhoutiekui/p/6349987.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值