论坛首页 入门技术论坛

如何用main方式启动xwork2.0?

浏览 3680 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-03-03  
如何用main方式启动xwork2.0?
   发表时间:2007-03-03  
	public static void main(String[] args) throws Exception {
		Map paramMap = new HashMap();
		paramMap.put("id", "0123456789");

		//		 set the ActionContext parameters
		Map context = new HashMap();
		context.put(ActionContext.PARAMETERS, paramMap);
		
//		ConfigurationManager cm = new ConfigurationManager();
//
//		Configuration conf = cm.getConfiguration();
//
//		
		//		 create an action proxy with no namespace, action alias (defined in xwork.xml), and a map of the context info
		ActionProxy proxy = new DefaultActionProxyFactory().createActionProxy(
				"", "viewBook", context);

		//		 we have the action proxy instance, lets execute it and retrieve the action
		String result = proxy.execute();
		if ("success".equals(result)) {
			ViewBookAction action = (ViewBookAction) proxy.getAction();

			// return info back to caller or just print to screen for this example
			System.out.println(action.getBook().getTitle());
		} else if ("notFound".equals(result)) {
			// forward to another inventory source
		} else {
			throw new RuntimeException("Im lazy");
		}

	}


以上代码会产生问题NullPointer异常,看代码好象是Container没有注入,原来的代码是

ActionProxy proxy = ActionProxyFactory().getFactory().createActionProxy(
				"", "viewBook", context);


但是2.0根本就不能如此用.刚刚接触xwork.
0 请登录后投票
   发表时间:2007-03-03  
改成如下方式就可以了:


package com.jamesby.xwork.helloworld;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.DefaultActionProxyFactory;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.inject.Container;

public class BookMain {

	public static void main(String[] args) throws Exception {
		Map paramMap = new HashMap();
		paramMap.put("id", "0123456789");
		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(
				"", "viewBook", context);
		String result = proxy.execute();
		if ("success".equals(result)) {
			ViewBookAction action = (ViewBookAction) proxy.getAction();
			System.out.println(action.getBook().getTitle());
			System.out.println("Success.................");
		} else if ("notFound".equals(result)) {
		} else {
			throw new RuntimeException("Im lazy");
		}

	}

}


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">
	<result-types>
	<result-type name="myResult"
		class="com.jamesby.xwork.helloworld.MyResult" />
	</result-types>    
       <action name="viewBook" class="com.jamesby.xwork.helloworld.ViewBookAction">
			<result type="myResult">
			</result>
       </action>
    </package>
    <constant name="devMode" value="false" />
</xwork>


package com.jamesby.xwork.helloworld;

import com.opensymphony.xwork2.Action;

public class ViewBookAction implements Action {
	Book book;

	String id;

	public String execute() throws Exception {
		book = new Book(); // book = bookDAO.findById(id, Book.class);
		book.setTitle("My book title ..............");
		if (book != null)
			return "success";
		return "notFound";
	}

	public Book getBook() {
		return this.book;
	}

	public void setId(String id) {
		this.id = id;
	}
}


package com.jamesby.xwork.helloworld;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;

public class MyResult implements Result {

	public void execute(ActionInvocation invocation) {
		System.out.println("my result is invoked..............");
	}

}
0 请登录后投票
   发表时间:2007-05-31  
大哥你的程序  能跑对吗?
0 请登录后投票
   发表时间:2007-05-31  
具体说一下你的 配置文件怎么放,都在什么位置, !!
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics