modelviewaction.java
来自「一个非常好的FRAMWRK!是一个外国组织做的!不!」· Java 代码 · 共 118 行
JAVA
118 行
/**
* 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 org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jdon.util.Debug;
import com.jdon.container.finder.ComponentKeys;
import com.jdon.controller.WebAppUtil;
import com.jdon.controller.model.Model;
import com.jdon.model.ModelForm;
import com.jdon.model.ModelManager;
import com.jdon.strutsutil.util.CreateViewPageUtil;
import com.jdon.strutsutil.util.EditeViewPageUtil;
import javax.servlet.ServletContext;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.ModuleUtils;
/**
* this class can be invoked in browser's url
* this class will push create/insert view page
* or editable view page.
*
* the push result is according action's value:
* 1. null or create ; will push a view page that user can create;
* 2. edit; will push a editbale view page that has existed data;
*
*how to use this class?
* 1. this class can be configured in struts-config.xml
*
* <action name="productForm" type="com.jdon.strutsutil.ModelViewAction" validate="false" scope="request" path="/admin/productAction">
* <forward name="create" path="/customer.jsp" />
* <forward name="edit|add|remove" path="/customer.jsp" />
* </action>
* forward's value is same as action's value
*
* 2. setup in ApplicationResources.propeties
* id.notfound =not found the record (in your language)
*
* @author banq
*/
public class ModelViewAction extends Action {
private final static String module = ModelViewAction.class.getName();
private ModuleUtils moduleUtils = ModuleUtils.getInstance();
protected ModelManager modelManager;
protected CreateViewPageUtil createViewPage;
protected EditeViewPageUtil editeViewPage;
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());
String action = request.getParameter("action");
if ( (action == null) || action.equalsIgnoreCase(ModelForm.CREATE_STR)) {
Debug.logVerbose("--> enter create process ", module);
createViewPage.doCreate(actionMapping, actionForm, request, moduleConfig);
return actionMapping.findForward(ModelForm.CREATE_STR);
} else if (!FormBeanUtil.validateAction(action, actionMapping))
throw new Exception("this action:" + action + " is invalid!");
else {
Debug.logVerbose("--> enter " + action + " process ", module);
Model model = editeViewPage.getModelForEdit(actionMapping, actionForm,
request, moduleConfig);
if (model == null) { //查询失败
if (actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME) != null)
return actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME);
else {
ActionMessages errors = new ActionMessages();
ActionMessage error = new ActionMessage("id.notfound");
errors.add(ActionErrors.GLOBAL_MESSAGE, error);
saveErrors(request, errors);
return actionMapping.findForward(action);
}
} else
return actionMapping.findForward(action);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?