javaweb learning summary -- the use of jsp custom tags combined with el expressions

  • The first way (tag sign-in el expression) realizes fetching data from memory and obtaining value through key:
  • 1. Write a Java class that implements the Tag interface or inherit TagSupport, etc... Many implementation classes of tag can inherit the implementation (tag processor class). In this way, the request object can be obtained, which is convenient for obtaining data from memory. .
  • The reference method in jsp is as follows:
  • <input type="text" name="11" value="<ui:getName value='${user.roleid}'/>"></input>
  • 1. Write a tld file (this file needs to be placed in the WEB-INF directory of the web function), which needs to be imported in jsp, <%@ taglib uri="/WEB-INF/right-tags.tld" prefix="ui" %>, this file is the bridge between jsp and the server, prefix="ui" is the prefix identifier used by jsp
  • content of right-tags.tld file
  • <?xml version="1.0" encoding="UTF-8"?>

  • <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

  • <!--taglib (tag library) version number -->

  • <tlib-version>2.2.3</tlib-version>
  • <!--Abbreviation, not used -->

  • <short-name>s</short-name>
  • <!--

  •     为自定义标签库设置一个uri,uri以/开头,/后面的内容随便写,如这里的/gacl ,
    
  •     在Jsp页面中引用标签库时,需要通过uri找到标签库
    
  •     在Jsp页面中就要这样引入标签库:<%@taglib uri="/gacl" prefix="gacl"%>
    
  •  -->
    
  • <uri>/right-tags</uri>
  • <!-- description is used to add a description of taglib (tag library) -->

  • <description>Custom tag library developed by Lonely Wolf</description>
  • <!--A taglib (tag library) contains multiple custom tags, each of which is described by a tag tag-->

  • <!-- A tag tag corresponds to a custom tag -->

  • <tag>

  • <name>getName</name>//
    
  • <tag-class>com.maystar.tag.TestTag</tag-class>
    
  • <body-content>JSP</body-content>
    
  • <description><![CDATA[validate the element can be show ]]></description>
    
  • <attribute>
    
  •   <name>value</name>
    
  •   <required>true</required>
    
  •   <rtexprvalue>true</rtexprvalue>
    
  • </attribute>
    
  • </tag>

  • </taglib>

  • 2.java processor class:
  • public class TestTag extends TagSupport{
  • public static Logger logger = Logger.getLogger (TestTag.class.getName ());
  • private static final long serialVersionUID = 1L;
  • //Receive the passed in PageContext object
  • private static PageContext pageContext;
    
  • private String value;
    
  • public void setValue(String value) {
  •   this.value = value;
    
  • }
  • @Override
    
  • public int doStartTag() throws JspException {
    
  •     HttpServletRequest request =(HttpServletRequest) pageContext.getRequest();
    
  •   Map map = (Map)request.getSession().getServletContext().getAttribute("MAP_ROLE");
    
  •   String str1=Convert.trimNull(map.get(value));
    
  •     JspWriter out = pageContext.getOut();
    
  •     try {
    
  •         //这里输出的时候会抛出IOException异常
    
  •         out.write(str1);
    
  •     } catch (IOException e) {
    
  •         //捕获IOException异常后继续抛出
    
  •         throw new RuntimeException(e);
    
  •     }
    
  •     return 0;
    
  • }
    
  • @Override
    
  • public int doEndTag() throws JspException {
    
  •     System.out.println("调用doEndTag()方法");
    
  •     return 0;
    
  • }
    
  • @Override
    
  • public TagSupport getParent() {
    
  •     return null;
    
  • }
    
  • @Override
    
  • public void release() {
    
  •     System.out.println("调用release()方法");
    
  • }
    
  • @Override
    
  • public void setPageContext(PageContext pageContext) {
    
  •     System.out.println("setPageContext(PageContext pageContext)");
    
  •     this.pageContext = pageContext;
    
  • }
    
  • public void setParent(TagSupport arg0) {
    
  • }
    
  • }
  • Execution process of custom tags
  •   When the JSP engine encounters a custom tag, it first creates an instance object of the tag processor class, and then calls its methods in turn according to the communication rules defined by the JSP specification.
  • 1、public void setPageContext(PageContext pc), JSP引擎实例化标签处理器后,将调用setPageContext方法将JSP页面的pageContext对象传递给标签处理器,标签处理器以后可以通过这个pageContext对象与JSP页面进行通信。
    
  • 2、public void setParent(Tag t),setPageContext方法执行完后,WEB容器接着调用的setParent方法将当前标签的父标签传递给当前标签处理器,如果当前标签没有父标签,则传递给setParent方法的参数值为null。
    
  • 3、public int doStartTag(),调用了setPageContext方法和setParent方法之后,WEB容器执行到自定义标签的开始标记时,就会调用标签处理器的doStartTag方法。
    
  • 4、public int doEndTag(),WEB容器执行完自定义标签的标签体后,就会接着去执行自定义标签的结束标记,此时,WEB容器会去调用标签处理器的doEndTag方法。
    
  • 5、public void release(),通常WEB容器执行完自定义标签后,标签处理器会驻留在内存中,为其它请求服务器,直至停止web应用时,web容器才会调用release方法。
    
  • The second way (el expression nested tags) instance code is to get the length of the value:
  • 1. Write a common tag processor class. In this way, the request object cannot be obtained. It is suitable for situations where the front desk needs to convert the encoding method or time type, or needs to fetch data from the cache database. The reference method in jsp is as follows:
    1. How to import in jsp page
  • <input type="text" name="11" value="${ui: getStringLength (user.username)}"></input>
  • <%@ taglib prefix="test" uri="/test-1.0"%>
  • 2. The content of a tlb file is written as follows, and the description is as follows. This tlb code can be merged into the tld file of the first method and share one.
  • <?xml version="1.0" encoding="UTF-8" ?>

  • <!--

  • Copyright 2004 The Apache Software Foundation
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
  • -->
  • <taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  • version="2.0">
  • <description>A tag library exercising function handlers.</description>Note: This attribute is optional, attribute, in order to describe the tag library
  • <tlib-version>1.0</tlib-version> Note: This is a required value, otherwise, the call will fail.
  • <short-name>test</short-name> Note that this attribute is an optional attribute, because the prefix is ​​defined on the jsp page, here is one, abbreviated name, I don't know what it means.
  • <uri>/test-1.0</uri> Note: The path defined here means that jsp finds the only path for this tag
  • <function>

  • <description>测试: 与 el 结合使用 如  ${test:getStringLength("XX")}</description>
    
  • <name>getStringLength</name> 注意,这里,定义的是,标签对应的函数名
    
  • <function-class>taglib.StringUtils</function-class>注意:这里,给出了,该类,所在的路径,全路径
    
  • <function-signature>java.lang.Integer getStringLength(java.lang.String)</function-signature> 
    
  • </function>

  • </taglib>

  • 3. java class
  • public class StringUtils {
  • private static Logger logger = Logger.getLogger(StringUtils.class);
  • public static Integer getStringLength(String str){ Note: The methods here must be static and should be public
  • logger.info("Input value: " + str);
  • if(str == null){
  • return 0;
  • }
  • return str.length();
  • }
  • }
    1. Declare the tag in web.xml, you can, declare, or not, declare, because, by default, it will find all tld, tag library description files under /WEB-INF, and then automatically load
  • <jsp-config>

  • <taglib>

  • <taglib-uri>/test-1.0</taglib-uri>
  • <taglib-location>test-1.0.tld</taglib-location>
  • </taglib>

  • </jsp-config>

  • **

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325469297&siteId=291194637