📄 modellistaction.java
字号:
/**
* Copyright 2005 Jdon.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.jdon.strutsutil;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jdon.util.Debug;
import java.util.*;
import com.jdon.controller.model.Model;
import com.jdon.controller.model.PageIterator;
import com.jdon.model.ModelManager;
import com.jdon.model.ModelKey;
import com.jdon.controller.WebAppUtil;
import javax.servlet.ServletContext;
import com.jdon.model.ModelManagerRegistry;
import com.jdon.controller.ContainerClient;
/**
* 用于批量查询
* 需要继承实现,与ModelListForm结合使用。
* 可将ModelListForm作为DynaActionForm一个属性。
*
*
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p></p>
* @author banq
* @version 1.0
*/
public abstract class ModelListAction extends Action {
private final static String module = ModelListAction.class.getName();
private ContainerClient containerClient = new ContainerClient();
private ModelManager modelManager;
private boolean cacheEnable = true;
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) throws
Exception {
if (modelManager == null){
ServletContext sc = this.getServlet().getServletContext();
modelManager = (ModelManager)containerClient.getPOJOService(ModelManagerRegistry.MM_NAME, sc);
}
int start = 0;
int count = 20;
String startStr = request.getParameter("start");
if ( (startStr != null) && (startStr.length() != 0)) {
start = Integer.parseInt(startStr);
}
String countStr = request.getParameter("count");
if ( (countStr != null) && (countStr.length() != 0)) {
count = Integer.parseInt(countStr);
}
PageIterator pageIterator = getPageIterator(request, start, count);
if (pageIterator == null) {
throw new Exception(
"getPageIterator erorr, the result is null! check EJB ..");
}
ModelListForm listForm = getModelListForm(actionMapping, actionForm,
request, pageIterator);
listForm.setStart(start);
listForm.setCount(count);
listForm.setAllCount(pageIterator.getAllCount());
listForm.setHasNextPage(pageIterator.isNextPageAvailable());
Collection c = getModelList(request, pageIterator);
Debug.logVerbose(" listForm 's property: getList size is " + c.size(), module);
listForm.setList(c);
listForm.setOneModel(setupOneModel(request));
customizeListForm(actionMapping, actionForm, request, listForm);
if (actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME) == null)
Debug.logError(
"not found the forward name '" + FormBeanUtil.FORWARD_SUCCESS_NAME +
"' in struts-config.xml", module);
return actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME);
}
/**
* 获得ModelListForm实例
*
* @param actionMapping
* @param actionForm
* @param request
* @return
* @throws java.lang.Exception
*/
public ModelListForm getModelListForm(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
PageIterator pageIterator) throws
Exception {
ModelListForm modelListForm = null;
if (actionForm == null){
modelListForm = new ModelListForm();
FormBeanUtil.saveActionForm(modelListForm, actionMapping, request);
}else if (actionForm instanceof ModelListForm)
modelListForm = (ModelListForm) actionForm;
if (modelListForm == null)
throw new Exception(
"not found the bean of com.jdon.strutsutil.ModelListForm");
else
return modelListForm;
}
/**
* 根据PageIterator中ID集合,获得相应的Model集合
* @param request
* @param pageIterator
* @return Model集合
* @throws java.lang.Exception
*/
private List getModelList(HttpServletRequest request,
PageIterator pageIterator) throws Exception {
List list = new ArrayList(pageIterator.getSize());
Model model = null;
Class modelClass = null;
while (pageIterator.hasNext()) {
Object dataKey = pageIterator.next();
if ( (modelClass == null) || (!cacheEnable)) { //第一个model从数据库中获取
Debug.logVerbose(" getCache from db." , module);
model = findModelByKey(request, dataKey);
if (model != null)
modelClass = model.getClass();
else
Debug.logError(model.getClass().getName() + " is null for key :" + dataKey, module);
} else {
//首先从缓存中获取
ModelKey modelKey = new ModelKey(dataKey, modelClass);
model = modelManager.getCache(modelKey);
if (model == null) {
model = fetchModel(request, dataKey);
if ( (model != null) && (model.isCacheble()))
modelManager.addCache(modelKey, model);
}
}
list.add(model);
}
pageIterator.reset();
return list;
}
/**
* 定制ModelListForm
*
* 缺省ModelListForm是只有一个List,包含一种Model集合
* 有的应用可能是两种Model集合,可以继承ModelListForm实现Map-backed ActionForms
* 再继承本Action,实现本方法。
*
* @param actionMapping
* @param actionForm
* @param request
* @param modelListForm
* @throws java.lang.Exception
*/
public void customizeListForm(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
ModelListForm modelListForm ) throws Exception{
}
/**
* 是否使用Model的Cache机制
* @param enable
*/
protected void setCache(boolean enable) {
cacheEnable = enable;
}
private Model fetchModel(HttpServletRequest request, Object dataKey) throws
Exception {
Model model = null;
try {
model = findModelByKey(request, dataKey);
if (model == null)
throw new Exception(" not found the model, Id= " + dataKey);
} catch (Exception ex) {
Debug.logError(" some ejb programs error, please check it" + ex, module);
throw new Exception("System error! please call system Admin." + ex);
}
return model;
}
protected Model setupOneModel(HttpServletRequest request) {
return null;
}
/**
* 必须实现的两个方法中之一
*
* PageIterator中包含的是Model的key或称ID集合
* PageIterator中的key(ID)集合用于findModelByKey来查询
* Model实例。
*
* @param request HttpServletRequest
* @param start int
* @param count int
* @return PageIterator
*/
public abstract PageIterator getPageIterator(HttpServletRequest request,
int start,
int count);
/**
* 必须实现的两个方法中之一
*
* 根据Model的key或称ID获取Model实例,key(ID)是getPageIterator
* 方法返回集合中的值
*
* @param request HttpServletRequest
* @param key Object
* @return Model
*/
public abstract Model findModelByKey(HttpServletRequest request, Object key);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -