📄 editeviewpageutil.java
字号:
/**
* Copyright 2003-2006 the original author or authors.
* 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.util;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionMapping;
import com.jdon.controller.model.ModelIF;
import com.jdon.model.ModelForm;
import com.jdon.model.ModelHandler;
import com.jdon.model.ModelKey;
import com.jdon.model.ModelManager;
import com.jdon.model.config.ModelMapping;
import com.jdon.strutsutil.FormBeanUtil;
import com.jdon.util.Debug;
/**
* prepare for push a editable jsp page. this class will call the service to get
* a model that has datas. this work will delegate the ModelHandler class
*
*
* @author banq
*/
public class EditeViewPageUtil {
private final static String module = EditeViewPageUtil.class.getName();
protected ModelManager modelManager;
public EditeViewPageUtil(ModelManager modelManager) {
this.modelManager = modelManager;
}
/**
* two things: 1. create a ModelForm null instance 2. obtain a existed Model
* instance copy the Model instance to the ModelForm instance
*
*/
public ModelIF getModelForEdit(ActionMapping actionMapping, ModelForm modelForm, HttpServletRequest request)
throws Exception {
ModelIF model = null;
ModelHandler modelHandler = null;
try {
String formName = FormBeanUtil.getFormName(actionMapping);
modelHandler = modelManager.borrowtHandlerObject(formName);
ModelForm form = modelHandler.initForm(request);
if (form != null) {
form.setAction(ModelForm.EDIT_STR);
FormBeanUtil.saveActionForm(form, actionMapping, request);
} else {
form = modelForm;
}
Debug.logVerbose("[JdonFramework] got a ModelForm ... ", module);
Debug.logVerbose("[JdonFramework] prepare to fetch a Model from service layer", module);
model = fetchModel(request, formName, modelHandler);
Debug.logVerbose("[JdonFramework] got the Model data successfully..", module);
modelHandler.modelIFCopyToForm(model, form);
} catch (Exception ex) {
Debug.logError("[JdonFramework]please check your service 、 model or form, error is: " + ex, module);
} finally {
modelManager.returnHandlerObject(modelHandler); //返回modelhandler再用
}
return model;
}
protected ModelIF fetchModel(HttpServletRequest request, String formName, ModelHandler modelHandler) throws Exception {
ModelIF model = null;
try {
Object keyValue = getParamKeyValue(request, modelHandler);
clearModelCache(formName, keyValue, modelHandler);
model = modelHandler.findModelIF(keyValue, request);
if (model == null) {
Debug.logError("[JdonFramework] Error: got a NULL Model..", module);
throw new Exception("got a NULL Model");
} else {
addModelCache(formName, keyValue, modelHandler, model);
}
} catch (Exception ex) {
Debug.logError("[JdonFramework] the method 'findModelByKey' of your handler or 'getMethod' of service happened error: " + ex, module);
throw new Exception(ex);
}
return model;
}
private void clearModelCache(String formName, Object keyValue, ModelHandler modelHandler) {
ModelIF model = null;
try {
ModelKey modelKey = new ModelKey(keyValue, formName);
model = modelManager.getCache(modelKey);
if (model != null) {//clear the cache
modelManager.removeCache(keyValue);
}
} catch (Exception e) {
Debug.logError("[JdonFramework] clearModelCache error: " + e);
}
}
private void addModelCache(String formName, Object keyValue, ModelHandler modelHandler, ModelIF model) {
ModelKey modelKey = new ModelKey(keyValue, formName);
modelManager.addCache(modelKey, model);
}
/**
* 获得参数key值 例如: /admin/productAction.do?action=edit&productId=1721
* 缺省:productId为product的modelmapping.xml中key定义值
*
* 对于如下调用: /admin/productAction.do?action=edit&userId=16
* userId不是modelmapping.xml中key定义值,则需要override本方法,
*
*
* @param actionMapping
* @param request
* @return 参数key值
* @throws java.lang.Exception
*/
public Object getParamKeyValue(HttpServletRequest request, ModelHandler modelHandler) {
Object keyValue = null;
try {
ModelMapping modelMapping = modelHandler.getModelMapping();
String keyName = modelMapping.getKeyName();
Debug.logVerbose("[JdonFramework] the keyName is " + keyName, module);
String keyValueS = request.getParameter(keyName);
Debug.logVerbose("[JdonFramework] got the keyValue is " + keyValueS, module);
if (keyValueS == null) {
Debug.logVerbose("[JdonFramework]the keyValue is null", module);
}
Class keyClassType = modelMapping.getKeyClassType();
if (keyClassType.isAssignableFrom(String.class)) {
keyValue = keyValueS;
}else{
Debug.logVerbose("[JdonFramework] convert String keyValue to" + keyClassType.getName(), module);
keyValue = ConvertUtils.convert(keyValueS, keyClassType);
}
} catch (Exception e) {
Debug.logError("[JdonFramework] getParamKeyValue error: " + e);
}
return keyValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -