⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajaxengineaction.java

📁 EasyJWeb是基于java技术
💻 JAVA
字号:
package com.easyjf.web.ajax;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.velocity.test.provider.Person;

import com.easyjf.util.CommUtil;
import com.easyjf.web.IWebAction;
import com.easyjf.web.Module;
import com.easyjf.web.Page;
import com.easyjf.web.WebForm;
import com.easyjf.web.core.FrameworkEngine;
import com.easyjf.web.validate.ValidatorManager;
/**
 * 处理远程脚本调用的Action引擎
 * @author 大峡
 *
 */

public class AjaxEngineAction implements IWebAction {	
	public Page execute(WebForm form, Module module) throws Exception {
		Page forward = null;
		String command = CommUtil.null2String(form.get("easyJWebCommand"));
		if("".equals(command))command=CommUtil.null2String(form.get("cmd"));
		boolean getAjaxStub=false;
		if ("".equals(command)) {

		} else {
			if (command.endsWith(".js"))
			{
				command = command.substring(0, command.length() - 3);
				getAjaxStub=true;
			}
			Class[] paras = new Class[2];
			paras[0] = WebForm.class;
			paras[1] = Module.class;
			String cmd = "do" + command.substring(0, 1).toUpperCase()
					+ command.substring(1);
			try{
			Method m = this.getClass().getMethod(cmd, paras);
			if (m != null) {
				Object[] objs = new Object[2];
				objs[0] = form;
				objs[1] = module;
				Object ret = m.invoke(this, objs);
				if (ret instanceof Page)
					forward = (Page) ret;
			}
			}
			catch(java.lang.NoSuchMethodException e)
			{
				if(getAjaxStub)
				{
					return doAjaxHome(form,module);
				}
				throw new Exception("方法名称不正确,在" + this.getClass() + "中找不到"
						+ cmd + "方法!请确认您页面中的easyJWebCommand参数值是否正确!");
			}				
		}
		return forward;
	}

	/**
	 * 生成ajax调试页面
	 * 
	 * @param form
	 * @param module
	 * @return js骨架模板
	 * @throws Exception
	 */
	public Page doAjaxHome(WebForm form, Module module) throws Exception {
		String command = CommUtil.null2String(form.get("easyJWebCommand"));
		if (command.endsWith(".js"))
		{
			command = command.substring(0, command.length() - 3);
		}
		Object bean=AjaxUtil.getServiceContainer().getService(command);		
		//System.out.println(command);
		form.addResult("scriptName", command);
		form.addResult("methodList", AjaxUtil.getAjaxStub(bean));
		return module.findPage("stub");
	}

	/**
	 * 显示engine脚本引擎
	 * 
	 * @param form
	 * @param module
	 * @return engine-js视图模板
	 * @throws Exception
	 */
	public Page doEngine(WebForm form, Module module) throws Exception {
		return module.findPage("engine-js");
	}
	public Page doDWRUtil(WebForm form, Module module) throws Exception {
		return module.findPage("engine-util");
	}
	public Page doUtil(WebForm form, Module module) throws Exception {
		return module.findPage("engine-util");
	}
	public Page doPrototype(WebForm form, Module module) throws Exception {
		return module.findPage("prototype");
	}
	public Page doRico(WebForm form, Module module) throws Exception {
		return module.findPage("rico");
	}
	public Page doValidate(WebForm form, Module module) throws Exception {
		String result="{";			
		try
		{
			ValidatorManager vm=FrameworkEngine.findValidatorManager();	
			if(vm!=null)
			{
				String name=CommUtil.null2String(form.get("validateObjectName"));
				String property=CommUtil.null2String(form.get("validateField"));
			
				if(!"".equals(name))
				{
					form.toPo(Person.class);
				}				
				if(vm.getErrors().hasError())
				{
					form.addResult("error",true);
						
					java.util.Iterator it=vm.getErrors().getErrors().entrySet().iterator();
					while(it.hasNext())
					{
					Map.Entry en=(Map.Entry)it.next();
					if("".equals(property)||property.equals(en.getKey()))
					result+=en.getKey()+":\""+en.getValue()+"\",";			
					}
					if(result.endsWith(","))result=result.substring(0,result.length()-1);
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		result+="}";		
		form.addResult("result", result);
		return module.findPage("plain");
	}
	
	/**
	 * 生成并显示支持远程调用服务器端对象的JS存根
	 * 
	 * @param form
	 * @param module
	 * @return 存根显示模板
	 * @throws Eception
	 */
	public Page doAjaxStub(WebForm form, Module module) throws Exception {
		
		return null;
	}

	public Page doAjaxTest(WebForm form, Module module) throws Exception {
		return null;
	}

	/**
	 * 执行Ajax调用
	 * 
	 * @param form
	 * @param module
	 * @return 调用结果显示模板
	 * @throws Eception
	 */
	public Page doAjaxCall(WebForm form, Module module) throws Exception {
		String methodName = (String) form.get("methodName");
		String scriptName=(String)form.get("scriptName");
		//String ajaxCallId=(String)form.get("remoteCallId");
		RemoteCall remoteCall=new RemoteCall(scriptName,methodName,form.getTextElement());		
		try{				
		form.addResult("result", remoteCall.execute());	
		}		
		catch(Exception e)
		{			
			form.addResult("error","true");
			e.printStackTrace();
			form.addResult("result","\""+(e.getCause().getMessage()!=null?e.getCause().getMessage():e.getMessage())+"\"");
		}		
		return module.findPage("plain");
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -