[JavaWeb基础] 012.Struts2 自定义标签使用
在做开发中,我们会把一些比较经常使用到的代码封装起来,这样可以加快开发的速度和减少错误,并且在修改bug可以一次修改多次修复。那么在前端页面上,如果我们要经常用到公用的显示功能,并涉及到服务端逻辑操作的时候,我们就需要使用到自定义标签,自定义标签使我们可以很方便的去调用一段共同的代码。下面就来看看实现自定义标签的步骤。
一.我们需要创建一个标签类,继承自javax.servlet.jsp.tagext.TagSupport;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <? 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 > |
第三.在页面上引入标签,并使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <%@ 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
比大多数人的努力程度之低,根本轮不到拼天赋...
宝宝巴士 SD.Team
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步