• JSP自定义标签(下)


    hello,家人们,今天我们再来更一节自定义标签的小课堂(●'◡'●)

    目录

    foeach标签

    select标签


    首先,我们还是得先上处理图:

    •   SKIP_BODY:跳过主体( 不会打印“awa”)
    •   EVAL_BODY_INCLUDE:计算标签主体内容并[输出](会打印“awa”)
    •   EVAL_PAGE:计算页面的后续部分
    •   SKIP_PAGE:跳过页面的后续部分(不建议用,直接跳过后面全部类容包括body)
    •   EVAL_BODY_AGAIN:再计算主体一次

    常见自定义标签主要步骤:

    • 编写助手类
    • 编写标签库描述文件(tld)
    • 在页面上引入标签使用

    • foeach标签

    1.先new一个cat类

    1. package com.zking.y10.pojo;
    2. import java.io.Serializable;
    3. public class Cat implements Serializable{
    4. private Integer age;
    5. private String name;
    6. private String info;
    7. public Cat() {
    8. // TODO Auto-generated constructor stub
    9. }
    10. public Cat(Integer age, String name, String info) {
    11. super();
    12. this.age = age;
    13. this.name = name;
    14. this.info = info;
    15. }
    16. @Override
    17. public String toString() {
    18. return "Cat [age=" + age + ", name=" + name + ", info=" + info + "]";
    19. }
    20. public Integer getAge() {
    21. return age;
    22. }
    23. public void setAge(Integer age) {
    24. this.age = age;
    25. }
    26. public String getName() {
    27. return name;
    28. }
    29. public void setName(String name) {
    30. this.name = name;
    31. }
    32. public String getInfo() {
    33. return info;
    34. }
    35. public void setInfo(String info) {
    36. this.info = info;
    37. }
    38. }

    2.定义一个普通类,然后继承BodyTagSupport

    1. package com.zking.y10.dao;
    2. import java.util.Iterator;
    3. import java.util.List;
    4. import java.util.Objects;
    5. import javax.servlet.jsp.JspException;
    6. import javax.servlet.jsp.tagext.BodyTagSupport;
    7. public class FoeachTag extends BodyTagSupport{
    8. private List list;
    9. private String var;
    10. public void setList(List list) {
    11. this.list = list;
    12. }
    13. public void setVar(String var) {
    14. this.var = var;
    15. }
    16. @Override
    17. public int doStartTag() throws JspException {
    18. if(Objects.isNull(this.list) || this.list.size()==0) {
    19. return SKIP_BODY;
    20. }
    21. Iterator it=this.list.iterator();
    22. Object next = it.next();
    23. this.pageContext.setAttribute(var, next);
    24. this.pageContext.setAttribute("it",it);
    25. return EVAL_BODY_INCLUDE;
    26. }
    27. @Override
    28. public int doAfterBody() throws JspException {
    29. Iterator a = (Iterator)this.pageContext.getAttribute("it");
    30. if(a.hasNext()) {
    31. this.pageContext.setAttribute(var,a.next());
    32. return EVAL_BODY_AGAIN;
    33. }
    34. return SKIP_BODY;
    35. }
    36. }

    3.写一个类,把数据填进去

    1. package com.zking.y10.dao;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import com.zking.y10.pojo.Cat;
    5. public class Test {
    6. public static List<Cat> getCat(){
    7. List<Cat> list=new ArrayList<Cat>();
    8. Cat c=new Cat();
    9. c.setAge(12);
    10. c.setName("金渐层");
    11. c.setInfo("超可爱噢");
    12. Cat c1=new Cat();
    13. c1.setAge(1);
    14. c1.setName("银渐层");
    15. c1.setInfo("肥肥胖胖,肉嘟嘟的");
    16. Cat c2=new Cat();
    17. c2.setAge(10);
    18. c2.setName("布偶");
    19. c2.setInfo("长毛猫,毛软软的,抱着很舒服");
    20. list.add(c);
    21. list.add(c1);
    22. list.add(c2);
    23. return list;
    24. }
    25. }

    4.最后再来个index.jsp测试一下

    1. <%@page import="com.zking.y10.dao.Test,java.util.List" %>
    2. <%@page import="com.zking.y10.pojo.Cat"%>
    3. <%@page import="java.util.List"%>
    4. <%@ page language="java" contentType="text/html; charset=UTF-8"
    5. pageEncoding="UTF-8"%>
    6. <%@taglib prefix="p" uri="/zkings" %>
    7. html>
    8. <html>
    9. <head>
    10. <meta charset="UTF-8">
    11. <title>Insert title heretitle>
    12. head>
    13. <body>
    14. <%
    15. List<Cat> list=Test.getCat();
    16. request.setAttribute("list",list);
    17. System.out.print(list);
    18. %>
    19. <p:foeach list="${list}" var="l">
    20. ${l.age}----${l.name}---${l.info}<br>
    21. p:foeach>
    22. body>
    23. html>

    运行结果:


    • select标签

    1.照常写一个普通类,然后继承BodyTagSupport

    1. package com.zking.y10.dao;
    2. import java.util.List;
    3. import javax.servlet.jsp.JspException;
    4. import javax.servlet.jsp.JspWriter;
    5. import javax.servlet.jsp.tagext.BodyTagSupport;
    6. import org.apache.commons.beanutils.BeanUtils;
    7. import com.mysql.jdbc.StringUtils;
    8. public class SelectTag extends BodyTagSupport{
    9. private Integer id;
    10. private String name;
    11. private String clazz;
    12. private String Style;
    13. private List list;
    14. private String value;
    15. private String text;
    16. private String SelectValue;
    17. public SelectTag() {
    18. // TODO Auto-generated constructor stub
    19. }
    20. public SelectTag(Integer id, String name, String clazz, String style, List list, String value, String text,
    21. String selectValue) {
    22. super();
    23. this.id = id;
    24. this.name = name;
    25. this.clazz = clazz;
    26. Style = style;
    27. this.list = list;
    28. this.value = value;
    29. this.text = text;
    30. SelectValue = selectValue;
    31. }
    32. public void setId(Integer id) {
    33. this.id = id;
    34. }
    35. public String getName() {
    36. return name;
    37. }
    38. public void setName(String name) {
    39. this.name = name;
    40. }
    41. public String getClazz() {
    42. return clazz;
    43. }
    44. public void setClazz(String clazz) {
    45. this.clazz = clazz;
    46. }
    47. public String getStyle() {
    48. return Style;
    49. }
    50. public void setStyle(String style) {
    51. Style = style;
    52. }
    53. public List getList() {
    54. return list;
    55. }
    56. public void setList(List list) {
    57. this.list = list;
    58. }
    59. public String getValue() {
    60. return value;
    61. }
    62. public void setValue(String value) {
    63. this.value = value;
    64. }
    65. public String getText() {
    66. return text;
    67. }
    68. public void setText(String text) {
    69. this.text = text;
    70. }
    71. public String getSelectValue() {
    72. return SelectValue;
    73. }
    74. public void setSelectValue(String selectValue) {
    75. SelectValue = selectValue;
    76. }
    77. @Override
    78. public String toString() {
    79. return "SelectTag [id=" + id + ", name=" + name + ", clazz=" + clazz + ", Style=" + Style + ", list=" + list
    80. + ", value=" + value + ", text=" + text + ", SelectValue=" + SelectValue + "]";
    81. }
    82. @Override
    83. public int doStartTag() throws JspException {
    84. JspWriter out = this.pageContext.getOut();
    85. try {
    86. String html=getHtml();
    87. out.print(html);
    88. } catch (Exception e) {
    89. e.printStackTrace();
    90. }
    91. return SKIP_BODY;
    92. }
    93. public String getHtml() throws Exception {
    94. StringBuilder sb=new StringBuilder();
    95. sb.append("");
    96. return sb.toString();
    97. }
    98. }

    2.在后缀名为.tld的文件中增加select标签
         注:tld文件必须保存到WEB-INF目录或其子目录

    1. <tag>
    2. <name>selectname>
    3. <tag-class>com.zking.y10.dao.SelectTagtag-class>
    4. <body-content>jspbody-content>
    5. <description>select标签description>
    6. <attribute>
    7. <name>idname>
    8. <required>truerequired>
    9. <rtexprvalue>falsertexprvalue>
    10. attribute>
    11. <attribute>
    12. <name>namename>
    13. <required>truerequired>
    14. <rtexprvalue>falsertexprvalue>
    15. attribute>
    16. <attribute>
    17. <name>listname>
    18. <required>truerequired>
    19. <rtexprvalue>truertexprvalue>
    20. attribute>
    21. <attribute>
    22. <name>clazzname>
    23. <required>falserequired>
    24. <rtexprvalue>falsertexprvalue>
    25. attribute>
    26. <attribute>
    27. <name>Stylename>
    28. <required>falserequired>
    29. <rtexprvalue>falsertexprvalue>
    30. attribute>
    31. <attribute>
    32. <name>valuename>
    33. <required>truerequired>
    34. <rtexprvalue>falsertexprvalue>
    35. attribute>
    36. <attribute>
    37. <name>textname>
    38. <required>truerequired>
    39. <rtexprvalue>falsertexprvalue>
    40. attribute>
    41. <attribute>
    42. <name>selectValuename>
    43. <required>falserequired>
    44. <rtexprvalue>falsertexprvalue>
    45. attribute>
    46. tag>

    3.测试

    1. <%@page import="com.zking.y10.dao.Test,java.util.List" %>
    2. <%@page import="com.zking.y10.pojo.Cat"%>
    3. <%@page import="java.util.List"%>
    4. <%@ page language="java" contentType="text/html; charset=UTF-8"
    5. pageEncoding="UTF-8"%>
    6. <%@taglib prefix="p" uri="/zkings" %>
    7. html>
    8. <html>
    9. <head>
    10. <meta charset="UTF-8">
    11. <title>Insert title heretitle>
    12. head>
    13. <body>
    14. <%
    15. List<Cat> list=Test.getCat();
    16. request.setAttribute("list",list);
    17. %>
    18. <p:foeach list="${list}" var="l">
    19. ${l.age}----${l.name}---${l.info}<br>
    20. p:foeach>
    21. <select id="test" name="test" class="" style="width:100px;">
    22. <option value=0>已付款option>
    23. <option value= selected>已发货option>
    24. <option value=2>已签收option>
    25. select>
    26. <p:select name="test" value="age" list="${list}" text="name" id="test" selectValue="3">p:select>
    27. body>
    28. html>

    4.运行结果:

    今天的简单举例到这里就结束啦~

    今天也要记得微笑呀. 

  • 相关阅读:
    最常见的Java面试题【杭州多测师_王sir】【杭州多测师】
    英文科技论文写作与发表-常见英语写作困扰(第3章)
    德国人工智能公司【Kodex AI】完成160万欧元融资
    消息中间件-RabbitMQ
    为什么WinXP SP2有时候会忘记CD自动播放的设置?
    gradle迁移到gradle.kts(复制可用)
    java毕业设计—— 基于java+javaEE+jsp的项目管理系统设计与实现(毕业论文+程序源码)——项目管理系统
    天玑9000+和骁龙8gen1+哪个性能更强 两者配置对比
    【数据结构】树和二叉树的概念及结构(一)
    【Windbg】记一次线程卡主的问题
  • 原文地址:https://blog.csdn.net/weixin_65975275/article/details/125787212