There are more and more codes in JSP, and there are naturally more redundant content. Often, you need to change N pages for a small change, which is not only a lot of work, but also prone to errors. Take you to solve this problem completely today!
tips:
- Image click to enlarge
This article introduces the general usage of jsp:tag, and the other one is called: jsp:tld. The use of the two at the same time does not conflict. We are used to calling them page controls, and usually both are used in a project.
- jsp:tag is mainly used to display the page after logical processing. The final effect is that you can give T some parameters, and T will process the result and display it on the page. For example: < c:if >, < c:for >, < c:set > are all implemented in this way, if you don’t believe me, you can click ctrl to see it.
- jsp:tld will be mapped to a specific class method, the final effect is that you can write a label on the page to display the data of the database on the page. For example: < sec:authorize >,< shiro:hasRole >, if you don't believe me, try again. . . . Ha ha ha ha
- Display is just one of the usages. You can think of how many usages of T can be based on your business scenarios and brain holes, because this label belongs to you.
- tld does not introduce the use of T in this article, you can add it later: [JSP] The application of tld in the project
1. Create a Tag file
My habit is to first create a tag folder under WEB-INF, and then divide it into a folder according to different functions or different divisions of modules. For example: WEB-INF/tags/layout
Create my Tag again. For example: ***.tag
<%-- To change this template use File | Settings | File Templates. --%> <%@ tag language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!--Object--> <%@ attribute name="nowSitet" type="com.wttech.tciss.jsxy.core.vo.statistics.ShutdownSite" required="false" description="当前时间节点temp"%> <%@ attribute name="shutdownSitest" type="java.util.List" required="false" description="年度列表数据temp"%> <!--Attribute name--> <%@ attribute name="selectName" type="java.lang.String" required="false" description="选择框Name"%> <%@ attribute name="selectId" type="java.lang.String" required="false" description="选择框Id"%> <%@ attribute name="selectDes" type="java.lang.String" required="false" description="select box text description"%> <!--Business logic: Practice: Copy one object and collection to another to another pair of properties Purpose: To prevent the problem that the parameter name of the control setting is inconsistent with the parameter name used by the tag --> <c:if test="${null!=nowSitet&&null!=shutdownSitest}"> <c:set var="nowSite" scope="request" target="nowSite" value="${nowSitet}"/> <c:set var="shutdownSites" scope="request" target="shutdownSites" value="${shutdownSitest}"/> </c:if> <th class="r" width="116">${not empty selectDes?selectDes:"年度信息"}:</th> <td width="220"> <select id="${not empty selectId?selectId:'shutdownSiteId'}" name="${not empty selectName?selectName:'shutdownSiteName'}" class="form-control w210px"> <c:forEach var="site" items="${shutdownSites}"> <c:choose> <c:when test="${v.id==nowSite.id}"> <option value="${site.id}" selected>${site.year}</option> </c:when> <c:otherwise> <option value="${site.id}">${site.year}</option> </c:otherwise> </c:choose> </c:forEach> </select> </td>
2. Use Tag files
My requirements here are as follows: many pages need to add the annual option to query data, and these data come from my background database (tld is more suitable for this requirement, I just want to give a chestnut and don't spray me), the front desk display The way is to use select to display.
I first add my jsp:tag to common.jsp
<!--Display control tag--> <%@ taglib prefix="layout" tagdir="/WEB-INF/tags/layout" %>
use my jsp:tag in the page
<layout:shudownsitelist selectId="shutdownSIteID" selectName="shutdownSIteID"/>
The final effect: a drop-down option box with loaded data.
- If I need this content elsewhere, I just need to copy one more line of the above code to other pages,
- If the display effect of the page needs to be modified due to changes in demand one day, I found that after my tag is modified, all the places where this control is used have been modified.