블로그 이미지
헤이장

Notice

Recent Post

Recent Comment

Recent Trackback

calendar

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
  • total
  • today
  • yesterday
2009. 7. 27. 20:19 Web/Framework

액션클래스가 실행되기전에 가로채서 어떤 일을 수행하는 인터셉터

우선 보면


<action name="chkLogin" class="action.CheckLoginAction">

            <interceptor-ref name="servletConfig"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="params"/>
            <result>main.jsp</result>
</action>


public class CheckLoginAction implements Preparable, ModelDriven<Member_Hanbit>, SessionAware {
...  생략 ...
}


모든 인터셉터는 정상적으로 작동한다.
 

그렇다면 이렇게 하면?


<action name="chkLogin" class="action.CheckLoginAction">
            <result>main.jsp</result>
</action>

public class CheckLoginAction implements Preparable, ModelDriven<Member_Hanbit>, SessionAware {
             ...  생략 ...
}

이건 또 뭔가...?
액션 클래스에 구현된 모든 인터셉터가 똑같이 정상 작동한다. 세션 조차도...
params 를 안 써도 파라메터도 받아진다


그럼 이건???


<action name="chkLogin" class="action.CheckLoginAction"> 
            <interceptor-ref name="params"/>
            <result>main.jsp</result>
</action>

public class CheckLoginAction implements Preparable, ModelDriven<Member_Hanbit>, SessionAware {
                ...  생략 ...
}

다른 인터셉터는 작동하지 않고 파라메터 인자만 받아진다....
(물론 객체가 없기때문에 위의 경우는 임시로 바로 받을 변수를 선언해주어야한다) 

※ struts.xml 에서 액션을 선언 할 경우
    인터셉터를 하나도 선언하지 않으면 알아서 구현된 모든 인터셉터를 실행해준다
    하지만.. 단 하나의 인터셉터라도 선언하게 된다면
    인터셉터의 자동 실행은 모두 사라지고 선언된 인터셉터만 실행된다 

    그렇다면 인터셉터는 귀찮게 선언할 필요가 없나 하면 그건 아닌거 같다 

만약 다른 모든 인터셉터가 실행되기 전에 prepare 에서도 전달된 파라메터 값이 필요할 때..


<action name="chkLogin" class="action.CheckLoginAction">
            <interceptor-ref name="params"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="params"/>
            <result>main.jsp</result>
</action>

이렇게 params 를 두번 선언해야 할 경우가 있다. // 스트럿츠2 프로그래밍 - 가메출판사 106 페이지 참고

 이럴때는 자동으로 실행되는 것으론 첫번째 params 가 실행되지 않는다 

※ 그러므로 가능하다면 모든 인터셉터를 선언해주는 것이 가장 좋을 것 같다
    꼭 액션클래스를 안봐도 사용된 인터셉터를 알 수 있게 가독성을 위해서도.

posted by 헤이장
2009. 7. 27. 13:42 Web/Framework

<Struts2 네임스페이스 에 대한 고찰> 

* 우선 경로를 /exam/~~ 이렇게 루트로 시작하게 사용하는것은 경로의 변경시 모두 변경해줘야 하는 불편이 있으므로 가능한 사용하지 않는것이 좋다는 것을 원칙으로 한다
* result 파일의 위치는 /exam/Question_Form.jsp 으로 가정한다 

첫번째 

    <package name="exam" extends="struts-default">
        <action name="to_Question_Form" class="action.exam.QuestionForm">
            <result>exam/Question_Form.jsp</result>
        </action>
    </package> 

현재위치 : 루트 안의 파일
    -> 실행명령 : location="to_Question_Form.action";
문제점 : jsp 에 js 파일이나 css, img 등 파일을 불렀을 경우 /로 시작하는 절대 경로를 적지 않는 한 경로는 대부분 틀려진다. 그리고 잘 사용하지 못하면 exam/exam/exam/exam/~~~ 을 보게 될지도 모른다
 

두번째 방법 

    <package name="exam" extends="struts-default">
        <action name="to_Question_Form" class="action.exam.QuestionForm">
            <result>Question_Form.jsp</result>
        </action>
    </package> 

현재위치 : 루트 안의 파일
     -> 실행명령 : location="exam/to_Question_Form.action";
현재위치 : exam 폴더 안의 파일
    -> 실행명령 : location="to_Question_Form.action";
문제점 : exam 폴더가 아닌곳에서 루트 또는 다른 곳에서 location="to_Question_Form.action"; 액션을 실행하면 문제없이 액션클래스가 실행되어버리고 result 에서 위치 문제로 에러를 발생시킨다

  

세번째 방법 - 네임스페이스 

    <package name="exam" namespace="/exam" extends="struts-default">
        <action name="to_Question_Form" class="action.exam.QuestionForm">
            <result>Question_Form.jsp</result>
        </action>
    </package>

 현재위치 : 루트 안의 파일
     -> 실행명령 : location="exam/to_Question_Form.action";
현재위치 : exam 폴더 안의 파일
     -> 실행명령 : location="to_Question_Form.action";
장점 : namespace 를 /exam 으로 주었기 때문에 /exam 경로가 아닌곳에서 액션이 실행되면  네임스페이스 에러로 액션의 실행을 근본적으로 막을 수 있다
네임스페이스는 항상 실행되어야 할 위치에서 액션이 실행 될 수 있도록 해준다
네임스페이스 별로 package 를 나눠두는것도 유지보수에 좋은 것 같다

posted by 헤이장
2009. 7. 27. 13:13 Web/Framework

사용 클래스 : com.opensymphony.xwork2.ActionChainResult

두 개의 다른 액션에서 처음 액션의 프라퍼티를 다음 액션에 세팅하고 싶을때 chain 인터셉터를 이용한다 

예를 들어 회원등록 액션과 로그인 액션이 있는데
회원등록 후 그 정보로 이어서 바로 로그인으로 진행되게 한다

 RegisterUser.java

 public class RegisterUser implements Action { 

     private String userId;
     private String password;
     private String message; 

     public String execute() throws Exception {
          // 비지니스 로직 생략
          return SUCCESS;
     }
     // getter, setter 생략
}


 LoginAction.java

 public class LoginAction implements Action {
     private String userId;
     private String password;
     private String message;
     public String execute() throws Exception {
          // 비지니스 로직 생략
          return SUCCESS;
     }
     //getter, setter 생략
}

 struts.xml

 <package name="ex" extends="struts-default">
     <action name="registerAndLogin" class="action.RegisterUser">
          <interceptor-ref name="params"/>
          <result name="success" type="chain">login</result>
     </action>

     <action name="login" class="action.LoginAction">
          <interceptor-ref name="chain"/>
          <interceptor-ref name="params"/>
     <result name="success" type="dispatcher">/result.jsp</result>
     </action>
</package>

posted by 헤이장
2009. 7. 27. 13:10 Web/Framework

- struts.xml - 

<action name="*/*" class="action.{1}Action" method="{2}">
     <result name="success">{2}.jsp</result>
</action>

첫번째 * 은 {1} 에, 두번째 * 은 {2} 에 해당된다

http://localhost:8084/Struts2/First/second.action

이 경우에
action/FirstAction.java 에서 second() 를 실행하고
second.jsp 로 결과를 보낸다

posted by 헤이장