封装标签-1

来源:互联网 发布:指定变号发短信软件 编辑:程序博客网 时间:2024/06/07 05:10

1.common类

//1.Component:提供空的构造函数//method:start,end, useBodypublic boolean start(Writer writer){return true;}public boolean end(Writer writer, String body) {writer.write(body);return false;}public boolean usesBody(){return false;}//2.抽象类,公用Bean,继承Componentpublic abstract class CommonBean extends Component{//构造函数public CommonBean(HttpServletRequest request,HttpServletResponse response){this.request=request;this.response=response;}//抽象动作类,获取模板protected abstract String getTemplate();protected HttpServletRequest request;  //requestprotected HttpServletResponse response;//responseprotected Map<String,Object> dynamicAttributes=new HashMap<String,Object>();//基本的bean属性protected String id;    //idprotected String name;  //nameprotected String cssStyle; //style样式protected String cssClass; //class样式protected String value; //取值protected String title; //标题protected String tabindex;//属性可设置或返回单选按钮的 tab 键控制次序。protected String accessKey; //属性可设置或返回访问单选按钮的快捷键protected String disabled; //属性可设置或返回是否禁用//基本的事件protected String onclick;//点击事件protected String onmousedown;//当鼠标按键被按下protected String onmouseup;//当鼠标按键被松开protected String onmouseover;//当鼠标被移到某元素之上protected String onmousemove;//鼠标被移动protected String onmouseout; //鼠标被移开protected String onfocus; //元素获取焦点protected String onblur;//失去焦点protected String onkeypress;//某个按键被按下或按住protected String onkeydown;//某个按键被按下protected String onkeyup;//当按键被松开protected String onselect;//文本被选定protected String onchange; //用户改变域的内容} //3.基本的标签 CommonTag  抽象类,继承BodyTagSupport,实现DynamicAttributespublic abstract class CommonTag extends BodyTagSupport implements DynamicAttributes{//获取组件component的抽象类public abstract Component getBean(HttpServletRequest request,HttpServletResponse response);protected Map<String,Object> dynamicAttributes=new HashMap<String,Object>();//重写DynamicAttributes的setDynamicAttribute的方法//uri:是属性的namespace,如果默认空间设置为null//localName:被设置属性的名称//value:被设置属性的值public void setDynamicAttribute(String uri, String localName, Object value){dynamicAttributes.put(localName, value);}//注入componentprotected Component component;public Component getComponent(){return component;}//开始标签的动作public int doStartTag(){component=getBean((HttpServletRequest)pageContext.getRequest,(HttpServletResponse)pageContext.getResponse);populateParams();//自定义参数//eval()函数可计算某个字符串,并执行其中的JavaScript代码。boolean evalBody=component.start(pageContext.getOut());if(evalBody){return component.usesBody()? EVAL_BODY_BUFFERED : EVAL_BODY_INCLUDE;}else{return SKIP_BODY;}}//结束标签public int doEndTag(){component.end(pageContext.getOut,getBody());component=null;return EVAL_PAGE;}private String getBody(){//上下文 bodyContentif(bodyContent == null){return "";}else{return bodyContent.getString().trim();}}//基本的bean属性protected String id;    //idprotected String name;  //nameprotected String cssStyle; //style样式protected String cssClass; //class样式protected String value; //取值protected String title; //标题protected String tabindex;//属性可设置或返回单选按钮的 tab 键控制次序。protected String accessKey; //属性可设置或返回访问单选按钮的快捷键protected String disabled; //属性可设置或返回是否禁用//基本的事件protected String onclick;//点击事件protected String onmousedown;//当鼠标按键被按下protected String onmouseup;//当鼠标按键被松开protected String onmouseover;//当鼠标被移到某元素之上protected String onmousemove;//鼠标被移动protected String onmouseout; //鼠标被移开protected String onfocus; //元素获取焦点protected String onblur;//失去焦点protected String onkeypress;//某个按键被按下或按住protected String onkeydown;//某个按键被按下protected String onkeyup;//当按键被松开protected String onselect;//文本被选定protected String onchange; //用户改变域的内容//自定义参数protected void populateParams(){//将component强转为CommonBeanCommonBean bean = (CommonBean)component;bean.setId(StringUtils.isBlank(id)? UUID.getUUID() : id);bean.setName(name);bean.setCssStyle(cssStyle);bean.setCssClass(cssClass);bean.setValue(value);bean.setTitle(title);bean.setTabindex(tabindex);bean.setAccessKey(accessKey);bean.setDisabled(disabled);bean.setOnclick(onclick);bean.setOnkeydown(onkeydown);bean.setOnmouseup(onmouseup);bean.setOnmouseover(onmouseover);bean.setOnmousemove(onmousemove);bean.setOnmouseout(onmouseout);bean.setOnfocus(onfocus);bean.setOnblur(onblur);bean.setOnkeypress(onkeypress);bean.setOnkeydown(onkeydown);bean.setOnkeyup(onkeyup);bean.setOnselect(onselect);bean.setOnchange(onchange);bean.setDynamicAttributes(dynamicAttributes);}}

2.附件attachment

//1.附件上传的Bean对象 AttachmentUploadpublic class AttachmentUpload extends CommonBean{//有参的构造函数public AttachmentUpload(HttpServletRequest request, HttpServletResponse response){super(request,response);}//实现抽象方法final public static String TEMPLATE="attachmentUpload";protected String getTemplate(){return TEMPLATE;}//自定义的参数protected String configCode;protected String className;protected String filedName;protected String ownerId;protected String finishCallback;  //结束后的回调方法protected String deleteCallback;  //删除后的回调方法protected Boolean required;//是否必须}//2.查看附件的Bean对象public class AttachmentView extends CommonBean{//重写构造函数public AttachmentView(HttpServletRequest request,HttpServletResponse response){super(request,response);}final public static TEMPLATE="attachmentView";protected String getTemplate(){return TEMPLATE;}protected List<AttachmentVO> attachmentVOs;protected String zipUrl;}//3.JAVA VO 对象 AttachmentVOpublic class AttachmentVO{private String id;private String name;private String downloadUrl;//下载路径private String picUrl;//图片路径private String audioUrl;//视频路径}//4.标签Tag,AttachemntUploadTag  附件上传的标签public class AttachemntUploadTag extends CommonTag{//重写抽象方法public Component getBean(HttpServletRequest request,HttpServletResponse response){return new AttachmentUpload(request,response);}//自定义属性protect Object model=null;protected String configCode="default";//设置编码//默认上传的字段名称为idprotected String fieldName="id";protected String finishCallback; protected String deleteCallback;protected Boolean required=false;//重写自定义参数 populateParamsprotected void populateParams(){super.populateParams();String className="";String ownerId="";Object obj=model;if(obj==null){obj=pageContext.findAttribute("model");}//获取对象的名称className=obj.getClass().getName();//利用反射获取对象属性的值,(获取附件所属的对象的id--fieldName)Object ownerIdObj=ReflectUtil.getFieldValue(obj,fieldName);if(ownerIdObj!=null){ownerId=ownerIdObj.toString();}//commonTag里面有自己的component((AttachmentUpload) component).setConfigCode(configCode);((AttachmentUpload) component).setClassName(className);((AttachmentUpload) component).setFieldName(fieldName);((AttachmentUpload) component).setOwnerId(ownerId);((AttachmentUpload) component).setFinishCallback(StringUtils.isBlank(finishCallback) ? "" : finishCallback);((AttachmentUpload) component).setDeleteCallback(StringUtils.isBlank(deleteCallback) ? "" : deleteCallback);((AttachmentUpload) component).setRequired(required);}}//4.标签Tag,AttachmentViewTag  附件查看的标签public class AttachmentViewTag extends commonTag{//重写抽象方法public Component getBean(HttpServletRequest request,HttpServletResponse response){return new AttachmentView(request,response);}private Object model;private String fieldName="id";private String period="year";private Boolean showAll=true;private Boolean showZipUrl=true;private Boolean checkUser;protected void populateParams(){super.populateParams();Object obj=model;if(obj==null){obj=pageContext.findAttribute("model");}Object ownerId = ReflectUtil.getFieldValue(obj,fieldName);if(ownerId!=null){AttachmentService attachmentService=SpringContextHolder.getBean(AttachmentService.class);List<Attachment> attachments=attachmentService.getAttachmentsWithOwnerId(ownerId.toString(),AttachmentConstant.STATUS_ABLE);List<AttachmentVO> attachmentVOs=new ArrayList<AttachmentVO>();String zipUrl="";if(showAll){if(attachments.size()>0){for(Attachment att : attachments){AttachmentVO vo=new AttachmentVO();vo.setName(att.getSourceFilename());String downloadUrl = AttachmentUtil.getDownloadUrl(attr,checkUser,period);vo.setDownloadUrl(downloadUrl);vo.setId(att.getId());attachmentVOs.add(vo);}}}((AttachmentView) component).setAttachmentVOs(attachmentVOs);if(showZipUrl){if(attachments.size()>0){zipUrl=AttachmentUtil.getZipDownloadUrl(ownerId,checkUser,period);}((AttachmentView)component).setZipUrl(zipUrl);}}}}//5.标签AttachmentUrlTag,直接继承BodyTagSupport,重写doStartTag,doEndTag标签public class AttachmentUrlTag extends BodyTagSupport{private Object model = null;private String fieldName = "id";private Boolean checkUser=false;private String var;private String period="year";public int doStartTag(){return EVAL_BODY_BUFFERED;}public int doEndTag(){Object obj=model;if(obj == null){obj=pageContext.findAttribute("model");}Object ownerId=ReflectUtil.getFieldValue(obj,fieldName);List<AttachmentVO> attachmentVOs = new ArrayList<AttachmentVO>();if(ownerId!=null){AttachmentService attachmentService=SpringContextHolder.getBean(AttachmentService.class);List<Attachment> attachments = attachmentService.getAttachmentsWithOwnerId(ownerId.toString(),AttachmentConstant.STATUS_ABLE);if (attachments.size() > 0) {for (Attachment att : attachments) {AttachmentVO vo = new AttachmentVO();vo.setName(att.getSourceFilename());String downloadUrl = AttachmentUtil.getDownloadUrl(att, checkUser, period);String picUrl = AttachmentUtil.getPicUrl(att, checkUser, period);String audioUrl = AttachmentUtil.getAudioUrl(att, checkUser, period);vo.setDownloadUrl(downloadUrl);vo.setPicUrl(picUrl);vo.setAudioUrl(audioUrl);vo.setId(att.getId());attachmentVOs.add(vo);}}}pageContext.getRequest.setAttribute(var,attachmentVOs);return EVAL_PAGE;}}


0 0
原创粉丝点击