여러 탭 의 끼 워 넣 기
<mt:switch value="test">
<mt:case value="test1">
my value is test1
</mt:case>
<mt:case value="test">
my valie is test
</mt:case>
</mt:switch>
위의 탭 에서 < mt: switch > 는 부모 탭 이 고, < mt: case > 는 하위 탭 이 며, 부모 탭 은 여러 개의 하위 탭 과 HTML, Scriptlets 를 삽입 할 수 있 습 니 다.
구체 적 인 코드 는 다음 과 같다.
package com.test.ch14
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.Hashtable;
import java.io.Writer;
import java.io.IOException;
public class IfTag extends BodyTagSupport
{
private boolean value;
public void setValue(boolean value){
this.value = value;
}
/**
*doStartTag , value true, TagBody ,
*/
public int doStartTag() throws JspTagException{
if(value)
{
System.out.println("value is true");
return EVAL_BODY_INCLUDE;
}else{
System.out.println("value is false");
return SKIP_BODY;
}
}
// doEndTag
public int doEndTag() throws JspTagException
{
try{
if(bodyContent != null){
bodyContent.writeOut(bodyContent.getEnclosingWriter());
}
}catch(java.io.IOException e){
throw new JspTagException("IO Error:" +e.getMessage());
}
return EVAL_PAGE;
}
}
Value 는 IfTag 의 속성 입 니 다.Value 가 true 일 때 IfTagBody 를 계산 합 니 다.Value 의 값 이 false 라면;IfTagBody 의 Tag 를 무시 합 니 다.
이 예 에서 IfTag 탭 에 하위 탭 이 삽입 되 어 있 습 니 다. 이 하위 탭 은 클 라 이언 트 에 정 보 를 출력 하 는 데 사 용 됩 니 다. 다음은 IfTag 에 포 함 된 하위 탭 을 보 겠 습 니 다. 예 를 들 어:
package com.test.ch14;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.Hashtable;
import java.io.Writer;
import java.io.IOException;
public class OutTag extends TagSupport
{
private Object value;
// doStartTag
public void setValue(Object value){
this.value = value;
}
public int doStartTag() throws JspTagException{
return EVAL_BODY_INCLUDE;
}
// doEndTag
public int doEndTag() throws JspTagException
{
try{
System.out.println(value);
pageContext.getOut().write(value.toString());
}catch(IOException ex){
throw new JspTagException("Fatal error: hello tag conld not write to JSP out");
}
return EVAL_PAGE;
}
}
OutTag 는 바디 가 없 는 간단 한 탭 으로 클 라 이언 트 에 정 보 를 출력 합 니 다.다음은 태그 설명 파일 을 업데이트 하고 다음 내용 을 추가 해 야 합 니 다.
<tag>
<name>if</name>
<tag-class>com.test.ch14</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.test.ch14.OutTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
다음은 새 겨 진 탭 을 사용 하 는 JSP 를 만 듭 니 다.예컨대
<%@ taglib uri="/demotag" prefix="mt"%>
<html>
<head>
<title>vcorwork tag</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body>
<HR>
<br>
<%
boolean test = true;
String outValue = "HelloWorld"
%>
<mt:if value="<%=test%>">
<mt:out value="<%= outValue%>">
mt:out....> 。
</mt:out>
</mt:if>
<HR>
<mt:if value="fale">
<mt:out value="<%=outValue%>">
。
</mt:out>
</mt:if>
</body>
</html>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.