📄 editeviewpage.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.util;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import com.jdon.util.Debug;
import com.jdon.controller.model.Model;
import com.jdon.model.mapping.ModelMapping;
import com.jdon.model.ModelHandler;
import com.jdon.model.ModelForm;
import com.jdon.model.ModelManager;
import com.jdon.model.ModelKey;
import com.jdon.strutsutil.FormBeanUtil;
import org.apache.struts.config.ModuleConfig;
/**
* 提供用于Model编辑页面的准备工作
* 如获得已经存在的Model
*
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p></p>
* @author banq
* @version 1.0
*/
public class EditeViewPage {
private final static String module = EditeViewPage.class.getName();
private ModelManager modelManager;
public EditeViewPage(ModelManager modelManager) {
this.modelManager = modelManager;
}
/**
* 为编辑或查询页面准备有数据的Model实例
* 调用用户自己定义的ModelHandler并运行
*
* @param actionMapping ActionMapping
* @param actionForm ActionForm
* @param request HttpServletRequest
* @param modelHandler ModelHandler
* @throws Exception
* @return Model
*/
public Model getModelForEdit(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
ModuleConfig moduleConfig) throws Exception {
Model model = null;
ModelHandler modelHandler = null;
try {
String formName = FormBeanUtil.getFormName(actionMapping);
modelHandler = modelManager.borrowtHandlerObject(formName);
//1.生成新的ActionForm,保存在atrribute中
ModelForm form = getModelForm(actionMapping,
modelHandler,
actionForm,
request,
moduleConfig);
FormBeanUtil.saveActionForm(form, actionMapping, request);
Debug.logVerbose(" got a ModelForm ... " , module);
//设定为编辑调用 注:由调用者参数决定 由action值决定,如是view,则要在配置中配置view
String action = request.getParameter("action");
if (action != null)
form.setAction(action);
else
Debug.logError(" action is null !", module);
Debug.logVerbose(" prepare to fetch a Model from backend" , module);
model = fetchModel(actionMapping, request, modelHandler);
Debug.logVerbose(" got the Model data successfully..", module);
if (model != null) //如果查询成功,数据对象拷贝
modelHandler.modelCopyToForm(model, form);
modelHandler.customizeForm(form, request);
} catch (Exception ex) {
Debug.logError("please check your ejb 、 model or form " + ex, module);
throw new Exception("System error! please call system Admin." + ex);
} finally {
modelManager.returnHandlerObject(modelHandler); //返回modelhandler再用
}
return model;
}
private ModelForm getModelForm(ActionMapping actionMapping,
ModelHandler modelHandler,
ActionForm actionForm,
HttpServletRequest request,
ModuleConfig moduleConfig) throws Exception {
Debug.logVerbose(" enter doCreate... ", module);
ModelForm form = modelHandler.initForm(request);
if (form == null)
if (actionForm != null)
form = (ModelForm) actionForm;
else
form = FormBeanUtil.createModelFormNow(actionMapping, actionForm, request,
moduleConfig);
return form;
}
/**
* 从后台获取已经存在的Model数据
* @param actionMapping
* @param request
* @param modelHandler
* @return
* @throws java.lang.Exception
*/
private Model fetchModel(ActionMapping actionMapping,
HttpServletRequest request,
ModelHandler modelHandler) throws Exception {
String paramKeyValue = getParamKeyValue(actionMapping, request, modelHandler);
if (paramKeyValue == null) {
Debug.logVerbose("the keyValue is null, maybe is saved in session", module);
}
String formName = actionMapping.getAttribute();
Model model = null;
try{
//从数据库获取。
model = modelHandler.findModelByKey(paramKeyValue, request);
if ( (model != null) && (model.isCacheble())) {
ModelKey modelKey = new ModelKey(paramKeyValue, formName);
modelManager.addCache(modelKey, model);
}
}catch(Exception ex){
Debug.logError(" Your handler happens errors, try to catch them !" + ex, module);
throw new Exception(ex);
}
return 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 String getParamKeyValue(ActionMapping actionMapping,
HttpServletRequest request,
ModelHandler modelHandler) throws
Exception {
//获得key的值
ModelMapping modelMapping = modelHandler.getModelMapping();
String keyName = modelMapping.getKeyName();
Debug.logVerbose(" the keyName is " + keyName , module);
String keyValue = request.getParameter(keyName);
Debug.logVerbose(" got the keyValue is " + keyValue , module);
return keyValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -