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') + "})") );