2013년 8월 12일 월요일

[ORACLEJAVA커뮤니티, struts, struts2,spring강좌]struts2 result – 직접구현예제

struts2 result – result 만들기<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><?xml:namespace prefix = o />:namespace prefix = o />
 
result 를 직접 구현하기 위해서는 Action과 비슷하게 Result 인터페이스의 execute() 메소드를 구현하면 되는데 리턴 타입이 없고 너무 단순해서 필요한 부분은 구현해 주어야 한다. Action execute 메소드 실행 후 매핑 된 result 클래스의 execute() 메소드를 실행하고 Interceptor의 후 처리를 한다.
 
package com.opensymphony.xwork2;
 
import java.io.Serializable;
 
public interface Result
    extends Serializable
{
 
    public abstract void execute(ActionInvocation actioninvocation)
        throws Exception;
}
 
Result 인터페이스는 너무 단순해서 세부적인 구현을 일일이 해야 하므로 StrutsResultSupport 클래스를 상속받아 구현하는 것이 좋다. (location 파라미터를 읽어 Ognl 표현식을 Parsing하고 URL을 인코딩 하는 기능이 포함되어 있다)
 
아래는 StrutsResultSipport 클래스의 원형이다.
 
package org.apache.struts2.dispatcher;
 
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.TextParseUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsStatics;
 
public abstract class StrutsResultSupport
    implements Result, StrutsStatics
{
 
    public StrutsResultSupport()
    {
        this(null, true, false);
    }
 
    public StrutsResultSupport(String location)
    {
        this(location, true, false);
    }
 
    public StrutsResultSupport(String location, boolean parse, boolean encode)
    {
        this.location = location;
        this.parse = parse;
        this.encode = encode;
    }
 
    public void setLocation(String location)
    {
        this.location = location;
    }
 
    public String getLastFinalLocation()
    {
        return lastFinalLocation;
    }
 
    public void setParse(boolean parse)
    {
        this.parse = parse;
    }
 
    public void setEncode(boolean encode)
    {
        this.encode = encode;
    }
 
    public void execute(ActionInvocation invocation)
        throws Exception
    {
        //location 속성을 파싱한다.
        lastFinalLocation = conditionalParse(location, invocation);
        doExecute(lastFinalLocation, invocation);
    }
 
    protected String conditionalParse(String param, ActionInvocation invocation)
    {
        if(parse && param != null && invocation != null)
            return TextParseUtil.translateVariables(param, invocation.getStack(), new com.opensymphony.xwork2.util.TextParseUtil.ParsedValueEvaluator() {
 
                public Object evaluate(Object parsedValue)
                {
                    if(!encode || parsedValue == null)
                        break MISSING_BLOCK_LABEL_58;
                    return URLEncoder.encode(parsedValue.toString(), "UTF-8");
                    UnsupportedEncodingException e;
                    e;
                    StrutsResultSupport._log.warn((new StringBuilder()).append("error while trying to encode [").append(parsedValue).append("]").toString(), e);
                    return parsedValue;
                }
 
                final StrutsResultSupport this$0;
 
           
            {
                this$0 = StrutsResultSupport.this;
                super();
            }
            });
        else
            return param;
    }
 
    protected abstract void doExecute(String s, ActionInvocation actioninvocation)
        throws Exception;
 
    private static final Log _log = LogFactory.getLog(org/apache/struts2/dispatcher/StrutsResultSupport);
    public static final String DEFAULT_PARAM = "location";
    private boolean parse;
    private boolean encode;
    private String location;
    private String lastFinalLocation;
 
 
 
우리는 StrutsResultSupport 클래스의 doExecute 메소드를 구현하여 간단한 result를 만들어 보자.
 
 

댓글 없음:

댓글 쓰기