티스토리 뷰

728x90
[struts.xml]

<action name="prefix" class="Action.PrefixAction">
<result>/prefix_test.jsp</result>
<result name="another_method">/prefix_test.jsp</result>
</action>
<action name="another" class="Action.AnotherAction" method="method">
<result>/prefix_test.jsp</result>
<result name="method">/prefix_test.jsp</result>
</action>

[prefix_test.jsp]
<s:form action="prefix">
<s:submit value="s:form에 지정된 prefix액션 실행" />
<s:submit value="s:form에 지정된 prefix액션의 메소드 지정 실행" name="method:anotherMethod" />
<s:submit value="다른 액션의 execute() 실행(접두어 사용)" name="action:another" />
<s:submit value="다른 액션의 execute() 실행(action 사용)" action="another" method="execute" />
        <!-- 얘는 접두어는 아닌데 action이 먹히질 않아서 실험용 -->
<s:submit value="다른 URL 실행" name="redirect:http://www.naver.com" />
<s:submit value="다른 액션 실행(struts.xml에 정의된)" name="redirectAction:another" />
</s:form>
<br />
결과 : <s:property value="result" />


[PrefixAction.java]

package Action;

public class PrefixAction {
String result;
public String execute() {
result = "s:form에 지정된 prefix액션 실행";
return "success";
}

public String anotherMethod() {
result = "s:form에 지정된 prefix액션의 메소드 지정 실행";
return "another_method";
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}
}


[AnotherAction.java]

package Action;

public class AnotherAction {
String result;
public String execute() {
result = "다른 액션의 execute() 실행";
return "success";
}

public String method() {
result = "다른 액션 실행(struts.xml에 정의된)";
return "method";
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}



1. Method 접두어 : <s:form> action의 지정 메소드 실행




2. Action 접두어 : 지정 액션의 execute() 실행
==> 분명히 API 문서에는 action으로 써주면 다른 액션 클래스의 execute()가 실행된다고 적혀있는데
      제대로 동작되지 않는다



3. Redirect 접두어 : 지정 URL 실행




4. RedirectAction 접두어 : 지정 액션 실행
   ==> Action 접두어와 비슷하지만 execute()를 실행시키는 Action과는 달리
         struts.xml에 정의된 대로 동작된다
   ※ struts 2.1.6 이전 버전에서는 redirect-action


API 문서에 의하면 action과 redirectAction의 기능이 분명히 구분되어 있는데 뭐가 잘못된건지 기능이 제대로 실행되질 않고
똑같이 동작을 한다

그래서 약간 다른 방법이 2가지가 있다(접두어에 해당하는 내용인지는 모르겠다)

1. <s:submit value="다른 액션의 지정 메소드 실행" name="action:another!another" />
    또는 <s:submit value="다른 액션의 지정 메소드 실행" name="redirectAction:another!another" />

2. <s:submit value="다른 액션의 지정 메소드 실행" action="another" method="another" />


위 방법으로도 해당 클래스의 메소드가 실행이 된다

직관적으로 가장 눈에 잘 들어오는 방법은 2번째 인것 같다