2008/Spring 2.52008. 9. 20. 17:12
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- the test application context definition for the jdbc based tests -->

    <bean id="productDao" class="springapp.repository.JdbcProductDao">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}"/>
      <property name="url" value="${jdbc.url}"/>
      <property name="username"  value="${jdbc.username}"/>
      <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>
   
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

=================================
Document path
C:\spring\util\apache-tomcat-5.5.27\webapps\tomcat-docs\index.html

8) JNDI Resources



4. Configure Tomcat's Resource Factory

To configure Tomcat's resource factory, add an elements like this to the $CATALINA_HOME/conf/server.xml file, nested inside the Context element for this web application.

<Context ...>
  ...
  <Resource name="bean/MyBeanFactory" auth="Container"
            type="com.mycompany.MyBean"
            factory="com.mycompany.MyBeanFactory"
            bar="23"/>
  ...
</Context>

Note that the resource name (here, bean/MyBeanFactory must match the value specified in the web application deployment descriptor. We are also initializing the value of the bar property, which will cause setBar(23) to be called before the new bean is returned. Because we are not initializing the foo property (although we could have), the bean will contain whatever default value is set up by its constructor.


tomcat Server.xml 설정
<GlobalNamingResources>
 <Resource name="jdbc/SpringDB" auth="Container"
            type="javax.sql.DataSource" username="spring" password="spring123"
            driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:XE"
            maxActive="8" maxIdle="4"/>

</GlobalNamingResources> 



맨밑에
 <Context docBase="spring_1"
      path="/spring_1"
      reloadable="true"
      source="org.eclipse.jst.j2ee.server:spring_1">
      <ResourceLink name="jdbc/SpringDB"
            global="jdbc/SpringDB"
            type="javax.sql.DataSource"/>
      </Context>



설정 하고 나서 Test_jndi.jsp 만들어서 Test를 해봅시다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
   
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
   
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<% InitialContext ctx = new InitialContext();
   Object  obj = ctx.lookup("java:comp/env/jdbc/SpringDB");
  
   if(obj  != null)
   {
    javax.sql.DataSource ds = (javax.sql.DataSource)obj;
    
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    String sql = "SELECT * FROM BOARD ";
    ResultSet rs = stmt.executeQuery(sql);
    
    while(rs.next())
    {
     out.println(rs.getString(1) + "<br>");
    }
    stmt.close();
    conn.close();
    
   }
   else {
    out.println("검색 실패 !!");
   }
  
   %>

</body>
</html>

Posted by penguindori
2008/Spring 2.52008. 9. 20. 14:05

http://static.springframework.org/docs/Spring-MVC-step-by-step/part1.html

web.xml 추가

<servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern> -> *.do로 수정
  </servlet-mapping>



2.
C:\spring\util\spring-framework-2.5.5\dist
(1.7 step by step)

spring.jar
spring-webmvc.jar  드레그 해서 이클립스 Webcontent/WEB-INF/lib/에 넣는다.

--------------------
springapp-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet -->

  <bean name="/hello.htm" class="springapp.web.HelloController"/>

</beans>




<bean name="/hello.htm" class="springapp.web.HelloController"/>  요청 Url

<bean id="">  ID 공유할때는 ID ; 특스문자 사용 불가

<!-- http://localhost:8088/spring_1/hello.do -->
  <bean name="/hello.do" class="springapp.web.HelloController"/>




=======================================
1. springapp-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet -->
 <!-- http://localhost:8088/spring_1/hello.do -->
  <bean name="/hello.do" class="springapp.web.HelloController"/>
 //springapp.web.HelloController를 /hello.do로 매핑 합니다.

</beans>

2. HelloController.java class 만들기

package springapp.web;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;

import java.io.IOException;


public class HelloController implements Controller {
 @Override
 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
       throws Exception
 {
 
  ModelAndView mv = new ModelAndView();
  mv.setViewName("spring_hello.jsp");
  mv.addObject("greeting","첫번째 스프링 예제!!!");
  return mv;
 }
 
}

3. jsp Page만들기
spring_hello.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
 ${greeting}
</body>
</html>

4. index.htm
<html>
<head>
<script type="text/javascript">
<!--
 location.href="hello.do";
-->
</script>
</head>
<body>
</body>

</html>
----------- 그러면 나온다 ; 아 힘드네 ㅋㅋ


사용자 삽입 이미지













Posted by penguindori