自定义标签

1、DiagramstyleHtmlTag.java类:

package com.hgs.framework.web.taglib;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hgs.framework.model.StylepropEntity;
import com.hgs.framework.model.StylepropvluEntity;
import com.hgs.framework.service.BaseStylepropManager;
import com.hgs.framework.service.BaseStylepropvluManager;
import com.hgs.framework.util.domain.ObjectUtils;
import com.hgs.framework.util.spring.ApplicationContextUtil;
/**
 * @author jianana
 * 
 */
@SuppressWarnings("unchecked")
public class DiagramstyleHtmlTag extends TagSupport {

	private static final long serialVersionUID = 851555851052867339L;

	/** 样式属性ID */
	private Integer stylepropId;

	@Override
	public int doStartTag() throws JspException {

		// 从spring而配置文件中取得ICodeListLoader实例
		ServletContext sc = pageContext.getServletContext();
		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

		BaseStylepropManager stylepropManager = (BaseStylepropManager) context.getBean("stylepropManager");
		BaseStylepropvluManager stylepropvluManager = (BaseStylepropvluManager) context.getBean("stylepropvluManager");
		StylepropEntity stylepropEntity = stylepropManager.getStylepropById(stylepropId);
		List<StylepropvluEntity> entities = stylepropvluManager.findStylepropvluListByStylepropId(stylepropId);

		if (stylepropEntity == null) {
			return SKIP_BODY;
		}
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer data = new StringBuffer();
			data.append(getFixedField(stylepropEntity, entities));
			out.print(data.toString());
			System.out.println(data.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		return SKIP_BODY;
	}

	private String getFixedField(StylepropEntity stylepropEntity, List<StylepropvluEntity> list) {
		StringBuffer buffer = new StringBuffer();
		// 1文本(长度控制,必填控制)
		// 2整形(长度控制,必填控制,范围控制,整数控制)
		// 3浮点(长度控制,必填控制,范围控制,浮点类型控制)
		// STYLEPROPVLU_ID 样式属性备选值
		// SVLU 文本输入值
		// NVLU 数值输入值
		String clazz = "";
		String fw = "";// 范围验证共用
		if (stylepropEntity.getSetflg().equals("3")) {
			fw = ",rangelength:[" + stylepropEntity.getMinvlu() + "," + stylepropEntity.getMaxvlu() + "]";
		}
		if (!stylepropEntity.getSetflg().equals("2")) {
			StringBuffer sb = new StringBuffer();
			if (stylepropEntity.getDatatyp().equals("1")) {// 文本
				String svlu = ObjectUtils.isNotEmpty(pageContext.getRequest().getParameter("svlu")) ? pageContext
						.getRequest().getParameter("svlu") : "";
				sb.append("required:true,maxlength:" + stylepropEntity.getLength()).append(fw);
				clazz = sb.toString();
				buffer.append("<input name='svlu' id='vlu' class='" + clazz + "' value='" + svlu + "' maxlength='"
						+ stylepropEntity.getLength() + "' />");
			} else if (stylepropEntity.getDatatyp().equals("2")) {// 整形
				if (stylepropEntity.getLength().compareTo(new Integer(10)) == 1) {
					stylepropEntity.setLength(10);
				}
				String nvlu = ObjectUtils.isNotEmpty(pageContext.getRequest().getParameter("nvlu")) ? pageContext
						.getRequest().getParameter("nvlu") : "";
				sb.append("required:true,maxlength:" + stylepropEntity.getLength() + ",digits:true").append(fw);
				clazz = sb.toString();
				buffer.append("<input name='nvlu' id='vlu' class='" + clazz + "' value='" + nvlu + "' maxlength='"
						+ stylepropEntity.getLength() + "' />");
			} else if (stylepropEntity.getDatatyp().equals("3")) {// 浮点
				if (stylepropEntity.getLength().compareTo(new Integer(15)) == 1) {
					stylepropEntity.setLength(15);
				}
				String nvlu = ObjectUtils.isNotEmpty(pageContext.getRequest().getParameter("nvlu")) ? pageContext
						.getRequest().getParameter("nvlu") : "";
				Integer num = stylepropEntity.getLength() - stylepropEntity.getPrecision() - 1;
				sb.append(
						"required:true,maxlength:" + stylepropEntity.getLength() + ",digits:true,decimalFormat:[" + num
								+ "," + stylepropEntity.getPrecision() + "]").append(fw);
				clazz = sb.toString();
				buffer.append("<input name='nvlu' id='vlu' class='" + clazz + "' value='" + nvlu + "' maxlength='"
						+ stylepropEntity.getLength() + "' />");
			}
		} else {
			buffer.append("<select id='vlu' name='stylepropvluId' class='required:true'>");
			buffer.append("<option value=''>" + ApplicationContextUtil.getMessage("common.select.option.default")
					+ "</option>");
			for (StylepropvluEntity entity : list) {
				if (ObjectUtils.isNotEmpty(pageContext.getRequest().getParameter("stylepropvluId"))
						&& entity.getId().equals(
								Integer.parseInt(pageContext.getRequest().getParameter("stylepropvluId")))) {
					buffer.append("<option value='" + entity.getId() + "' selected='selected'>" + entity.getPropvalue()
							+ "</option>");
				} else {
					buffer.append("<option value='" + entity.getId() + "'>" + entity.getPropvalue() + "</option>");
				}
			}
			buffer.append("</select>");
		}
		return buffer.toString();
	}
	public String toString(StringBuilder sb) {
		if (sb.length() > 0) {
			return sb.toString().substring(1);
		} else {
			return "";
		}
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public void release() {
		super.release();
	}

	public Integer getStylepropId() {
		return stylepropId;
	}

	public void setStylepropId(Integer stylepropId) {
		this.stylepropId = stylepropId;
	}
}

2、test-targs.tld
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description><![CDATA["Based on The Spring Tags."]]></description>
  <display-name>"Spring Tags"</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>e</short-name>
  <uri>/test-tags</uri>  
  <tag>
    <description>
		diagramstyle desc
    </description>
    <name>diagramcustom</name>
    <tag-class>com.hgs.framework.web.taglib.DiagramstyleHtmlTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
		<description>stylepropId desc</description>
		<name>stylepropId</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
  </tag>
</taglib>
3、web.xml配置:

	<!-- 引入tld,或放入jar文件的/META-INF目录下 -->
	<jsp-config>
		<taglib>
			<taglib-uri>/crs-targs</taglib-uri>
			<taglib-location>/WEB-INF/crs-tags.tld</taglib-location>
		</taglib>
	</jsp-config>

4、页面引用:

<crs:diagramcustom stylepropId="${diagramstyle.stylepropId}"></crs:diagramcustom>

$("#vlu").rules("add",  eval("({" + $("#vlu").attr('class') + "})") );


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值