Struts2 Action : 액션
액션 : Action
클라이언트로부터 받은 HTTP요청과 비즈니스 오브젝트가 제공하는 서비스를 연결해주는 역할
구현방법
-
다른 클래스나,인터페이스를 구현하지 않은 POJO 클래스로 작성
-
com.opensymphony.xwork2.Action 인터페이스를 구현한 클래스로 작성
-
com.opensymphony.xwork2.ActionSupport 클래스를 상속한 클래스로 작성
POJO 형태의 액션
다른 클래스나, 인터페이스를 구현하지 않는다.
HelloAction1.java
package struts2.hello;
public class HelloAction1 {
private String message;
public String execute() throws Exception { message = "Hello World"; return "success"; }
public String getMessage() { return this.message; }
} |
struts.xml
<action name="hello1" class="struts2.hello.HelloAction1"> <result name="success">/hello.jsp</result> </action> |
hello.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<!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=EUC-KR"> </head> <body> <h1>${ message }</h1> </body> </html> |
Action인터페이스를 구현한 액션
Action인터페이스의 execute() 메소드를 재정의 해야한다.
HelloAction2.java
public class HelloAction2 implements Action {
private String message;
public String execute() throws Exception { message = "Hello World!!!"; return SUCCESS; }
public String getMessage() { return this.message; }
} |
ActionSupport 클래스를 상속한 액션
ActionSupport 클래스의 상속도
Action |
Validateable |
ValidationAware |
TextProvider |
LocaleProvider |
ActionSupport |
Action 인터페이스
-
액션의 기본동작
Validateable 인터페이스
-
액션 메소드 실행 전에 유효성 검사 기능 지원
ValidataionAware 인터페이스
-
액션메시지,액션 에러 메시지, 필드 에러 메시지를 저장, 조회 할 수 있는 기능 지원
TextProvider 인터페이스
-
지역화된 메시지 리소스에 접근하는 기능 지원
LocaleProvider 인터페이스
-
Locale 객체 제공
HelloAction3.java
public class HelloAction3 extends ActionSupport {
private String message;
public String execute() throws Exception { message = "Hello World~~~"; return SUCCESS; }
public String getMessage() { return this.message; }
} |
NameSpace
요청URL의 중간 경로로 사용된다.
http:/ / ip:port/ [어플리케이션패스] / [네임스페이스] / [액션이름] .action
Default Action
매핑되어 있지 않은 URL로 요청이 왔을 때 에러를 발생시키지 않고 기본으로 매핑시킬 액션이다.
모든 패키지는 자신의 디폴트 액션을 가질 수 있다.
하나의 네임스페이스당 하나의 디폴트 액션을 가질 수 있다.
설정파일 include
struts.xml을 만들 때, 한 파일에 모든 설정을 넣는다면 설정 내용도 길어지고 팀 개발이 어렵게 된다.
각각의 설정 내용을 개별적인 xml파일로 작성후 include 하는 것으로 해결 할 수 있다.
<include file="개별적인 설정 파일명" />
"제로 컨피규레이션 지향" 룰에 의해 설정파일 가장 상단에 묵시적으로 <include file="struts- default.xml">을 포함하고 있다.
액션 메소드
execute 메소드 외에 여러 개의 액션 메소드를 만들 수 있다.
기본 메소드가 아닌 메소드는 action 설정에 method속성으로 지정한다.
와일드카드 매핑
*를 사용하여 설정의 내용을 줄일 수 있다.
'* '는 여러 번 올 수 있으며 첫번째 *부터 { 1} ,{ 2} ,.. 차례로 매핑된다.
액션의 이름에 ' / '를 사용할 경우 struts.properties 파일에
struts.enable.SlashesInActionNames = true 내용을 추가한다.