📄 commonmbean.java.svn-base
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.s7turn.common;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import javax.faces.context.FacesContext;import javax.faces.model.SelectItem;/** * * @author Long */public abstract class CommonMBean<T> { protected T instance; public CommonMBean() { instance = createInstance(); } protected String getIdKey() { return "id"; } protected String getIdProperty() { return "Id"; } protected String getFoundAction() { return "found"; } protected String getSaveAction() { return "saved"; } protected String getRemoveAction() { return "removed"; } public abstract CommonService<T> getService(); public String findById() { Long id = Long.parseLong( getParameter(getIdKey()) ); instance = getService().findById(id); return getFoundAction(); } public String save() { getService().save(getInstance()); return getSaveAction(); } public String remove() { T t = getInstance(); Long id = Long.parseLong(getParameter(getIdKey())); setBeanProperty(t, getIdProperty(), id ); getService().remove(t); return getRemoveAction(); } public List getAllItems() { return getService().getAllItems(); } protected abstract T createInstance(); public String getParameter( String param ) { FacesContext fctx = FacesContext.getCurrentInstance(); String tempId = fctx.getExternalContext().getRequestParameterMap().get( param ); return tempId; } public Long getLongParameter(String param, long defaultValue) { try{ return Long.parseLong(getParameter(param)); }catch(Exception e) { } return new Long(defaultValue); } public T getInstance() { return instance; } public void setInstance(T t) { instance = t; } protected void setBeanProperty( Object bean, String prop, Object objvalue ) { try { Method method = bean.getClass().getMethod("set" + prop); method.invoke(bean, new Object[]{objvalue}); } catch (IllegalAccessException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } } protected Object getProperty( Object bean, String prop, Object defaultValue ) { try { Method method = bean.getClass().getMethod("get" + prop, new Class[0]); Object obv = method.invoke(bean, new Object[0]); return obv; } catch (IllegalAccessException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(CommonMBean.class.getName()).log(Level.SEVERE, null, ex); } return defaultValue; } protected SelectItem[] toSelectItems(List list, String propValue, String propLabel, String propDisabled) { int allSize = list == null ? 0 : list.size(); SelectItem[] items = new SelectItem[ allSize ]; int i = 0; for( Object obj : list ) { Object objKey = getProperty( obj, propValue, null ); if( objKey != null ) { Object objDesc = getProperty( obj, propLabel, null ); items[i] = new SelectItem(); items[i].setLabel(objDesc.toString()); items[i].setValue( objKey ); if( propDisabled != null ) { Boolean disb = (Boolean) getProperty( obj, propDisabled, new Boolean(false) ); items[i].setDisabled(disb.booleanValue()); } i ++; } } return items; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -