• 干程序员这么多年,没有自己的标签库怎么哪的出手?


    干程序员这么多年,没有自己的标签库怎么哪的出手? 基于servlet 和jsp2.0 实现自己的jsp自定义标签

    自己实现一个自定义标签的小demo,效果如下

    实现步骤

    1. 做一个Bo 也就是javabean

    package com.bpehome.bo;

    public class UserBo {
     
     private String userName;
     
     private String email;
     
     private String age;

     public String getUserName() {
      return userName;
     }

     public void setUserName(String userName) {
      this.userName = userName;
     }

     public String getEmail() {
      return email;
     }

     public void setEmail(String email) {
      this.email = email;
     }

     public String getAge() {
      return age;
     }

     public void setAge(String age) {
      this.age = age;
     }
    }

    2. 标签实现类

    package com.bpehome.tag;

    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;

    import com.bpehome.bo.UserBo;

    public class UserTag extends TagSupport {
     
     private UserBo user;

         @Override

         public int doStartTag() throws JspException {

             try {

                 JspWriter out = this.pageContext.getOut();

                 if(user == null) {

                     out.println("No UserInfo Found...");

                     return SKIP_BODY;

                 }

                 out.println("<table width='500px' border='1' align='center' style='border:1px solid red'>");

                 out.println("<tr>");

                 out.println("<td width='20%'>Username:</td>");

                 out.println("<td>" + user.getUserName() + "</td>");

                 out.println("</tr>");

                 out.println("<tr>");

                 out.println("<td>Age:</td>");

                 out.println("<td>" + user.getAge() + "</td>");

                 out.println("</tr>");

                 out.println("<tr>");

                 out.println("<td>Email:</td>");

                 out.println("<td>" + user.getEmail() + "</td>");

                 out.println("</tr>");

                 out.println("</table>");

             } catch(Exception e) {

                 throw new JspException(e.getMessage());

             }

             return SKIP_BODY;

         }

         @Override

         public int doEndTag() throws JspException {

             return EVAL_PAGE;

         }

         @Override

         public void release() {

             super.release();

             this.user = null;

         }

         //getter and setters

         public UserBo getUser() {

             return user;

         }

         public void setUser(UserBo user) {

             this.user = user;

         }
    }

    3.web-inf 文件夹下 做一个mytaglib.tld 文件

    <?xml version="1.0" encoding="UTF-8"?>

    <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">

    <tlib-version>1.0</tlib-version>

    <jsp-version>2.0</jsp-version>

    <short-name>sbean</short-name>

    <uri>/mytaglib</uri>

    <tag>

        <name>showUserBo</name>

        <tag-class>com.bpehome.tag.UserTag</tag-class>

        <body-content>empty</body-content>

        <attribute>

            <name>user</name>

             <required>false</required>

             <rtexprvalue>true</rtexprvalue>

        </attribute>

     </tag>

    </taglib>

    4.web.xml文件中定义如下

    <jsp-config>
        <taglib>
            <taglib-uri>/mytaglib</taglib-uri>
            <taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
        </taglib>
      </jsp-config>

    5.如何使用这个标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ page isELIgnored="false" %>
    <%@ taglib uri="/mytaglib" prefix="sbean"%>

    <%@ page import="com.bpehome.bo.UserBo" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>自定义标签练习</title>

    <%
     UserBo user = new UserBo();

        user.setUserName("令狐冲之12");

        user.setAge("25");

        user.setEmail("1055592525@qq.com");
     
        pageContext.setAttribute("userBo", user); 

     %>

    </head>

    <body>
      <!-- 给标签设置user属性绑定要展现的UserInfo对象  -->

        <sbean:showUserBo user="${pageScope.userBo}" />

    </body>
    <html>

    ----------------------------------------------------------

    别惊呆哦 大家有什么不明白的吗?

  • 相关阅读:
    CSS 处理table表格长度溢出问题 解决table导致的横向滚动条问题
    NodeRED中解析高德地图天气api的json数据显示天气仪表盘
    【20220325】连岳摘抄
    【20220324】好过、坏过,还是要过
    使用格拉姆角场(GAF)以将时间序列数据转换为图像
    LSTM 已死,事实真是这样吗?
    TensorFlow和Pytorch中的音频增强
    论文回顾:Batch Augmentation,在批次中进行数据扩充可以减少训练时间并提高泛化能力
    特征工程:基于梯度提升的模型的特征编码效果测试
    Linux中常用Shell命令
  • 原文地址:https://www.cnblogs.com/qylbg/p/3191183.html
Copyright © 2020-2023  润新知