본문 바로가기

JSP & WEB & Ajax

Struts + iBATIS 간단 예제

아이디와 패스워드를 입력받아 인증이 성공하면 success.html을 보여준다.(정말 간단하다ㅜ_ㅜ)

 

1. login.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 <%@ taglib uri = "/WEB-INF/tld/struts-html.tld" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
 <html:form action="/login.do">
  <html:hidden property="method" value="select" /> <!--여기서 헤맸다 ㅠ_ㅠ  -->   <table>
   <tr>
    <td>아이디</td>
    <td><html:text property="userid" value=""/></td>
   </tr>
   <tr>
    <td>패스워드</td>
    <td><html:password property="userpass" value=""/></td>
   </tr>
   <tr>
    <td><html:submit property="loginbtn" value="로그인" /></td>
    <td><html:reset value="취소"/></td>
   </tr>
  </table>
 </html:form>
</body>
</html>

 

=======설명========

<html:form action="/login.do"> --> struts-config.xml에서 /login 이라는 path와 매핑된다.

<html:hidden property="method" value="select" /> --> 여기가 가장 중요하다(내가 헤맸던 곳이므로 -0-ㅋ)

 

struts- config.xml에서 action mapping 하는 부분에 보면 parameter="method" 라는 부분이 있다.  파라미터에 정의한 인자(method)의 값(value)과  DispatchAction 클래스를 상속받아 구현한(실제로 여러메소드를 구현한)클래스의 메소드와 일치하는 메소드를 호출할 수 있다.

ex) method=select이면 DispatchAction을 상속받은 SelectAction의 select() 메소드가 호출된다.


 

 

2. struts-config.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
      
    <!-- ========== Form Bean Definitions ================================== -->
    <form-beans>
        <form-bean 
         name="SelectBeanForm" 
         type="com.bean.SelectBean">          
        </form-bean>        
    </form-beans>
   
    <!-- ========== Global Forward Definitions =============================== -->
    <global-forwards>
        <forward name="success" path="/success.html" />
        <forward name="fail" path="/fail.jsp" />
        <forward name="error" path="/error.jsp" />
    </global-forwards>

     
    <!-- ========== Action Mapping Definitions =============================== -->
    <action-mappings>
        <action

            name="SelectBeanForm"        
            parameter="method" 
            path="/login" 
            scope="request"
            type="com.action.SelectAction" 
            validate="false"
        >
        </action>        
    </action-mappings>  

  </struts-config>

 

3. web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "
http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
        <!-- ActionServlet Congif =====================================-->
        <servlet>
                <servlet-name>action</servlet-name>
                <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
                <init-param>
                        <param-name>config</param-name>
                        <param-value>/WEB-INF/struts-config.xml</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>                
        </servlet>
        
        
        <!-- ActionServlet Mapping ====================================-->
        <servlet-mapping>
                <servlet-name>action</servlet-name>
                <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        
         <!-- Struts Tag Library Descriptors -->
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-bean.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-html.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-logic.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
        </taglib>
            
</web-app>


 

4. SelectBean.java

 

package com.bean;

import org.apache.struts.action.ActionForm;

public class SelectBean extends ActionForm{
 
 private String userid;
 private String userpass;
 
 public String getUserid() {
  return userid;
 }
 public void setUserid(String userid) {
  this.userid = userid;
 }
 public String getUserpass() {
  return userpass;
 }
 public void setUserpass(String userpass) {
  this.userpass = userpass;
 }
 

}

 

5. SelectAction.java(DispatchAction을 확장했다)

 

package com.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Reader;

import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import com.bean.*;

import com.ibatis.common.resources.*;
import com.ibatis.sqlmap.client.*;

 

 

public class SelectAction extends DispatchAction{
 
 public ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

  SelectBean info = (SelectBean)form;
  SelectBean info2;
  
  Reader configReader = Resources.getResourceAsReader("SqlMapConfig.xml");
  SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(configReader);
  sqlMap.startTransaction();
  
  System.out.println("userpass = " + info.getUserpass());
  System.out.println("userId = " + info.getUserid());
  
  info2 = (SelectBean)sqlMap.queryForObject("getId",info);
  
  
  System.out.println("info2=" + info2);
  sqlMap.commitTransaction();
  String getuserid;
  String getpassword;
  if(info2 == null){
   getuserid = "";
   getpassword ="";
  }else{
   getuserid = info2.getUserid();
   getpassword = info2.getUserpass();
  }
  
  if(getuserid.equals(info.getUserid()) && getpassword.equals(info.getUserpass())){
   return mapping.findForward("success");
  }else{
   return mapping.findForward("fail");
  }
 }
 
}

 

6. SqlMapConfig.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"
http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
<properties resource ="SqlMapConfig.properties" />

<settings
 cacheModelsEnabled = "true"
 enhancementEnabled = "true"
 lazyLoadingEnabled = "true"
 maxRequests = "32"
 maxSessions = "10"
 maxTransactions = "5"
 useStatementNamespaces = "false"
/>

<transactionManager type = "JDBC">
 <dataSource type="SIMPLE">
  <property name ="JDBC.Driver" value="${driver}"/>
  <property name ="JDBC.ConnectionURL" value="${url}"/>
  <property name ="JDBC.Username" value="${username}"/>
  <property name ="JDBC.Password" value="${password}"/>
 </dataSource>
</transactionManager>

<sqlMap resource="SelectBean.xml" />
</sqlMapConfig>

 

7. SqlMapConfig.properties

 

driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@localhost1521:whoau

username=Dev

password=pass

 

8. SelectBean.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="SelectBean">
 <typeAlias alias="selectbean" type="com.bean.SelectBean"/>
 <select id = "getId" resultClass = "selectbean">
  SELECT USERID as userid,
   PASSWORD as userpass
  FROM USERINFO
  WHERE USERID = #userid# and PASSWORD = #userpass#
 </select>

</sqlMap>

'JSP & WEB & Ajax' 카테고리의 다른 글

jQuery로 작업하기 Part 1  (0) 2009.06.16
서블렛 + JDBC 연동시 코딩 고려사항 -제1탄-  (0) 2009.05.25
가상 input 만들어내기  (0) 2009.05.15
드래그&드롭 가능한 html table  (0) 2009.04.09
Google Ajax WebTool Kit  (0) 2009.04.09