在項目中可能有不少相同的jsp頁面表示功能,這時可使用自定義的tag進行定義,漸少重複的工做量便於往後維護!css
下面就基於struts2進行自定義標籤的定義與實現:html
首先:自定義類MyTag繼承struts2的Componentjava
package cookielogin; import java.io.IOException; import java.io.Writer; import org.apache.struts2.components.Component; import com.opensymphony.xwork2.util.ValueStack; public class MyTag extends Component { private ValueStack stack; private String text; private String name; private String defaultValue; @Override public boolean start(Writer writer) {
<span style="white-space:pre"> </span>//這裏進行頁面的輸出 try { writer.write("<h1>" + this.text + "</h1>"); writer.write("<input name='text' value="); writer.write(this.getName()); writer.write(" name='" + this.getName() + "'>"); } catch (IOException e) { e.printStackTrace(); } return super.start(writer); } public MyTag(ValueStack stack) { super(stack); this.stack = stack; } public ValueStack getStack() { return stack; } public void setStack(ValueStack stack) { this.stack = stack; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDefaultValue() { return defaultValue; } public void setDefaultValue(String defaultValue) { this.defaultValue = defaultValue; } }
2:指定標籤類MyTagSupport繼承ComponentTagSupportweb
package cookielogin; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.components.Component; import org.apache.struts2.views.jsp.ComponentTagSupport; import com.opensymphony.xwork2.util.ValueStack; public class MyTagSupport extends ComponentTagSupport { private static final long serialVersionUID = 3586338980857274359L; String name; String text; @Override public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { return new MyTag(stack); } @Override // 設置bean值 protected void populateParams() { System.out.println("開始設置參數!populateParams"); MyTag tag = (MyTag) getComponent(); tag.setName(name); tag.setText(text); tag.setDefaultValue("我就是默認值!咋滴!!"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>2.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>ctags</short-name> <uri>http://www.cml.com/tags-custom</uri> <description> <![CDATA[ <p>This is custom tag.</p> ]]> </description> <tag> <description><![CDATA[我自定義的tag]]></description> <name>myTag</name> <tag-class>cookielogin.MyTagSupport</tag-class> <body-content>JSP</body-content> <attribute> <description><![CDATA[這個是我自定義的標籤]]></description> <name>text</name> <required>true</required> </attribute> <attribute> <description><![CDATA[這個是我自定義的標籤]]></description> <name>name</name> <required>true</required> </attribute> <attribute> <description><![CDATA[這個是我自定義的標籤]]></description> <name>defaultValue</name> <required>false</required> </attribute> <dynamic-attributes>false</dynamic-attributes> </tag> </taglib>
<%@taglib prefix="d" uri="http://www.cml.com/tags-custom"%>
使用:apache
<d:myTag text="hhahh" name="sdfsd"></d:myTag>
markdown
訪問頁面後出現:cookie
基於struts2的自定義標籤實現完畢!雖然功能很簡單,可是基本功能已經實現,能夠根據項目的實際須要進行編輯!jsp