欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义 标签

程序员文章站 2022-04-25 21:49:59
...

 

 

usertag.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">

<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>baoyou</short-name>
<uri>/usertag</uri>
<tag>
    <name>userinfo</name>
    <tag-class>com.baoyou.test.autotag.UserTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <name>user</name>
         <required>false</required>
         <rtexprvalue>true</rtexprvalue>
    </attribute>
 </tag>
</taglib>

 

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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
	xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	id="WebApp_9" version="2.4">
	 
	 
   <jsp-config> 
     <taglib> 
        <taglib-uri>/usertag</taglib-uri> 
        <taglib-location>/WEB-INF/usertag.tld</taglib-location> 
     </taglib> 
   </jsp-config>
	 
</web-app>

 

public class User {

	private int userId;
	private String username;
	private String password;
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	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;
	}
	 
}

 

package com.baoyou.test.autotag;
 

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class UserTag extends TagSupport{
 
	private static final long serialVersionUID = 1L;
	
	private User user;
	 
	@Override
	public int doStartTag() throws JspException { 
		 JspWriter out = this.pageContext.getOut();
		 try{
		 if (user == null) { 
				out.println("No UserInfo Found...");
		 }
		 out.println("<table width='500px' border='1' align='center'>");
         out.println("<tr>");
         out.println("<td width='20%'>userid:</td>");
         out.println("<td>" + user.getUserId() + "</td>");
         out.println("</tr>");
         out.println("<tr>");
         out.println("<td>username:</td>");
         out.println("<td>" + user.getUsername() + "</td>");
         out.println("</tr>");
         out.println("<tr>");
         out.println("<td>password:</td>");
         out.println("<td>" + user.getPassword() + "</td>");
         out.println("</tr>");
         out.println("</table>");
		 }catch(Exception e){
			 throw  new JspException(e.getMessage());
		 }
		return SKIP_BODY;
	}
	
	@Override
	public int doEndTag() throws JspException { 
		 return EVAL_PAGE;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}

 

<%@ page language="java" import="java.util.*,com.baoyou.test.autotag.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/usertag" prefix="baoyou"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>My JSP 'index.jsp' starting page</title> 
  </head> 
  <body>
  <% User user = new User(); 
 	 user.setUserId(1);
 	 user.setUsername("baoyou");
 	 user.setPassword("baoyou"); 
     pageContext.setAttribute("userinfo", user);      %>
   <baoyou:userinfo user="${pageScope.userinfo }" />
  </body>
</html>

 

 

 

 

 
自定义 标签
            
    
    博客分类: jsp jsp自定义标签 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


自定义 标签
            
    
    博客分类: jsp jsp自定义标签 自定义 标签
            
    
    博客分类: jsp jsp自定义标签 自定义 标签
            
    
    博客分类: jsp jsp自定义标签 
 
 
 谢谢您的赞助,我会做的更好!

 

 

  • 自定义 标签
            
    
    博客分类: jsp jsp自定义标签 
  • 大小: 16.3 KB
相关标签: jsp 自定义标签