ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스트럿츠2 Hello World
    카테고리 없음 2011. 8. 1. 17:06

    처음 시작함에 앞서 기본이 되는건 역시 "Hello World" 가 아닐까 한다.

    Struts2 에서의 액션의 흐름 상태를 파악해 보자.

    액션의 순서대로 따라  가보도록 하겠다.

    일단 이클립스에서 빌드했을 경우 경로들을 살펴보면


    이런 형태 이고 처음에 실행했을 경우 web.xml 을 인식하고 실행 된다.

    액션의 이름은 index.action

    start.jsp 의 내용은

    <% response.sendRedirect("index.action");%>

    이게 다이다. 

    start.jsp - > web.xml - >

    web.xml
    
     
        
            struts2
            org.apache.struts2.dispatcher.FilterDispatcher
        
    
        
            struts2
            /*
        
    
    
    이부분의 대한 설명을 하자면 이 URL상의 모든 .action 또는 .do 프로퍼티에 정의한 액션의 이름들을 만나면 스트럿츠 필터가 스트럿츠로 실행하겠다는 뜻이 된다. 고로 여기서 struts.xml 을 찾을 것이고 이제 struts.xml로 이동한다. -> struts.xml

    sturts.xml
    
    
    
    
    
    	
    	
    	
    	
    		
    		
    			/index/index.jsp
    		
    	
    
    
    위의 start.jsp 에서 index.action 이 실행되어서 /index/index.jsp 이 부분에 걸리게 된다. index에 정의되어 있는 클래스를 여기서 찾게 될 것이고 ememomo.action.IndexAction 이라는 서블릿을 실행 하러 이동할 것이다.
     
    IndexAction.java
    package ememomo.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class IndexAction extends ActionSupport{
    	String hello;
    	@Override
    	public String execute() throws Exception {
    		
    		hello = "Welcome Epong's Blog Struts2 Start~~";
    		return SUCCESS;
    	}
    	public void setHello(String hello) {
    		this.hello = hello;
    	}
    	
    	public String getHello(){
    		return this.hello;
    	}
    	
    }
    
    이클래스에서 ActionSupport 의 함수를 상속하여 execute 에서 request / response를 실행한다고 생각하면 된다. 이 부분은 struts에서의 규약대로 지켜준다면 알아서 처리 해 주는 것이기에. 눈으로 볼 순 없다. 그래서 나중되면 어찌 동작하는지 볼 수 없어서 헷갈릴 수 도 있지만, 더 편하기 때문에 Framework을 사용하는 것이 아닌가. 또 getter 와 setter로 값을 반환 / 치환 하기 때문에 값을 넣을때랑 받을대는 getter와 setter의 구현이 필수적이다. 이부분에서 hello = "Welcome Epong's Blog Struts2 Start~~"; return SUCCESS; 이부분에서 hello 라는 스트링 값을 대입하고 SUCCESS를 리턴한다. 이러 하게 되면 아까 struts.xml 부분에서 success">/index/index.jsp 이부분의 success 를 만나 index/index.jsp 경로의 jsp를 실행 하게 된다. index.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
     ${ hello }
     <%= request.getAttribute("hello") %>
    </body>
    </html>


    taglib 를 이용해서 값을 받아왔다. 이 부분은 <%= request.getAttribute("hello") %>이렇게 사용하는 것과 동일하다. 참으로 복잡하게 돌아가는 struts2 이다. 허나 익숙해지면 왜 사람들이 Framework을 사용하려 하는지 알게 될 것이다. 프로젝트가 커지면 커질수록 이런 FrameWork 요소는 필수적이다.. 결과 화면


    댓글

COPYRIGHT 2010 EpoNg. ALL RIGHTS RESERVED.