jsp页面自定义标签
程序员文章站
2022-04-25 21:50:47
...
自定义标签
新建maven项目taglib
maven pom.xml
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name></display-name> <servlet> <servlet-name>baseServlet</servlet-name> <servlet-class>com.lyncc.common.base.servlet.BaseServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>baseServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
BaseServlet.java
public class BaseServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; private List<String> ls = null; private List<User> users = null; @Override public void init() throws ServletException { super.init(); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { ls = new ArrayList<String>(); ls.add("Lyncc"); ls.add("Smile"); ls.add("Ted"); ls.add("Moon"); req.setAttribute("ls", ls); users = new ArrayList<User>(); User u1 = new User("Lyncc", "123456", true); User u2 = new User("Smile", "123456", true); User u3 = new User("Ted", "123456", false); User u4 = new User("Moon", "123456", true); User u5 = new User("Th000", "123456", false); User u6 = new User("Infi", "123456", false); users.add(u4); users.add(u3); users.add(u2); users.add(u1); users.add(u5); users.add(u6); req.setAttribute("ls2", users); req.getRequestDispatcher("WEB-INF/view/first.jsp").forward(req,resp); } @Override public void destroy() { super.destroy(); } }
User.java
public class User { private String username; private String password; private boolean isVisible; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean isVisible() { return isVisible; } public void setVisible(boolean isVisible) { this.isVisible = isVisible; } public User(String username, String password, boolean isVisible) { this.username = username; this.password = password; this.isVisible = isVisible; } }
first.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="o" uri="/WEB-INF/tld/original.tld" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title> </head> <body> <c:forEach items="${ls}" var="str"> ${o:formatSpecial(str)}<br> </c:forEach> <c:forEach items="${o:showVisible(ls2)}" var="u"> ${u.username}=====${u.password}=====${u.visible}<br> </c:forEach> </body> </html>
original.tld
<?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/web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>2.0</tlib-version> <function> <name>formatSpecial</name> <function-class>com.lyncc.common.taglib.util.OpenTaglibFunctions</function-class> <function-signature>java.lang.String formatSpecial(java.lang.String)</function-signature> </function> <function> <name>showVisible</name> <function-class>com.lyncc.common.taglib.util.OpenTaglibFunctions</function-class> <function-signature>java.util.List showVisible(java.util.List)</function-signature> </function> </taglib>
OpenTaglibFunctions.java
public class OpenTaglibFunctions { public static final String formatSpecial(String str){ return str+"war3 player"; } public static final List<User> showVisible(List<User> u){ if(null==u||u.size()==0)return u; int size = u.size()-1; for(int i = size;i>=0;i--){ if(!u.get(i).isVisible())u.remove(i); } return u; } }
访问http://localhost:8080/taglib/ 可以看见效果,记录一下,有时候挺方便的~
上一篇: maya怎么制作逼真的睫毛? maya睫毛建模方法
下一篇: 12款饱含情感的汉仪手写字体推荐