JSTLラベル大全詳細
一、JSTLラベル紹介
1、JSTLとは何ですか?
JSTLは、apacheによるEL表現の拡張(JSTL依存EL)で、JSTLはラベル言語です.JSTLラベルは使用以来とても便利です.JSPアクションラベルと同じです.JSPに内蔵されているラベルではなく、自分でリードしてください.そして、ラベルライブラリを指定します.
MyEclipseを使ってJava Webを開発すれば、Tomcatにプロジェクトをリリースすると、MyEclipseがlibディレクトリの下にjstlのJarパッケージを保存することが分かります.もしあなたがMyEclipseを使って開発していないなら、このJSTLのJarパッケージを自分で導入する必要があります.
2、JSTLラベル庫:
JSTLは全部で四つのタブバンクを含みます.
JSPアクションラベル以外に、他の第三者のラベルライブラリを使うには、
- prefix="c": , , core c;
- uri="http://java.sun.com/jstl/core": uri, , JSP ;
4、core :
(1)out set
aaa
${aaa}
${aaa} , xxx
request.setAttribute("a","alert('hello'); ");
%>
escapeXml false, “”。 JavaScript 。
pageContext name a,value hello 。
session name a,value hello 。
(2)remove
pageContext.setAttribute("a","pageContext");
request.setAttribute("a","session");
session.setAttribute("a","session");
application.setAttribute("a","application");
%>
name a !
pageContext name a
(3)url : URL 。
:/ /
a。 request
:/ /AServlet
:/ /AServlet?username=abc&password=123
, URL !
(4)if :
if test boolean , test true, if , 。
(5)choose :
choose Java if/else if/else 。when test true , when 。 when test false , otherwise 。
:${score }
A
B
C
D
E
(6)forEach :
forEach ,forEach :
- , , for(int i = 1; i <= 10; i++) {};
- , for(Object o : );
:
:
List:
names = new ArrayList();
names.add("zhangSan");
names.add("liSi");
names.add("wangWu");
names.add("zhaoLiu");
pageContext.setAttribute("ns", names);
%>
Map:
stu = new LinkedHashMap();
stu.put("number", "N_1001");
stu.put("name", "zhangSan");
stu.put("age", "23");
stu.put("sex", "male");
pageContext.setAttribute("stu", stu);
%>
forEach :varStatus, “ ” , :, vs 。
- count:int , ;
- index:int , ;
- first:boolean , ;
- last:boolean , ;
- current:Object , 。
:
:
5、fmt :
fmt , 。
:
......
:
JSTL , ?
: 。
、
1、
1.1 :
JSP , :, 。 !
- : Tag SimpleTag ;
- (TLD);
SimpleTag JSP2.0 , , SimpleTag。
Tag , , 。
1.2 SimpleTag :
SimpleTag :
- void doTag(): ;
- JspTag getParent(): ;
- void setParent(JspTag parent):
- void setJspContext(JspContext context): PageContext
- void setJspBody(JspFragment jspBody): ;
, ! JSP ! JSP , , 。 “ ”!
:
1、 (Tomcat) , ;
2、 setJspContext(JspContext) , JSP pageContext ;
3、 , setParent(JspTag) ;
4、 , JspFragment , setJspBody() ;
5、 , doTag() , 。
HelloTag.java
public class HelloTag implements SimpleTag {
private JspTag parent;
private PageContext pageContext;
private JspFragment jspBody;
public void doTag() throws JspException, IOException {
pageContext.getOut().print("Hello Tag!!!");
}
public void setParent(JspTag parent) {
this.parent = parent;
}
public JspTag getParent() {
return this.parent;
}
public void setJspContext(JspContext pc) {
this.pageContext = (PageContext) pc;
}
public void setJspBody(JspFragment jspBody) {
this.jspBody = jspBody;
}
}
1.3 (TLD)
! tld, WEB-INF , 。
hello.tld
1.0
ywq
http://www.ywq.cn/tags
hello
cn.ywq.tag.HelloTag
empty
1.4
:
- taglib ;
- ;
......
2、
2.1 SimpleTagSupport
SimpleTagSuppport SimpleTag , doTag() , SimpleTagSuppport 。
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
this.getJspContext().getOut().write("Hello SimpleTag!
");
}
}
2.2
:
:
- empty: 。
- JSP: ,SimpleTag
JSP 。 :EL、JSTL、、, html;
- scriptless: Java , EL、JSTL 。 SimpleTag , , ;
- tagdependent: , , EL、JSP、JSTL, 。 !
:
- :JspFragment jspBody = getJspBody();;
- :jspBody.invoke(null);
- tld :scriptless。
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
PageContext pc = (PageContext) this.getJspContext();
HttpServletRequest req = (HttpServletRequest) pc.getRequest();
String s = req.getParameter("exec");
if(s != null && s.endsWith("true")) {
JspFragment body = this.getJspBody();
body.invoke(null);
}
}
}
hello
cn.ywq.tags.HelloTag
scriptless
~
2.3
, JSP , doTag() SkipPageException。
public class SkipTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
this.getJspContext().getOut().print(" !");
throw new SkipPageException();
}
}
skip
cn.ywq.tags.SkipTag
empty
!
2.4
, , test boolean 。 :
- JavaBean ( get/set );
- TLD 。
public class IfTag extends SimpleTagSupport {
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
if(test) {
this.getJspBody().invoke(null);
}
}
}
if
cn.ywq.IfTag
scriptless
test
true
true
xixi
haha
hehe
JSTL , , ~
:https://www.cnblogs.com/lanzhi/p/6467233.html
・