Write a Java class , This class contains static methods to implement functions
package com.golden.struts;public class MyFunctions { /** * @ explain The method must be public static */ public static String sayHello(String name) { return "Hello " + name; }}
Provide TLD file , This file must be placed in WEB-INT Lower or WEB-INT Under any subdirectory
TLD The way a document is written , May refer to Struts Of TLD file
<?xml version="1.0" encoding="UTF-8" ?><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>my functions library</description> <display-name>my functions</display-name> <tlib-version>1.0</tlib-version> <short-name>my</short-name> <uri>http://www.bjsxt.com/functions</uri> <function> <name>sayHello</name> <function-class>com.bjsxt.struts.MyFunctions</function-class> <function-signature> java.lang.String sayHello(java.lang.String) </function-signature> </function></taglib>
Introduce on the page
<%@ taglib prefix="my" uri="http://www.golden.com/functions"%>
Use
${my:sayHello(name) }
remarks :
describe , name , edition , Abbreviation ,uri, It's all custom , But not with Struts There is a conflict between the relevant documents
Methodical <name> Is provided to JSP Used when calling <function-class> Corresponding to our custom class
<function-signature> We must pay attention to the writing of
Return value , Method name , parameter list , If the basic type is returned, it can be written directly int, For others, write the specific type name, such as java.lang.String or java.lang.String[] or java.lang.Object.
I recommend you to read more about “ ” The article