📄 actionsmanager.java
字号:
// Copyright 2005-2007 onetsoft.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.onetsoft.fastjsp;
import com.onetsoft.fastjsp.util.ComboKey;
import com.onetsoft.fastjsp.util.StringUtils;
import com.onetsoft.fastjsp.util.UCIGenerator;
import com.onetsoft.fastjsp.valid.ValidationException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
class ActionsManager {
private Map urlActionPool = new HashMap(2);
private Map formActionPool = new HashMap(2);
public ActionsManager(Class pageClass) {
initActionManagers(pageClass);
}
private void initActionManagers(Class pageClass) {
try {
initializeComponentActions(getInitComponentInstance(pageClass), new UCIGenerator());
} catch (InstantiationException e) {
throw new ApplicationRuntimeException(e);
} catch (IllegalAccessException e) {
throw new ApplicationRuntimeException(e);
} catch (NoSuchMethodException e) {
throw new ApplicationRuntimeException(e);
} catch (InvocationTargetException e) {
throw new ApplicationRuntimeException(e);
}
}
private static AbstractComponent getInitComponentInstance(Class componentClass) throws IllegalAccessException, InstantiationException {
return (AbstractComponent) componentClass.newInstance();
}
/**
* 初始化当前组件及下属组件的url actions
*
* @param component
* @param uci
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void initializeComponentActions(AbstractComponent component, UCIGenerator uci) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
uci.next();
//load url actions for each component
ComboKey key = new ComboKey(uci.getValue(), component.getClass());
urlActionPool.put(key, loadUrlActionManager(component));
//load form actions for each component
formActionPool.put(key, loadFormActionManager(component));
//init children components' url actions
Class[] a = component.getComponents();
if (a == null)
return;
for (int i = 0; i < a.length; i++) {
initializeComponentActions(getInitComponentInstance(a[i]), uci);
}
}
private UrlActionManager loadUrlActionManager(AbstractComponent component) {
return new UrlActionManager(component);
}
private FormActionManager loadFormActionManager(AbstractComponent component) {
return new FormActionManager(component);
}
/**
* 获取表单实例
*
* @param key
* @param formName
* @return
*/
AbstractForm getFormInstance(ComboKey key, String formName) {
return ((FormActionManager) formActionPool.get(key)).getForm(formName);
}
/**
* 执行任何可能存在的action
* 注:
* 1.url传入的action参数名为action
* 2.表单传入的action参数名为action_XXXX。此名称为提交button的"name"
* 同时应注意大小写敏感
*
* @param pageService
* @see com.onetsoft.fastjsp.Configuration#getActionName() 页面 action 名称配置
*/
void execute(AbstractPageService pageService) throws ApplicationRuntimeException {
try {
//执行可能存在的 form action
if (pageService.formRequest) {
if (!StringUtils.isEmpty(pageService.data.getString(StringUtils.FORM))) // throw new IllegalArgumentException("Cannot find form action parameter \"form\" ,forget to set ?");
executeFormAction(pageService.cycle);
}
//执行可能存在的 url action
else {
if (checkActionExists(pageService)) {
executeUrlAction(pageService);
}
}
} catch (ApplicationRuntimeException e) {
if (e.getCause() instanceof ValidationException) {
pageService.delegateCombo.addDelegate(((ValidationException) e.getCause()).getDelegate());
} else
throw e;
}
}
/**
* url传入的action参数名为action
*
* @param pageService
* @return
*/
private static boolean checkActionExists(AbstractPageService pageService) {
return pageService.data.getString(pageService.servicer.module.actionName).length() > 0;
}
/**
* 执行url action
*
* @param pageService
*/
private void executeUrlAction(AbstractPageService pageService) {
int uci = pageService.data.getInt(LinkImpl.UCI, 0);
if (uci == -1) return;
ComboKey k = new ComboKey(uci, null);
UrlActionManager manager = (UrlActionManager) urlActionPool.get(k);
if (manager != null)
manager.executeAction(pageService.cycle);
}
/**
* 执行 form action
*
* @param cycle
*/
private void executeFormAction(AbstractPageCycle cycle) {
int uci = cycle.service.data.getInt(LinkImpl.UCI, 0);
ComboKey k = new ComboKey(uci, null);
FormActionManager manager = (FormActionManager) formActionPool.get(k);
if (manager != null)
manager.executeAction(cycle);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -