在做开发中,我们会把一些比较经常使用到的代码封装起来,这样可以加快开发的速度和减少错误,并且在修改bug可以一次修改多次修复。那么在前端页面上,如果我们要经常用到公用的显示功能,并涉及到服务端逻辑操作的时候,我们就需要使用到自定义标签,自定义标签使我们可以很方便的去调用一段共同的代码。下面就来看看实现自定义标签的步骤。
一.我们需要创建一个标签类,继承自javax.servlet.jsp.tagext.TagSupport;
package com.babybus.sdteam.tag; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import com.babybus.sdteam.dao.StudentHibernateDao; import com.babybus.sdteam.vo.Student; public class DisplayStudentAttributeTag extends TagSupport { /** * 序列号 */ private static final long serialVersionUID = 1L; private int studentid; private String classname; public int getStudentid() { return studentid; } public void setStudentid(int studentid) { this.studentid = studentid; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public DisplayStudentAttributeTag() { System.out.println("DisplayStudentAttributeTag 构造函数"); } public int doStartTag() throws JspException { System.out.println("doStartTag函数"); return SKIP_BODY; //不实现标签的体,即空体标签。 } public int doEndTag() throws JspException { System.out.println("doEndTag函数"); // 查询数据库 StudentHibernateDao studentDao=new StudentHibernateDao(); Student querystudent = new Student(); querystudent.setId(this.getStudentid()); querystudent.setClassname(this.getClassname()); ArrayList<Student> arrayList = new ArrayList<Student>(); try { arrayList = (ArrayList<Student>)studentDao.queryStudent(querystudent); } catch (SQLException e1) { e1.printStackTrace(); } // 通过父类TagSupport的属性值pageContext 取得相关的内置对象 JspWriter out=pageContext.getOut(); // 遍历循环打印。 try { out.println("<table>"); out.println("<tr>"); out.println("<td>姓名</td>"); out.println("<td>年龄</td>"); out.println("<td>班级</td>"); out.println("</tr>"); for(int i=0;i<arrayList.size();i++){ Student student=(Student)arrayList.get(i); out.println("<tr>"); out.println("<td>" + student.getStudentname() + "</td>"); out.println("<td>" + student.getAge() + "</td>"); out.println("<td>" + student.getClassname() + "</td>"); out.println("</tr>"); } out.println("<table>"); } catch (IOException e) { e.printStackTrace(); } return EVAL_PAGE; } }
第二.我们需要在WebRoot/WEB-INF下面新建一个tld文件。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <!-- 自定义库标签的根 --> <tlibversion>1.2</tlibversion> <!-- 版本号 --> <jspversion>1.1</jspversion> <!-- JSP版本号 --> <shortname>CustomTags</shortname> <!-- 标签名称--> <uri>customtags</uri> <!-- 外界导入标签时,认识的名字,很重要。--> <tag> <name>displayStudent</name> <!-- 标签名称 --> <tagclass>com.babybus.sdteam.tag.DisplayStudentTag</tagclass> <!-- 对应的类 --> </tag> <tag> <name>displayStudentAttribute</name> <!-- 标签名称 --> <tagclass>com.babybus.sdteam.tag.DisplayStudentAttributeTag</tagclass> <!-- 对应的类 --> <attribute> <name>studentid</name> <required>true</required> </attribute> <attribute> <name>classname</name> <required>false</required> </attribute> </tag> </taglib>
第三.在页面上引入标签,并使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="customtags" prefix="stu"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body style="overflow:auto;margin:0"> <stu:displayStudent> </stu:displayStudent> <stu:displayStudentAttribute studentid= "3" classname = "初三"> </stu:displayStudentAttribute> </body> </html>
以上就是自定义标签的实现步骤
结语
- 受益,掌握了Struts2自定义标签
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4755806.html