XWork 2.0 입문 인 스 턴 스 코드

Xwork. xml 프로필
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 2.0//EN" "http://www.opensymphony.com/xwork/xwork-2.0.dtd">
<xwork>
    <include file="xwork-default.xml"/>
    <package name="default" extends="xwork-default" namespace="/test">
		<result-types>
			<result-type name="successResult" class="com.jamesby.xwork.helloworld.SuccessResult" />
			<result-type name="failedResult" class="com.jamesby.xwork.helloworld.FailedResult" />
		</result-types>    
    	<interceptors>
	    	<interceptor name="testInterceptor"  class="com.jamesby.xwork.helloworld.TestInterceptor"/>
	    </interceptors>	
	   <action name="testAction" class="com.jamesby.xwork.helloworld.TestAction">
			<result name="success" type="chain">
				<param name="actionName">testChainAction</param>
			</result>	
			<result name="failed" type="failedResult"/>			
			<interceptor-ref name="params"/>
			<!--Model Driven-->
			<interceptor-ref name="model-driven"/>
			<interceptor-ref name="defaultStack"/>
			
			<interceptor-ref name="testInterceptor"/>			
	   </action>
	   
	   <action name="testChainAction" class="com.jamesby.xwork.helloworld.TestChainAction">
			<result name="success" type="successResult">
				<param name="param">Custom Type</param>
			</result>	

			<result name="failed" type="failedResult"/>			

			<interceptor-ref name="params"/>
			<interceptor-ref name="model-driven"/>
			<interceptor-ref name="defaultStack"/>
			<interceptor-ref name="testInterceptor"/>

	   </action>	   
    </package>
    <constant name="devMode" value="false" />
</xwork>

xwork - conversion. properties 속성 파일
com.jamesby.xwork.helloworld.TestType=com.jamesby.xwork.helloworld.TestConvert

Action 파일 과 테스트 Chain 의 Action 파일
public class TestAction implements Action, ModelDriven {
	TestModel book = new TestModel();
	int id;
	public String execute() throws Exception {

		System.out.println("
Action is invoked............"); System.out.println("Action:Received Id is " + id); System.out.println("Action:Receive Book Title is " + book.getTitle()); // book = bookDAO.findById(id, Book.class); book.setTitle("Action Title"); if (id < 1000) return "failed"; return "success"; } public TestModel getModel() { return book; } public void setId(int id) { this.id = id; } }
public class TestChainAction implements Action, ModelDriven {
	TestModel book = new TestModel();
	int id;
	public String execute() throws Exception {

		System.out.println("
Chain Action is invoked............"); System.out.println("Chain Action:Received Id is " + id); System.out.println("Chain Action:Receive Book Title is " + book.getTitle()); // book = bookDAO.findById(id, Book.class); book.setTitle("Chain Action Title"); if (id < 2000) return "success"; return "failed"; } public TestModel getModel() { return book; } public void setId(int id) { this.id = id; } }

ModelDriven 의 모델 코드
public class TestModel {
	String id;
	String title;
	public void setId(String id) {
		this.id = id;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getId() {
		return this.id;
	}
	public String getTitle() {
		return this.title;
	}
}

결과 코드
public class SuccessResult implements Result {
		private TestType param;
		public void setParam(TestType param) {
			this.param = param;
		}
		public void execute(ActionInvocation invocation) {
			System.out.println("
SuccessResult is invoked.............."); System.out.println("param is "+param.getValue()); System.out.println("SuccessResult:Received Title is " + invocation.getStack().findValue("title")); } } public class FailedResult implements Result { public void execute(ActionInvocation invocation) { System.out.println("
FailedResult is invoked.............."); System.out.println("FailedResult:Received Title is " + invocation.getStack().findValue("title")); } }

TypeConvert 파일 코드
import java.lang.reflect.Member;
import java.util.Map;

import ognl.DefaultTypeConverter;

public class TestConvert extends DefaultTypeConverter {
	public Object convertValue(Map context, Object target, Member member,
			String propertyName, Object value, Class toType) {
		return new TestType("" + value);
	}
}


public class TestType {
	private String value;
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public TestType(String value) {
		this.value = value;
	}
}

사용자 정의 Interceptor 와 PreResultListener
public class TestInterceptor extends AbstractInterceptor {
	public String intercept(ActionInvocation invocation) throws Exception {
		invocation.addPreResultListener(new PreResultListener() {
			public void beforeResult(ActionInvocation invocation,
					String resultCode) {
				System.out.println("
Pre Result Listerer is invoked........"); } }); return invocation.invoke(); } }

주 응용 프로그램 코드
public class HelloWorldMain {

	public static void main(String[] args) throws Exception {
		Map paramMap = new HashMap();
		/**
		 * 1000    testAction Failed Result
		 * 1000-2000 testAction testChainAction SuccessResult
		 * 2000    testAction testChainAction Failed Result
		 */
		paramMap.put("id", "500");
		paramMap.put("title", "param title");

		Map context = new HashMap();
		context.put(ActionContext.PARAMETERS, paramMap);

		ConfigurationManager cm = new ConfigurationManager();
		Configuration conf = cm.getConfiguration();
		Container containter = conf.getContainer();
		DefaultActionProxyFactory actionProxyFactory = new DefaultActionProxyFactory();
		actionProxyFactory.setContainer(containter);
		ActionProxy proxy = actionProxyFactory.createActionProxy("/test",
				"testAction", context);
		String result = proxy.execute();
		if ("success".equals(result)) {
			TestAction action = (TestAction) proxy.getAction();
			System.out.println("
Return Success................."); System.out.println("Return:Return Title is "+action.getModel().getTitle()); } if ("failed".equals(result)) { TestAction action = (TestAction) proxy.getAction(); System.out.println("
Return Failed................."); System.out.println("Return:Return Title is "+action.getModel().getTitle()); } } }

방금 Xwork 2 를 배 웠 습 니 다. struts 2 를 배 우려 고 했 는데 xwork 기반 이 필요 하 다 는 것 을 알 게 되 었 습 니 다. 개인 적 으로 xwork 를 배 우 는 것 은 하 나 는 그의 차단기 체 제 를 이해 하 는 것 이 고 다른 하 나 는 ValueStack 체 제 를 이해 하 는 것 이 라 고 생각 합 니 다. xwork 2 학습 소결 을 작성 하려 고 합 니 다. 많은 지적 바 랍 니 다.

좋은 웹페이지 즐겨찾기