반응형

2024_0716_SpringEx2_Collection [ADD] 20240716 Spring 객체 생성 후 출력하기

 

GitHub - chaSunil/FirstProject: 1차 프로젝트

1차 프로젝트. Contribute to chaSunil/FirstProject development by creating an account on GitHub.

github.com

 

 

 

 

 

메이븐 설정

 

 

 

생성 화면

 

 

 

이쪽 구역에 복사하기

 

 

  	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>6.0.0</version>
		</dependency>
		
		
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>6.0.0</version>
	</dependency>
	
	
	</dependencies>

 

 

 

라이브러리를 확인합니다

 

 

 

 

web. xml 복사해서 경로에 넣기

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>2024_0716_SpringEx2_Collection</display-name>
  
  
  	<!-- /////////////////// 스프링 설정 시작 /////////////////////////////////// -->
 	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>


	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

 

 

applicationContext.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-3.0.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 
</beans>

 

 

 

applicationContext.xml
0.00MB

 

 

 


 

 

 

 

Spring으로 ArrayList 만들어서 사용해보기

package myutil;

import java.util.List;

public class MyList {
	
	List sido_list;

	public List getSido_list() {
		return sido_list;
	}

	public void setSido_list(List sido_list) {
		this.sido_list = sido_list;
	}

}

 

 

 

 

 <!-- MyList 객체 생성코드 작성
      Setter Injection이용해서 sido_list 넣어준다
      MyList myListBean = new MyList();
 -->
 <bean id="myListBean" class="myutil.MyList">
 	<property name="sido_list">
 		<!-- List list = new ArrayList();
 		     myListBean.setSido_list(list);
 		 -->
 		<list>
 			<value>서울</value>
 			<value>경기</value>
 			<value>인천</value>
 			<value>대전</value>
 			<value>대구</value>
 		</list>
 	</property>
 </bean>
 
 
</beans>

 

 

 

 

Spring에서 만든 ArrayList 출력하기

<%@page import="java.util.List"%>
<%@page import="myutil.MyList"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
	WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);

	MyList myList = wac.getBean("myListBean", MyList.class);
	
	List sido_list = myList.getSido_list();
	
	pageContext.setAttribute("sido_list", sido_list);
%>

<hr>
	시도목록
<ht>
<ul>
	<!-- for(String sido : sido_list) -->
	<c:forEach var="sido" items="${ sido_list }">
		<li>${ sido }</li>
	</c:forEach>
</ul>

 

 

 

 

Set 만들어서 사용해보기

package myutil;

import java.util.Set;

public class MySet {
	
	Set<String> fruit_set;

	public Set<String> getFruit_set() {
		return fruit_set;
	}

	public void setFruit_set(Set<String> fruit_set) {
		this.fruit_set = fruit_set;
	}

}

 

 

 

 

Spring에서 Set 만들어주기(applicationContext.xml)

 <!-- MySet객체생성을 위한 코드작성 -->
 <bean id="mySetBean" class="myutil.MySet">
 	<property name="fruit_set">
 		<!-- new HashSet() -->
 		<set>
 			<value>사과</value>
 			<value>배</value>
 			<value>참외</value>
 			<value>멜론</value>
 			<value>포도</value>
 		</set>
 	</property>
 </bean>

 

 

 

 

MySetDisplay.jsp 만들어주기

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<% 
	WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);

	MySet mySet = wac.getBean("mySetBean", MySet.class);
	
	Set fruit_set = mySet.getFruit_set();
	
	pageContext.setAttribute("fruit_set", fruit_set);
%>

<hr>
	과일목록
<hr>
<ul>
	<c:forEach var="fruit" items="${ fruit_set }">
		<li>${ fruit }</li>
	</c:forEach>
</ul>

 

 

 

 

MyMap 만들기

package myutil;

import java.util.Map;

public class MyMap {
	
	Map<String, Object> wordMap;

	// Constructor Injection으로 주입받는다
	public MyMap(Map<String, Object> wordMap) {
		super();
		this.wordMap = wordMap;
	}

	public Map<String, Object> getWordMap() {
		return wordMap;
	}

	public void setWordMap(Map<String, Object> wordMap) {
		this.wordMap = wordMap;
	}

}

 

 

 

 

applicationContext에서 Spring을 통해 map 만들어주기

 <!-- MyMap 객체생성을 위한 코드작성 -->
 <bean id="myMapBean" class="myutil.MyMap">
 	<constructor-arg>
 		<!-- Map map = new HashMap(); -->
 		<map>
 			<!-- map.put("one","하나를 의미합니다.") -->
 			<entry key="one" value="하나를 의미합니다."/>
 			<entry key="two" value="둘을 의미합니다."/>
 			<entry key="three" value="셋을 의미합니다."/>
 			<entry key="four" value="넷을 의미합니다."/>
 			<entry key="five" value="다섯을 의미합니다."/>
 		</map>
 	</constructor-arg>
 </bean>

 

 

 

 

 

 Map을 불러와줄 jsp 만들기

<%@page import="java.util.Properties"%>
<%@page import="myutil.MyProp"%>
<%@page import="java.util.Map"%>
<%@page import="myutil.MyMap"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<% 
	WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);

	MyMap myMap = wac.getBean("myMapBean", MyMap.class);
	
	Map wordMap = myMap.getWordMap();
	
	pageContext.setAttribute("wordMap", wordMap);
	
	MyProp myProp = wac.getBean("myPropBean", MyProp.class);
	
	Properties dbProp = myProp.getDbProp();
	
	pageContext.setAttribute("dbProp", dbProp);
%>

<hr>
	영어학습
<hr>
<ul>
	<c:forEach var="word" items="${ wordMap }">
		<dt><b>${ word.key }</b></dt>
		<dd>${ word.value }</dd>
		<dd>${ word }</dd>
	</c:forEach>
</ul>

 

 

 

 

 

Spring을 사용해 Properties 가져오기

Vo 생성하기

package myutil;

import java.util.Properties;

public class MyProp {
	
	Properties dbProp;

	public Properties getDbProp() {
		return dbProp;
	}

	public void setDbProp(Properties dbProp) {
		this.dbProp = dbProp;
	}
	
}

 

 

 

 

applicationContext 생성하기

 <!-- MyProp 생성할 코드 작성 -->
 <bean id="myPropBean" class="myutil.MyProp">
 	<property name="dbProp">
 		<!-- Properties prop = new Properties() -->
 		<props>
 			<prop key="driver">oracle.jdbc.driver.OracleDriver</prop>
 			<prop key="url">jdbc:oracle:thin:@localhost:1521:xe</prop>
 			<prop key="username">scott</prop>
 			<prop key="password">tiger</prop>
 		</props>
 	</property>
 </bean>

 

 

 

 

jsp에 작성하기

<%@page import="java.util.Properties"%>
<%@page import="myutil.MyProp"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<% 
	WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);
	
	MyProp myProp = wac.getBean("myPropBean", MyProp.class);
	
	Properties dbProp = myProp.getDbProp();
	
	pageContext.setAttribute("dbProp", dbProp);
%>

<hr>
	Properties 클래스
<hr>
<ul>
	<c:forEach var="prop" items="${ dbProp }">
		<li>${ prop }</li>
	</c:forEach>
</ul>

반응형