📄 formbeanutil.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;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.jdon.controller.events.EventModel;
import org.apache.commons.beanutils.PropertyUtils;
import com.jdon.util.Debug;
import com.jdon.controller.events.EventSupport;
import java.security.Principal;
import com.jdon.controller.model.Model;
import com.jdon.util.Debug;
import com.jdon.model.ModelForm;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.FormBeanConfig;
/**
* 工具类,相关ActionForm 或Model之类的工具箱
*
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p>Company: </p>
* @author banq
* @version 1.0
*/
public final class FormBeanUtil {
public final static String module = FormBeanUtil.class.getName();
public final static String FORWARD_SUCCESS_NAME = "success";
public final static String FORWARD_FAILURE_NAME = "failure";
/**
* 将ActionForm保存在struts_config.xml定义的attribute中
* @param form
* @param mapping
* @param request
*/
public static void saveActionForm(ActionForm form, ActionMapping mapping,
HttpServletRequest request) {
if ( (form != null) && (mapping.getAttribute() != null)) {
if ("request".equals(mapping.getScope())) {
request.setAttribute(mapping.getAttribute(), form);
} else {
HttpSession session = request.getSession();
session.setAttribute(mapping.getAttribute(), form);
request.setAttribute(mapping.getAttribute(), form);
}
}
}
/**
* 将保存在struts_config.xml定义的attribute中ActionForm取出
* @param form
* @param mapping
* @param request
*/
public static ActionForm loadActionForm(ActionMapping mapping,
HttpServletRequest request) {
if ("request".equals(mapping.getScope())) {
return (ActionForm) request.getAttribute(mapping.getAttribute());
} else {
HttpSession session = request.getSession();
return (ActionForm) session.getAttribute(mapping.getAttribute());
}
}
/**
* 删除保存在attribute中的ActionForm实例
* @param mapping
* @param request
*/
public static void removeActionForm(ActionMapping mapping,
HttpServletRequest request) {
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else {
HttpSession session = request.getSession();
session.removeAttribute(mapping.getAttribute());
request.removeAttribute(mapping.getAttribute());
}
}
}
public static String getFormName(ActionMapping mapping) throws Exception{
String formName = "NoFormName Error!";
if (mapping.getName() != null)
formName = mapping.getName();
else if ( (mapping.getAttribute() != null))
formName = mapping.getAttribute();
else
throw new Exception("not found the attribute or name in action config");
return formName;
}
/**
* 根据struts-config.xml配置立即创建ActionForm
* @param actionMapping ActionMapping
* @param actionForm ActionForm
* @param request HttpServletRequest
* @param moduleConfig ModuleConfig
* @return ModelForm
* @throws Exception
*/
public static ModelForm createModelFormNow(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
ModuleConfig moduleConfig) throws
Exception {
ModelForm form = null;
String formName = null;
String formClass = null;
try{
formName = FormBeanUtil.getFormName(actionMapping);
FormBeanConfig formConfig = moduleConfig.findFormBeanConfig(formName);
if (formConfig == null){
throw new Exception(" not found config for " + formName);
}
formClass = formConfig.getType();
Class newClass = Class.forName(formClass);
form = (ModelForm)newClass.newInstance();
FormBeanUtil.saveActionForm(form, actionMapping, request);
}catch(Exception ex){
Debug.logError(" formName:" + formName +
"formClass create error :" + formClass + ex, module);
}
return form;
}
public static boolean validateAction(String actionName, ActionMapping mapping) {
boolean res = true;
int result = actionTransfer(actionName); //如果没有使用规定名称
if (result == 0)
res = false;
if (mapping.findForward(actionName) == null) //如果配置文件没有该名称
res = false;
return res;
}
public static String getName(HttpServletRequest request) throws Exception {
Principal principal = request.getUserPrincipal();
if (principal == null) {
Debug.logError(" No Principal", module);
throw new Exception(" No Principal");
}
return principal.getName();
}
public static ActionErrors notNull(Object object, String errorsInfo) {
ActionErrors errors = new ActionErrors();
String Id = (String) object;
if (object == null) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(errorsInfo));
}
return errors;
}
/**
* create a Event
*/
public static EventModel createEvent(ModelForm form, Model model) throws
Exception {
EventModel em = new EventModel();
em.setActionName(module);
try {
PropertyUtils.copyProperties(model, form);
em.setModel(model);
String action = form.getAction();
em.setActionType(FormBeanUtil.actionTransfer(action));
} catch (Exception ex) {
Debug.logError("create Event error:" + ex, module);
throw new Exception(ex);
}
return em;
}
/**
* 将字符串数据操作类型转为数字型,提高以后的处理速度
* @param actionName
* @return
*/
public static int actionTransfer(String actionName) {
if (actionName.equalsIgnoreCase(ModelForm.CREATE_STR))
return EventSupport.CREATE;
else if (actionName.equalsIgnoreCase(ModelForm.EDIT_STR))
return EventSupport.EDIT;
else if (actionName.equalsIgnoreCase(ModelForm.UPDATE_STR))
return EventSupport.UPDATE;
else if (actionName.equalsIgnoreCase(ModelForm.DELETE_STR))
return EventSupport.DELETE;
else if (actionName.equalsIgnoreCase(ModelForm.VIEW_STR))
return EventSupport.VIEW;
else if (actionName.equalsIgnoreCase(ModelForm.ADD_STR))
return EventSupport.ADD;
else if (actionName.equalsIgnoreCase(ModelForm.REMOVE_STR))
return EventSupport.REMOVE;
else
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -