jsp页面自定义标签

阅读更多

自定义标签

新建maven项目taglib

maven pom.xml


     
			javax.servlet
			javax.servlet-api
			3.0.1
			provided
		
		
			javax.servlet.jsp
			jsp-api
			2.2
		
		
			javax.servlet
			jstl
			1.2
		
  

 web.xml



	

    
      baseServlet
      com.lyncc.common.base.servlet.BaseServlet
    
    
      baseServlet
      /
    

       BaseServlet.java

public class BaseServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List ls = null;
	
	private List users = null; 
	
	@Override
	public void init() throws ServletException {
		super.init();
	}
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		ls = new ArrayList();
		ls.add("Lyncc");
		ls.add("Smile");
		ls.add("Ted");
		ls.add("Moon");
		req.setAttribute("ls", ls);
		
		users = new ArrayList();
		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" %>




test



  ${o:formatSpecial(str)}
${u.username}=====${u.password}=====${u.visible}

 original.tld



  2.0
  
    formatSpecial
    com.lyncc.common.taglib.util.OpenTaglibFunctions
    java.lang.String formatSpecial(java.lang.String)
  
  
  
    showVisible
    com.lyncc.common.taglib.util.OpenTaglibFunctions
    java.util.List showVisible(java.util.List)
  
  

 OpenTaglibFunctions.java

public class OpenTaglibFunctions {
	
	public static final String formatSpecial(String str){
		return str+"war3 player";
	}
	
	public static final List showVisible(List 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/ 可以看见效果,记录一下,有时候挺方便的~

你可能感兴趣的:(tld,入门,自定义标签)