modeldispaction.java
来自「一个非常好的FRAMWRK!是一个外国组织做的!不!」· Java 代码 · 共 129 行
JAVA
129 行
/**
* Copyright 2003-2005 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;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.ModuleUtils;
import com.jdon.container.finder.ComponentKeys;
import com.jdon.controller.WebAppUtil;
import com.jdon.controller.model.Model;
import com.jdon.model.ModelHandler;
import com.jdon.model.ModelKey;
import com.jdon.model.ModelManager;
import com.jdon.strutsutil.util.CreateViewPageUtil;
import com.jdon.strutsutil.util.EditeViewPageUtil;
import com.jdon.util.Debug;
/**
* 专门显示Model类。
*
* 为安全起见,单独设立一个类。同时加入缓存机制。
* request的nocache参数表示失效缓存机制。
* modelDispAction.do?nocache
*
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p></p>
* @author banq
* @version 1.0
*/
public class ModelDispAction extends ModelViewAction {
private final static String module = ModelDispAction.class.getName();
private ModuleUtils moduleUtils = ModuleUtils.getInstance();
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) throws
Exception {
if (modelManager == null) {
ServletContext sc = this.getServlet().getServletContext();
modelManager = (ModelManager) WebAppUtil.getComponentInstance(ComponentKeys.MODEL_MANAGER, sc);
createViewPage = new CreateViewPageUtil(modelManager);
editeViewPage = new EditeViewPageUtil(modelManager);
}
ModuleConfig moduleConfig = moduleUtils.getModuleConfig(request, getServlet().getServletContext());
Model model = this.editeViewPage.getModelForEdit(actionMapping, actionForm,
request, moduleConfig);
if (model == null) { //如果查询失败,显示出错信息
ActionMessages errors = new ActionMessages();
Debug.logError("id.notfound in database", module);
ActionMessage error = new ActionMessage("id.notfound");
errors.add(ActionMessages.GLOBAL_MESSAGE, error);
saveErrors(request, errors);
ActionForward af = actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME);
if (af != null) return af;
else return actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME);
} else {
return actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME);
}
}
/**
* 首先查询缓存,然后从后台获取已经存在的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 = this.editeViewPage.getParamKeyValue(actionMapping,
request, modelHandler);
if (paramKeyValue == null) {
Debug.logVerbose(
" the keyValue is null, please finish it in findModelByKey method",
module);
}
String nocache = request.getParameter("nocache");
String formName = actionMapping.getAttribute();
Model model = null;
if (nocache != null) {
model = modelHandler.findModelByKey(paramKeyValue, request);
} else {
//特别是 先从缓存获得。
ModelKey modelKey = new ModelKey(paramKeyValue, formName);
model = modelManager.getCache(modelKey);
if (model == null) {
model = modelHandler.findModelByKey(paramKeyValue, request);
modelManager.addCache(modelKey, model);
}
}
return model;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?