본문 바로가기

spring

spring+ibatis+jsp+jstl

springMVC를 사용하지 않고 jsp에서 Bean을 직접 호출함..

이런걸 왜 만들었을까? 시키니까..ㅡ.ㅡ;


<?xml version="1.0" encoding="UTF-8" ?>

<jsp:root

xmlns="http://www.w3c.org/1999/xhtml"

xmlns:jsp="http://java.sun.com/JSP/Page"

xmlns:c="http://java.sun.com/jsp/jstl/core"

version="2.0">

 

<jsp:directive.page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" />

<jsp:directive.page import="java.util.Collection"/>

<jsp:directive.page import="java.util.HashMap"/>

<jsp:directive.page import="java.util.ArrayList"/>

<jsp:directive.page import="org.springframework.context.ApplicationContext"/>

<jsp:directive.page import="org.springframework.web.context.support.WebApplicationContextUtils"/>

<jsp:directive.page import="org.springframework.context.support.ClassPathXmlApplicationContext"/>

<jsp:directive.page import="org.springframework.context.support.FileSystemXmlApplicationContext"/>

 

<jsp:directive.page import="kr.co.dsic.sample.service.SampleService"/>

 

<jsp:text>

           <![CDATA[ <?xml version="1.0" encoding="UTF-8" ?> ]]>

</jsp:text>

<jsp:text>

           <![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>

</jsp:text>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>JSP+SPRING</title>

</head>

<body>

<jsp:scriptlet>

         

           //컨텍스트 가져옴..

           //ApplicationContext context = new ClassPathXmlApplicationContext("spring-application.xml");

 

           //ApplicationContext context = new FileSystemXmlApplicationContext(getServletContext().getRealPath("/WEB-INF")+"/context/spring-application.xml");

          

           ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

          

          

           //서비스 빈 취득..

           SampleService ss = (SampleService) context.getBean("sampleService");

 

           ArrayList paramterList = new ArrayList();

           ArrayList resultList = new ArrayList();

          

           HashMap hm = new HashMap();

           hm.put("search1", "");

           paramterList.add(hm);

          

           System.out.println("*********paramterList : "+hm.toString());

          

           //빈 메소드 실행..

           resultList = (ArrayList)ss.getSelectList(paramterList, "USER_MAIN.SELECT");

          

           request.setAttribute("resultList", resultList);

          

           for (Object o : resultList) {

                     System.out.println("*********resultList : "+((HashMap)o).toString());

           }

          

</jsp:scriptlet>

 

<c:forEach items="${resultList}" var="result" varStatus="i">

           <div>

                     <c:out value="${i.count}"/>.

                     <c:out value="${result['USER_MAIN_SEQ']}" />

                     <c:out value="${result['USER_MAIN_NAME']}" />

                     <c:out value="${result['USER_MAIN_DESC']}" />

                     <c:out value="${result['USER_MAIN_ID']}" />

                     <c:out value="${result['USER_MAIN_REG_DATE']}" />

           </div>

</c:forEach>

 

</body>

</html>

</jsp:root>

 

다음은 이용된 코드..대충...정리..

 

SampleService.java

 

           public Collection getSelectList(ArrayList acList, String map) {

 

                     HashMap hm = new HashMap();

                     hm = (HashMap) acList.get(0);

                     ArrayList arrResult = (ArrayList) commonDao.getClobSelect(hm, map);

 

                     return arrResult;

           }

 

CommonDao.java

 

           public Collection getClobSelect(HashMap hm, String map) throws DataAccessException {

 

                     ArrayList arrResult = null;

                     arrResult = (ArrayList) getSqlMapClientTemplate().queryForList(map, hm);

 

                     for(int i=0;i<arrResult.size();i++){

                    hm = (HashMap)arrResult.get(i);

                    Iterator iter = hm.entrySet().iterator();

                    while (iter.hasNext()) {

                Map.Entry entry = (Map.Entry)iter.next();

                if (entry.getValue() !=null && (entry.getValue() instanceof java.sql.Clob) ) {

                  entry.setValue(this.getStringForClob((Clob)entry.getValue()));

                }

            }

          }

                     return arrResult;

           }

 

Spring 설정

 

           <!-- SqlMap -->

           <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

                     <property name="configLocation" value="WEB-INF/context/ibatis-common.xml"/>

                     <!--property name="configLocation" value="classpath:/ibatis-common.xml"/-->          

                     <property name="dataSource" ref="oracleDataSource"/>

           </bean>

 

 

           <!-- Common Data Access Object -->

           <bean id="commonDao" class="kr.co.xxxx.common.dao.CommonDao">

                     <property name="sqlMapClient" ref="sqlMapClient" />

           </bean>

 

 

           <!-- Samele Service -->

           <bean id="sampleService" class="kr.co.xxxx.sample.service.SampleService">

                     <property name="commonDao" ref="commonDao" />

           </bean>