MAIN MENU
News Php Tips Android Programming Design Pattern By PHP Spring+Strut+Hibernate C# Tips & Technique C# Design Pattern Linux Quick Tips C# using Linq น่าใช้จริงๆ Java & JavaScript Tips Database & SQL ZengCode Framework Guide Zeng Code Code Programming IPhone (Tips and Trick)
Download เอกสารที่น่าสนใจ




|
JAVA ### Create your own tag: a custom tag body (Simple) (2010-05-12)
ตัวอย่างนี้มีการเข้าถึง HttpContext และ Attribute ด้วยนะคับลองเอาไปประยุกต์กันดูนะครับ เป็นตัวอย่างที่ทำความเข้าใจได้ไม่ยาก
โครงสร้างไฟล์ประมาณนี้นะครับ (ใช้ Eclipse)

1.Simple.tld เป็น Description ของ tag library
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Mark's Simple Tags</short-name>
<tag>
<name>ZengTag</name>
<tag-class>test.HelloWorldTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>myName</name>
</attribute>
</tag>
</taglib>
|
2. ตัว Tag Library Code //compile the following code into WEB-INF\classes\test\
package test;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.http.*;
import java.util.*;
public class HelloWorldTag extends BodyTagSupport
{
private String myName;
public void setMyName(String myname)
{
this.myName = myname;
}
public int doStartTag() throws JspException
{
HttpServletRequest request = null;
request = (HttpServletRequest) pageContext.getRequest();
HttpSession session = request.getSession(false);
String mySession = null;
try
{
mySession = session.getAttribute("mySession").toString();
}catch(Exception e)
{
mySession = e.toString();
}
try
{
JspWriter out = pageContext.getOut();
//HttpServletRequest request;
//HttpSession session = request.getSession(true);
out.write(this.myName+" => "+mySession);
}
catch(Exception e)
{
throw new JspException(e.getMessage());
}
return EVAL_PAGE;
}
}
|
3.index.jsp
<%@ taglib uri='META-INF/simple.tld' prefix='Zeng' %>
<%
//HttpSession session;
session.setAttribute("mySession", "I Love Java So much");
%>
<html>
<body>
<Zeng:ZengTag myName="Chiwa" />
<Zeng:ZengTag myName="Pee" />
<Zeng:ZengTag myName="ZengCode" />
</body>
|
ไม่ยากเลยใช่ไหมครับ เอาไปประยุกต์ใช้กันดูนะครับ เข้าถึง HttpContext ได้ก็ทำอะไรได้เยอะแยะแหระ
ผมกะว่าจะลองทำ simple java framework เล่นๆ สักตัว แต่หาจังหวะเวลายากจังครับ
|
Comment
Temp (13 มิถุนายน 2553)
IP : 124.120.102.152
|
|
http://www.pantipmarket.com/items/9337020
http://www.weloveshopping.com/template/a06/showproduct.php?pid=15282238&shopid=142796
|
|
|