7.2 Struts »ç¿ëÇϱâ

ÀÛ¼ºÀÚ : ÁøÀº¿µ ( 2004-08-05)

[¸ÞÀÎ] [¸ñ·Ï]

¸ñÂ÷

7.2.1 Struts Ä¿½ºÅÒ Å±×

½ºÆ®·¯Ã÷ ÇÁ·¹ÀÓ¿öÅ©´Â ºä¸¦ ½±°Ô »ý¼ºÇϵµ·Ï µ½±â À§ÇÏ¿© ¸¹Àº Ä¿½ºÅÒ Å±׸¦ Á¦°øÇÑ´Ù. ÀÌ Ä¿½ºÅÒ Å±׵éÀ» Àü °­Á¿¡¼­ strutsTest\WEB-INF\tld Æú´õ¿¡ º¹»çÇß´Ù. ÀÌ Å±׵éÀ» »ç¿ëÇϱâ À§ÇÏ¿© web.xml¿¡¼­ ´ÙÀ½°ú °°ÀÌ ¼³Á¤ÇÑ´Ù. ½ºÆ®·¯Ã÷¿¡¼­ Á¦°øÇÑ ¸ðµç tldÆÄÀÏ¿¡ ´ëÇÏ¿© ´ÙÀ½°ú °°ÀÌ ¼³Á¤ÇÏ¸é µÈ´Ù.
<?xml version="1.0" encoding="euc-kr"?>

<!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>

    <taglib>
        <taglib-uri>struts/bean</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts/html</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts/logic</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts/nested</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts/template</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-template.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts/tiles</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
    </taglib>
</web-app>

Ä¿½ºÅÒ Å±׸¦ »ç¿ëÇغ» °³¹ßÀÚµéÀ̶ó¸é web.xml¿¡ Ä¿½ºÅÒ Å±×ÀÇ »ç¿ëÀ» À§ÇÑ tldÆÄÀÏÀ» ¼³Á¤ÇÏ´Â ºÎºÐ¿¡ ´ëÇÏ¿© ½±°Ô ÀÌÇØÇÒ ¼ö ÀÖÀ» °ÍÀÌ´Ù. web.xml¿¡¼­ À§¿Í °°ÀÌ ¼³Á¤ÇÑ ´ÙÀ½ JSP¿¡¼­ ´ÙÀ½°ú °°ÀÌ »ç¿ëÇÒ Ä¿½ºÅÒ Å±׸¦ ÁöÁ¤ÇÏ´Â °ÍÀÌ °¡´ÉÇÏ°Ô µÈ´Ù.
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

Ä¿½ºÅÒ Å±׸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀº ³ªÁß¿¡ ´Ù½Ã ´Ù·ç°Ú´Ù.


7.2.2 ½ºÆ®·µÃ÷ È帧

Ŭ¶óÀ̾ðÆ®·ÎºÎÅÍ °ªÀ» ÀÔ·Â¹Þ¾Æ ÇØ´ç ¾ÆÀ̵ð¿Í Æнº¿öµå°¡ ¸Â´Ù¸é, ¿øÇÏ´Â ÆäÀÌÁö·Î À̵¿ÇÏ´Â ½ºÆ®·¯Ã÷ ±¸Á¶´Â ¾Æ·¡¿Í °°´Ù.


Á»´õ ±¸Ã¼ÀûÀÎ ³»¿ëÀ» »ìÆ캸ÀÚ.
  • À¥¾ÖÇø®ÄÉÀ̼ÇÀÌ ½ÃÀÛÇÒ ¶§ ActionServletÀÇ init()°¡ È£ÃâµÇ°í web.xml¿¡ ¼±¾ðµÇ¾î Àִ´ë·Î struts-config.xml¸¦ ·ÎµåÇÑ´Ù. struts-config.xmlÀº StrutsÇÁ·¹ÀÓ¿öÅ©¿¡¼­ »ç¿ëÇÏ´Â ¸ðµç ¼³Á¤ Á¤º¸¸¦ ´ã°í ÀÖ´Â ÆÄÀÏÀ̶ó°í »ý°¢ÇÏ¸é µÈ´Ù. config À̸§Àº ²À struts-config.xmlÀÌ ¾Æ´Ñ ´Ù¸¥ À̸§À» °¡Áö´Â XMLÆÄÀÏÀ̾ »ó°ü¾ø´Ù.
    ActionServletŬ·¡½º´Â struts¿¡¼­ Á¦°øÇÏ°í ÀÖ´Ù.

  • Ŭ¶óÀ̾ðÆ®°¡ id¿Í pw¸¦ ÀÔ·ÂÇÑ ÈÄ submit¸¦ ´©¸£¸é action=login.do ·Î À̵¿ÇÑ´Ù.
    web.xml¿¡ *.do ´Â ÀüºÎ org.apache.struts.action.ActionServlet·Î À̵¿ÇÏ°Ô²û mapping ½ÃÄѳù´Ù.

  • Ŭ¶óÀ̾ðÆ®°¡ ÀÔ·ÂÇÑ id¿Í pw´Â ActionForm Ŭ·¡½º¸¦ »ó¼Ó¹Þ´Â Ŭ·¡½ºÀÇ °´Ã¼¿¡ ÀÚµ¿À¸·Î ÀúÀåµÈ´Ù.
    ÀÌ·¯ÇÑ Çü½ÄÀº struts-config.xml <form-beans> ű׿¡ ÁöÁ¤ÇÑ´Ù.

  • Ŭ¶óÀ̾ðÆ®ÀÇ ÀԷ°ªÀ» ÀúÀåÇÑ °´Ã¼¸¦ °¡Áö°í ¾î¶² µ¿ÀÛÀ» ÃëÇÒ°ÍÀΰ¡¸¦ ÁöÁ¤ÇÑ ºÎºÐÀº struts-config.xmlÀÇ <action-mappings>ű×ÀÌ´Ù. ÀÌ Å±׿¡¼­ ÁöÁ¤ÇÑ Å¬·¡½º·Î °´Ã¼¸¦ Àü´ÞÇÑ´Ù. ÇØ´ç Ŭ·¡½º´Â ActionŬ·¡½º¸¦ »ó¼ÓÇÑ Å¬·¡½º·Î DB¿¡ Á÷Á¢ ¿¬°áÇϴ Ŭ·¡½º¸¦ È£ÃâÇÏ´Â ºÎºÐÀÌ µé¾î ÀÖ´Ù.

  • ó¸®ÇÑ ÈÄ <action> ű׿¡¼­ ÁöÁ¤ÇÑ ¹®¼­·Î mappingÇÑ´Ù. Àü´ÞÇϴ Ŭ·¡½ºÀÇ Å¸ÀÔÀº ActionForwardÀÌ´Ù.

7.2.3 ¿¹Á¦

À§¿¡¼­ »ç¿ëÇß´ø ¿¹Á¦¸¦ °¡Áö°í Á÷Á¢ ½ÇÇàÇغ¸ÀÚ.

¨ç web.xml
È£ÃâÇÏ·Á´Â ¼­ºí¸´ÀÇ º°Äª°ú ¸ÅĪ¿¡ ´ëÇÑ Á¤º¸, struts-config.xmlÀ» ¸Þ¸ð¸®¿¡ ·ÎµåÇÏ·Á´Â Á¤º¸°¡ µé¾îÀÖ´Ù.
<?xml version="1.0" encoding="euc-kr"?>

<!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>
	<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/tld/struts-config.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	    <servlet-name>action</servlet-name>
	    <url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<taglib>
		<taglib-uri>struts/bean</taglib-uri>
		<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>struts/html</taglib-uri>
		<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>struts/logic</taglib-uri>
		<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>struts/nested</taglib-uri>
		<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>struts/template</taglib-uri>
		<taglib-location>/WEB-INF/tld/struts-template.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>struts/tiles</taglib-uri>
		<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
	</taglib>
</web-app>


¨è struts-config.xml
Ŭ¶óÀ̾ðÆ®·ÎºÎÅÍ ÀԷ¹ÞÀº Á¤º¸¸¦ ÀúÀåÇÏ´Â ³»¿ë°ú ó¸®ÇØ¾ß ÇÏ´Â ³»¿ëÀ» ´ã°í ÀÖ´Â ÆÄÀÏÀÌ´Ù.
<?xml version="1.0" encoding="euc-kr"?>

<!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-beans>
                <form-bean name="loginForm" type="kr.co.a.LoginForm" />
        </form-beans>

        <action-mappings>
                <action name="loginForm" path="/login" type="kr.co.a.LoginAction" input="/login.jsp">
                        <forward name="main" path="/main.jsp" />
                </action>
        </action-mappings>

        <controller />
        <message-resources  parameter=""/>
</struts-config>


¨é LoginForm.java
Ŭ¶óÀ̾ðÆ®°¡ ÀÔ·ÂÇÏ´Â Á¤º¸¸¦ ÀúÀåÇϴ Ŭ·¡½ºÀÌ´Ù.
package kr.co.a ; 

import javax.servlet.http.*;

import org.apache.struts.action.*;

public class LoginForm extends ActionForm{
        private String id ;
        private String password ;

        public LoginForm(){
                System.out.println( "LoginForm" ) ; 
        }

        public String getPassword() {
                return password;
        }

        public void setPassword(String password) {
                this.password = password;
        }

        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }

}


¨ê LoginAction.java
Ŭ¶óÀ̾ðÆ®·ÎºÎÅÍ ÀÔ·ÂÇÑ °ªÀ» °¡Áö°í ºñ±³ÇÏ¿© ÇØ´ç jsp¹®¼­·Î ¸ÅÇÎÇÏ´Â, Áï Á÷Á¢ 󸮸¦ ÇÏ´Â ¹®¼­ÀÌ´Ù.
package kr.co.a ; 

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class LoginAction extends Action{
        public ActionForward execute ( ActionMapping mapping , ActionForm form , 
                HttpServletRequest request , HttpServletResponse response ) throws Exception {
        
                LoginForm loginForm = ( LoginForm ) form ; 
                String inputId = loginForm.getId().trim() ; 
                String inputPassword = loginForm.getPassword().trim() ; 

                return ( mapping.findForward( "main" )) ; 
        }
}


¨ë login.jsp
<%@page contentType="text/html;charset=euc-kr" %>
<FORM METHOD=POST ACTION="login.do">
¾ÆÀ̵ð : <INPUT TYPE="text" NAME="id"><br>
ºñ¹Ð¹øÈ£ : <INPUT TYPE="password" NAME="password"><br>
<INPUT TYPE="submit">
</FORM>


¨ì main.jsp
<%@page contentType="text/html;charset=euc-kr" %>
main


¨í build.xml
Ŭ·¡½ºÆнº¸¦ µû·Î ÀâÁö ¾Ê¾Æµµ ÆÐÅ°Áö ÄÄÆÄÀÏÀ» ÇÒ ¼ö ÀÖ´Ù. (ºÎ·Ï °­ÁÂÀÇ ant¸¦ È®ÀÎÇÑ´Ù.)
<?xml version="1.0" encoding="euc-kr"?>
<project basedir="." default="compile">
        <property name="catalina.home" value="D:\01.program\01.java\02.ee\01.Tomcat 4.1" />
        <property name="dist.servlet.jar" value="${catalina.home}/common/lib/servlet.jar" />

        <path id="classpath">
                <fileset dir="${basedir}/lib" includes="*.jar" />
                <pathelement location="${dist.servlet.jar}" />
        </path>

        <target name="compile">
                <echo message="Compiling the java source files..." />
                <javac srcdir="${basedir}/src/Login" destdir="${basedir}/classes" debug="on" >
                        <classpath refid="classpath" />
                </javac>
        </target>
</project>


¨î ÄÄÆÄÀÏ commandâÀ» ¶ç¿î ÈÄ ÇØ´ç ÄÁÅؽºÆ®ÀÇ WEB-INF±îÁö À̵¿ÇÑ´Ù.

ant¶ó°í ¸í·ÉÀ» ¾²¸é build.xmlÀÌ ½ÇÇàµÇ¸é¼­ ÆÐÅ°Áö ÄÄÆÄÀÏÀÌ µÈ´Ù.

¨ï ½ÇÇà
ÅèĹÀ» ½ÇÇà½ÃŲÈÄ ºê¶ó¿ìÀú¸¦ ¶ç¿ì°í ¾Æ·¡¿Í °°Àº ÁÖ¼Ò¸¦ ÀÔ·ÂÇÑ´Ù.


¾ÆÀ̵ð¿Í Æнº¿öµå¸¦ ÀÔ·ÂÇÑ ÈÄ Àü¼Û¹öÆ°À» ´©¸£¸é ¾Æ·¡¿Í °°ÀÌ Ã¢ÀÌ ³ªÅ¸³ª¸é ¼º°øÀÌ´Ù.

Âü°í¹®Çå