封装标签-1

1.common类

//1.Component:提供空的构造函数
//method:start,end, useBody

public 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,继承Component
public abstract class CommonBean extends Component{
	//构造函数
	public CommonBean(HttpServletRequest request,HttpServletResponse response){
		this.request=request;
		this.response=response;
	}
	//抽象动作类,获取模板
	protected abstract String getTemplate();
	
	protected HttpServletRequest request;  //request
	protected HttpServletResponse response;//response
	protected Map<String,Object> dynamicAttributes=new HashMap<String,Object>();
	
	//基本的bean属性
	protected String id;    //id
	protected String name;  //name
	protected 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,实现DynamicAttributes
public 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);
	}
	
	//注入component
	protected 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(){
		//上下文 bodyContent
		if(bodyContent == null){
			return "";
		}else{
			return bodyContent.getString().trim();
		}
	}
	//基本的bean属性
	protected String id;    //id
	protected String name;  //name
	protected 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强转为CommonBean
		CommonBean 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对象 AttachmentUpload
public 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 对象 AttachmentVO
public 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";//设置编码
	//默认上传的字段名称为id
	protected String fieldName="id";
	protected String finishCallback; 
	protected String deleteCallback;
	protected Boolean required=false;
	
	//重写自定义参数 populateParams
	protected 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值