#副標=Java整合XML應用系列(14): #大標=應用JSP整合XML/XSL之解疑篇(下) 作者=鄧崇林 =================================== orderConfirm.xml =================================== =================================== =================================== =================================== <%=xslString%> =================================== =================================== myNewOrder.xml =================================== =================================== private Object convert(String s, Class c) {} =================================== =================================== package clt.taglibs.var; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.BodyTagSupport; public class SetVariableTag extends BodyTagSupport { private String name = null, type = null, value = null, content = null; public void setName(String name) { this.name = name; } public void setType(String type) { this.type = "java.lang." + type; } public void setValue(String value) { this.value = value; } public int doEndTag() throws JspException { Class klass = null; if ((getId() == null) && (name == null)) { throw new JspException("Must specify either 'id' or 'name' attribute!"); } try { klass = Class.forName(type); } catch(ClassNotFoundException e) { e.printStackTrace(); return EVAL_PAGE; } content = bodyContent.getString().trim(); bodyContent.clearBody(); // 當使用空標記元素時,此一content不等於null,也不等於空白字串 if (content == null && value == null) { return EVAL_PAGE; } else if (value != null) { // do nothing } else if (content != null) { value = content; } if (getId() != null && (name == null)) { if (pageContext.getAttribute(getId()) != null) { // 已經訂有同名的 Scripting 變數名稱,也暗示存有JavaBean throw new JspException("Already specified 'id' attribute!"); } else { pageContext.setAttribute(getId(), convert(value, klass)); } } else if (name != null) { if (pageContext.getAttribute(name) == null) { throw new JspException("Cannot specify 'name' attribute!"); } else { // 透過下列方法來建立該變數值 pageContext.setAttribute(name, convert(value, klass)); } } else { throw new JspException("Cannot work at all!"); } return EVAL_PAGE; } // private Object convert(String s, Class c) throws JspException { try { if (s == null) { if (c.equals(Boolean.class) || c.equals(Boolean.TYPE)) s = "false"; else return null; } if (c.equals(Boolean.class) || c.equals(Boolean.TYPE) ) { if (s.equalsIgnoreCase("on") || s.equalsIgnoreCase("true")) s = "true"; else s = "false"; return new Boolean(s); } else if (c.equals(Byte.class) || c.equals(Byte.TYPE) ) { return new Byte(s); } else if (c.equals(Character.class) || c.equals(Character.TYPE)) { return s.length() > 0 ? new Character(s.charAt(0)) : null; } else if (c.equals(Short.class) || c.equals(Short.TYPE) ) { return new Short(s); } else if (c.equals(Integer.class) || c.equals(Integer.TYPE) ) { return new Integer(s); } else if (c.equals(Float.class) || c.equals(Float.TYPE) ) { return new Float(s); } else if (c.equals(Long.class) || c.equals(Long.TYPE) ) { return new Long(s); } else if (c.equals(Double.class) || c.equals(Double.TYPE) ) { return new Double(s); } else if (c.equals(String.class) ) { return s; } else { throw new JspException("unKnown type! "); } } catch (Exception ex) { throw new JspException(ex.getMessage()); } } // public void release() { type = value = content = null; super.release(); } } =================================== =================================== package clt.taglibs.var; import javax.servlet.jsp.tagext.TagData; import javax.servlet.jsp.tagext.TagExtraInfo; import javax.servlet.jsp.tagext.VariableInfo; public final class SetVariableTEI extends TagExtraInfo { public SetVariableTEI() { super(); } public VariableInfo[] getVariableInfo(TagData data) { String varName = data.getId(); if (varName == null) varName = data.getAttributeString("name"); return new VariableInfo[] { new VariableInfo(varName, // "id"或"name"屬性所指定的描述變數名稱 data.getAttributeString("type"), // 為變數設定恰當的資料型態 (data.getId() != null ? true : false), VariableInfo.AT_END) }; } } =================================== =================================== javac -d C:\tomcat32\webapps\runpc\WEB-INF\classes *.java cd ..\classes jar cvf varTags.jar clt\taglibs\var\*.class move varTags.jar ..\lib del clt\taglibs\var\*.class cd ..\lib pause =================================== =================================== 1.0 1.1 setVariable clt.taglibs.var.SetVariableTag clt.taglibs.var.SetVariableTEI JSP Set scripting variable, author:鄧崇林 id no no name no yes type yes no value no yes =================================== =================================== /clt.taglibs.var varTags.tld =================================== =================================== <%@ taglib uri="/clt.taglibs.var" prefix="clt" %> ===================================