JavaBean集合对象注入属性学习篇

JavaBean集合对象注入属性

下面是 bean集合类

//定义了Map,set,list
public class setys 
{
	private Map<String,String> map;
	private Set<String> set;
	private List<String> list;
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public Set<String> getSet() {
		return set;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
}

下面是 xml的配置(初始化集合数据)

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans  
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	
	<bean id="setys" class="com.lh.util.setys">
		<property name="map">
			<map>
				<entry key="1" value="Java"></entry>
				<entry key="2" value="C++"></entry>
				<entry key="3" value="Python"></entry>
			</map>
		</property>
		<property name="set">
			<set>
				<value>LOL</value>
				<value>DOTA</value>
			</set>
		</property>
		
		<property name="list">
			<list>
				<value></value>
				<value></value>
			</list>
		</property>
		
	</bean>
	</beans>

下面是 前端代码(采用JSTL库中的JSON输出)

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page  import="org.springframework.core.io.*"%>
<%@ page  import="org.springframework.beans.factory.BeanFactory"%>
<%@ page  import="org.springframework.beans.factory.xml.XmlBeanFactory"%>
<%@ page  import="org.springframework.context.ApplicationContext"%>
<%@ page  import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@ page import="com.lh.entity.*"%>
<%@ page import="com.lh.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<base href="<%=basePath%>">
 <title>为JavaBean的集合对象注入属性值</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
<body>
<%
  		ApplicationContext context=new ClassPathXmlApplicationContext("one.xml");//获取ApplicationContext容器	
  		setys setys1 = (setys) context.getBean("setys");		// 指定Bean的名称来取得Bean实例				
 %>
 
 <center>
   <br>输出list集合中的元素:<br><br>
   <c:forEach var="list" items="<%=setys1.getList() %>">
   		${list}<br/>
   </c:forEach>
  <br> 输出set集合中的元素:<br><br>
   <c:forEach var="set" items="<%=setys1.getSet() %>">
   		${set}<br/>
   </c:forEach>
  <br> 输出map集合中的元素:<br><br>
   <c:forEach var="map" items="<%=setys1.getMap() %>">
   		${map.value}<br/>
   </c:forEach>
   </center>
</body>
</html>

下面是前端打印出的集合值。
在这里插入图片描述
奥利给。

原创文章 24 获赞 0 访问量 1243

猜你喜欢

转载自blog.csdn.net/FYM1211/article/details/105290355
今日推荐