JSP code more and more, redundant content naturally also more, often a very small change you need to change n pages, the workload is not said, but also prone to error. Today I'll take you to solve this problem completely!
Tips
- Image click to enlarge
This article describes the general use of Jsp:tag, and the other is called: Jsp:tld, both use does not conflict. We're accustomed to what T calls a page control, which is usually used in a project.
- Jsp:tag Main Page To do the logical processing after the display, the final effect is that you can give T some parameters, T will be processed after the resulting results displayed in the page. For a chestnut:<c:if>,<c:for>,<c:set> All of this is done, do not believe you ctrl point to go to see Bai.
- JSP:TLD will map to a specific class method, and the final effect is that you can write a label on the page to display the database data to the page. Take a chestnut:<sec:authorize>,<shiro:hasrole, don't believe you try again .... haha haha
- The display is just one of the uses, and you can think about how many ways t can be used depending on your business scenario and brain hole, because this tag belongs to you.
- TLD does not describe how T is used in this article and can be added later: [JSP]TLD application in the project
1. Create a tag file
My habit is to first create a tag folder under Web-inf, and then divide a folder by the different functions, or different partitions of the module. For example: Web-inf/tags/layout
Create my tag again. For example: ***.tag
<%--Created by IntelliJ. user:ta0477Date: -/7/ - Time: -: - toChange 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="Current Time Node temp"%><%@ attribute Name="shutdownsitest"type="java.util.List"Required="false"Description="annual list Data temp"%><!--Property name -<%@ attribute Name="Selectname"type="java.lang.String"Required="false"Description="Select box Name"%><%@ attribute Name="Selectid"type="java.lang.String"Required="false"Description="Select Box ID"%><%@ attribute Name="Selectdes"type="java.lang.String"Required="false"Description="selection Box Text description"%><!--business logic: The practice of copying an object and a collection to another pair of properties. Purpose: Prevent the parameter name of the control setting from being not uniform with the parameter names used by tag -<c:ifTest= "${null!=nowsitet&&null!=shutdownsitest}"> <C:setvar= "Nowsite"Scope= "Request"Target= "Nowsite"value= "${nowsitet}"/> <C:setvar= "Shutdownsites"Scope= "Request"Target= "Shutdownsites"value= "${shutdownsitest}"/></c:if><thclass= "R"width= "The ">${not Empty Selectdes?selectdes: "Information of the Year"}:</th><TDwidth= "The "> <SelectID= "${not empty selectid?selectid: ' Shutdownsiteid '}"name= "${not empty selectname?selectname: ' Shutdownsitename '}"class= "Form-control w210px"> <C:foreachvar= "Site"Items= "${shutdownsites}"> <C:choose> <C:whenTest= "${v.id==nowsite.id}"> <optionvalue= "${site.id}"selected>${site.year}</option> </C:when> <c:otherwise> <optionvalue= "${site.id}">${site.year}</option> </c:otherwise> </C:choose> </C:foreach> </Select></TD>
2. Using the tag file
What I need here is this: many pages need an annual option to query the data, which comes from my back-end database (the requirement is more appropriate for the TLD, I just want to raise a chestnut not to spray me), the way the front desk shows is to use Select to show.
I first joined my jsp:tag in the common.jsp.
<!-- Display control tag --> <% @ taglib prefix = " layout " Tagdir = /web-inf/tags/layout " %>
Use my Jsp:tag in the page
<selectid= "Shutdownsiteid" selectname= "Shutdownsiteid" />
The final effect: a drop-down box that loads the data well.
- If I need this content elsewhere, I just need to copy the above code one line to another page.
- If one day because of the change in demand, the page display effect needs to be modified, I found my tag modified after all the use of this control has been modified.
Hope this article is helpful to you, help me to praise it.!!!!
You are welcome to exchange study with me.
Original Forbidden REPRINT!!!!!!
[Jsp]tag application in the project