📄 modelhandler.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.model;
import javax.servlet.http.HttpServletRequest;
import com.jdon.controller.model.Model;
import com.jdon.controller.events.EventModel;
import com.jdon.util.Debug;
import org.apache.commons.beanutils.PropertyUtils;
import com.jdon.model.mapping.ModelMapping;
/**
* Model和ModelForm处理总类
*
* 本类实现了Action中有关Model和ModelForm的具体应用功能,需要具体程序
* 继承本类,然后在modelmapping.xml配置如下语句:
*
*
<modelmapping formName = "productForm"
key="productId"
model="com.jdon.estore.model.Product"
handler = "com.jdon.estore.web.catalog.ProductHandler" />
* 其中的ProductHandler就是本类的具体实现
*
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p></p>
* @author banq
* @version 1.0
*/
public abstract class ModelHandler {
private final static String module = ModelHandler.class.getName();
protected ModelMapping modelMapping;
public void setModelMapping(ModelMapping modelMapping) {
this.modelMapping = modelMapping;
}
public ModelMapping getModelMapping() {
return modelMapping;
}
/**
* 初始化“显示”表单数据时的ModelForm,
* 只适合create页面初始化
*
* 注:“显示”指:如创建页面或编辑页面,该功能只对ModelViewAction,且在下列
* 条件时有效:
* ModelViewAction的配置attribute值,如attribute="modelForm";
* 如配置name="modelForm",则无效,因为Struts自动生成ModelForm实例。
*
* 本方法对ModelSaveAction无效:
* ModelSaveAction的ModelForm是由配置name="modelForm",根据modelForm的ActionForm
* 值由Struts自动生成的,该modelForm实例保存有用户提交的数据。
*
* @param request
* @return ModelForm
* @throws java.lang.Exception
*/
public abstract ModelForm initForm(HttpServletRequest request) throws
Exception;
/**
* 定制Form
* 缺省为空
* 用于组合一个页面中多个Form
*
* @param form
* @param request
* @return ModelForm
* @throws java.lang.Exception
*/
public void customizeForm(ModelForm form, HttpServletRequest request)
throws Exception{
}
/**
* 从后台根据keyValue获取已经存在的一个Model实例
* @param keyValue
* @param request
* @return Model
* @throws java.lang.Exception
*/
public abstract Model findModelByKey(String keyValue,
HttpServletRequest request) throws
Exception;
/**
* 提交到后台,由后台如EJB处理用户提交的表单数据
* 常用有:在数据库创建数据;修改数据或删除数据。
* @param em
* @param request
* @throws java.lang.Exception
*/
public abstract void serviceAction(EventModel em, HttpServletRequest request) throws
Exception;
/**
* 将Model复制到ModelForm,使用PropertyUtils.copyProperties命令
* 本方法可以override,因为有时,Model和ModelForm并不完全一样,
* 可能需要具体程序处理。
* @param model
* @param form
* @throws java.lang.Exception
*/
public void modelCopyToForm(Model model, ModelForm form) throws Exception {
try {
//数据对象拷贝
PropertyUtils.copyProperties(form, model);
} catch (Exception e) {
e.printStackTrace();
Debug.logError(" formCopyToModel error:" + e, module);
throw new Exception(" formCopyToModel error:");
}
}
/**
* 将ModelForm复制到Model,使用PropertyUtils.copyProperties命令
* 本方法可以override,因为有时,Model和ModelForm并不完全一样,
* 可能需要具体程序处理。
* @param model
* @param form
* @throws java.lang.Exception
*/
public void formCopyToModel(ModelForm form, Model model) throws Exception {
try {
//数据对象拷贝
PropertyUtils.copyProperties(model, form);
} catch (Exception e) {
e.printStackTrace();
Debug.logError(" formCopyToModel error:" + e , module);
throw new Exception(" formCopyToModel error:");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -