2013년 8월 12일 월요일

[ORACLEJAVA 커뮤니티struts spring 강좌]Struts2 Action – Action 인터페이스 이용

com.opensymphony.xwork2.Action 인터페이스는 Action이 실행된 후 리턴하는 결과 문자열과 execute() 추상 메소드를 포함하고 있는 인터페이스 인데 Struts2의 Action을 이 인터페이스를 구현하여 작성할 수 있다.

오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의)  
 

 

Action Interface의 구현은 다음과 같이 되어 있다.

 

package com.opensymphony.xwork2;

 

 

public interface Action

{

 

    public abstract String execute()

        throws Exception;

 

    public static final String SUCCESS = "success";

    public static final String NONE = "none";

    public static final String ERROR = "error";

    public static final String INPUT = "input";

    public static final String LOGIN = "login";

}

 

[실습]

- 이전 예제의 기능에 다음과 같은 부분이 추가 되었다.

 

Action Interface를 구현하여 Action 작성

Action 클래스의 execute() 메소드에서 validation check 수행

 

 

- 다음과 같은 일반 Action 인터페이스를 상속받아 액션 클래스를 만들자. (hello2.action package에 만든다)

 

[HelloWorld.java]

 

package hello2.action;

 

import com.opensymphony.xwork2.Action;

 

public class HelloWorld implements Action {

          private String name;

          private String msg;

          private String retMsg;  //client로 보낼 오류 메시지를 담기 위해

 

          public String getMsg() {

                    return msg;

          }

 

          public void setName(String name) {

                    this.name = name;

          }

         

          public String getRetMsg() {

                    return retMsg;

          }

         

          public String execute() throws Exception {

                    //validation check

                    if (name == null || "".equals(name)) {

                                retMsg = "Enter Name!!";

                                return INPUT;

                    }

                   

                    msg = "Hello," + name;

                   

                    return SUCCESS;

          }

}

 

이전 POJO 클래스로 구현한 Action과 차이점은 Action 인터페이스를 상속한 것 이외 execute() 메소드에서 리턴 하는 값을 Action 인터페이스의 SUCCESS 상수를 이용했다. (물론 “success”를 직접 return 해도 무방하다.)

 

- Context 아래에 hello2 이라는 폴더 만들고 hello.jsp, result.jsp를 만들자.

 

[hello.jsp]

 

<h2>${retMsg}<h2>

<form action=/struts2_helloworld/hello2/hello.action>

Input Your Name : <input type=text name=name>

<input type=submit>

</form>

[result.jsp]

<h2>${msg}<h2>

 

 

- struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

   

<struts>

    <package name="hello2" namespace="/hello2" extends="struts-default">

      <action name="hello" class="hello2.action.HelloWorld">

        <!-- params인터셉터는 액션의 setter값을 요청 파라미터로 셋팅한다(생략가능)-->

        <interceptor-ref name="params"/>

        <result name="success">/hello2/result.jsp</result>

<result name="input">/hello2/hello.jsp</result>

      </action>

    </package>

</struts>

- 결과확인

 

http://localhost:8080/struts2_helloworld/hello2/hello.jsp 라고 입력한 후 확인하자.


댓글 없음:

댓글 쓰기