Detailed description of custom Taglib in jsp, jsptaglib
Jsp taglib custom Tag custom jsp Tag can implement the Tag interface or inherit the TagSupport class. Because TagSupport has implemented the Tag interface, it inherits the TagSupport directly for convenience.
Import javax. servlet. servletContext; import javax. servlet. jsp. jspException; import javax. servlet. jsp. tagext. tagSupport; import org. springframework. beans. factory. config. autowireCapableBeanFactory; import org. springframework. web. context. webApplicationContext; import org. springframework. web. context. support. webApplicationContextUtils; public abstract class extends acttag extends TagSupport {private static final long serialVersionUID = protected; protected WebApplicationContext spring; @ Overridepublic int doStartTag () throws JspException {ServletContext servletContext = pageContext. getServletContext (); spring = WebApplicationContextUtils. getRequiredWebApplicationContext (servletContext); // automatically assemble the spring class according to the name. getAutowireCapableBeanFactory (). autowireBeanProperties (this, AutowireCapableBeanFactory. AUTOWIRE_BY_NAME, false); return super. doStartTag () ;}@ Overridepublic int doEndTag () throws JspException {doTag (); return super. doEndTag ();} protected abstract String doTag ();}
Here, we only need to rewrite the doStartTag () and doEndTag () methods. doStartTag () is the method that will be executed when entering the tag, doEndTag () is the method that will be executed when the tag is ended.
DoStartTag () returns the following values:
EVAL_BODY_INCLUDE: reads the Body into an existing output stream.
SKIP_BODY: Ignore the processing of the Body
DoEndTag () returns the following values:
EVAL_PAGE: Continue processing page
SKIP_PAGE: Ignore the processing of the remaining pages
In my project, beans are automatically injected by spring according to name. Considering that multiple tags will be defined, a layer is abstracted here. All the labels to be further defined can inherit the AbstractTag class and implement the doTag () method.
Column:
public class SecurityTag extends AbstractTag {private static final long serialVersionUID = -4459796109014389209L;private DeptService deptService;private String type;@Overrideprotected String doTag() {System.out.println(type);deptService.delete(4l);return null;}public void setDeptService(DeptService deptService) {this.deptService = deptService;}public void setType(String type) {this.type = type;}}
Write your own logic in the doTag () of SecurityTag. Here I just want to make a simple test.
Now, the label class has been written. We need to add a xxx. tld file to the src directory.
xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version="2.0">Security Tag Library1.0d/security st common.commons.tags.SecurityTagJSP type true String
Finally, we introduce this tag on the page.
Prefix: d <% @ taglib uri = "/security" prefix = "d" %>
Call
Tag UDF
The above is all done, and then the User-Defined Function is relatively simple, paste the code
public class Functions {private static DeptService deptService;public static String test(Integer age){deptService.delete(6l);return String.valueOf(age);}public static void setDeptService(DeptService deptService) {Functions.deptService = deptService;}}
All the methods to be called must be static. If you need to use the service interface, you must declare the method as static, add the set method, and then inject the bean defined by spring.
xmlns:util="https://www.springframework.org/schema/util" xmlns:context="https://www.springframework.org/schema/context"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttps://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util-3.1.xsdhttps://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.1.xsd">
Finally, you can add the following items under the tag of the tld file on the page.
Test
Common. commons. tags. Functions
String test (Integer)
Use
$ {D: test (234 )}