1.<c:out value="${ buttons}" escapeXml="false" />
<c:out value="${ buttons}" escapeXml="false" />
è¿æ¯JSTLæ ç¾çç¨æ³
JSTLç®ä»
JSTLæ¯ä¸ä¸ªä¸æå®åçå¼æ¾æºä»£ç çJSPæ ç¾åºï¼æ¯ç±apacheçjakartaå°ç»æ¥ç»´æ¤çãJSTLåªè½è¿è¡å¨æ¯æJSP1.2åServlet2.3è§èç容å¨ä¸ï¼å¦tomcat 4.xãä½æ¯å¨å³å°æ¨åºçJSP 2.0ä¸æ¯ä½ä¸ºæ åæ¯æçã
JSTLç®åçææ°çæ¬ä¸º1.ï¼æç»åå¸ç为1.0ãJSTLå å«ä¸¤ä¸ªé¨åï¼æ ç¾åºåELï¼Expression Language表达å¼è¯è¨ï¼è¯è¨ãæ ç¾åºç®åæ¯æåç§æ ç¾ï¼
æ ç¾ URIå ç¼ ç¤ºä¾
Core /jstl/core c <c:tagname ...> XML processing /jstl/xml x <x:tagname ...> IN capable formatting /jstl/fmt fmt <fmt:tagname ...> Database access (SQL) /jstl/sql sql <sql:tagname ...>
Coreæ¯æJSPä¸çä¸äºåºæ¬çæä½ï¼
XML processingæ¯æXMLææ¡£çå¤çï¼
IN capable formattingæ¯æ对JSP页é¢çå½é åï¼
Database access (SQL)æ¯æJSP对æ°æ®åºçæä½ã
ç±äºæ¬äººæ°´å¹³æéï¼æ¬æä» ä»ç»Coreæ ç¾ï¼å¦æå ´è¶£ï¼å¯ä¸èµ·æ¢è®¨å ¶å®ä¸ç§æ ç¾ç使ç¨ä¸æ©å ã
ELè¯è¨ä»ç»
ELè¯è¨æ¯JSTLè¾åºï¼è¾å ¥ï¼ä¸ä¸ªJAVA表达å¼ç表示形å¼ã
å¨JSTLä¸ï¼ELè¯è¨åªè½å¨å±æ§å¼ä¸ä½¿ç¨ãELè¯è¨åªè½éè¿å»ºç«è¡¨è¾¾å¼æ¥è¿è¡è°ç¨ãå¨å±æ§å¼ä¸ä½¿ç¨è¡¨è¾¾å¼æä¸ç§æ¹å¼ã
1ã valueå±æ§å å«ä¸ä¸ªè¡¨è¾¾å¼
<some:tag value=""/>
å¨è¿ç§æ åµä¸ï¼è¡¨è¾¾å¼å¼è¢«è®¡ç®åºæ¥å¹¶æ ¹æ®ç±»å转æ¢è§åèµå¼ç»valueå±æ§ãæ¯å¦ï¼<c:out value="" />ä¸çå°±æ¯ä¸ä¸ªELï¼å®ç¸å½äºJSPè¯å¥<%=request.getAttribute(âusernameâ)%>æ<% =session.getAttribute(âusernameâ)%>
2ã valueå±æ§å å«ä¸ä¸ªæå¤ä¸ªå±æ§ï¼è¿äºå±æ§è¢«ææ¬åå²æå´ç»
<some:tag value="sometext"/>
å¨è¿ç§æ åµä¸ï¼è¡¨è¾¾å¼ä»å·¦å°å³è¿è¡è®¡ç®ï¼å¹¶å°ç»æ转æ¢ä¸ºå符串åï¼æ ¹æ®ç±»å转æ¢è§åï¼ï¼å¹¶å°ç»æèµå¼ç»valueå±æ§
3ã valueå±æ§ä» ä» å å«ææ¬
<some:tag value="sometext"/>
å¨è¿ç§æ åµä¸ï¼å符串åå±æ§valueå°æ ¹æ®ç±»å转æ¢è§å转æ¢ä¸ºæ ç¾æå¸æçç±»åã
ELè¯è¨çæä½ç¬¦
åå¾æ个对象æéåä¸çå±æ§å¼
为äºè·å¾éåä¸çå±æ§ï¼ELæ¯æ以ä¸ä¸¤ç§æä½
1ï¼ä½¿ç¨.æä½ç¬¦æ¥è·å¾æååçå±æ§ãä¾å¦è¡¨è¾¾å¼è¡¨æ对象userçusernameå±æ§
2ï¼ ä½¿ç¨[]æä½ç¬¦æ¥è·å¾æååæææ°åæåçå±æ§ã
表达å¼å表达å¼å«ä¹ç¸å
è¡¨è¾¾å¼ è¡¨ærowéåç第ä¸ä¸ªæ¡ç®ã
å¨è¿éuseræ¯ä¸ä¸ªç±»ç对象ï¼å®çå±æ§usernameå¿ é¡»ç¬¦åæ åJavaBeançè§èï¼å³å¿ 须为usernameå±æ§å®ä¹ç¸åºçgetterãsetteræ¹æ³ã
Emptyæä½ç¬¦(空å¼æ£æ¥)
使ç¨emptyæä½ç¬¦æ¥å³å®å¯¹è±¡ãéåæå符串åéæ¯å¦ä¸ºç©ºænullãä¾å¦ï¼
true
å¦ærequestçåæ°å表ä¸çusernameå¼ä¸ºnullï¼å表达å¼çå¼ä¸ºtrueã ELä¹å¯ä»¥ç´æ¥ä½¿ç¨æ¯è¾æä½ç¬¦ä¸nullè¿è¡æ¯è¾ãå¦trueã
æ¯è¾æä½ç¬¦ æä½ç¬¦ æè¿°
==æeq ç¸çæ£æ¥
!=æne ä¸çæ£æ¥
<ælt å°äºæ£æ¥
>ægt 大äºæ£æ¥
<=æle å°äºçäºæ£æ¥
>=æge 大äºçäºæ£æ¥
æ°åè¿ç®ç¬¦ä¸é»è¾è¿ç®ç¬¦åä¸JAVAè¯è¨ç¸åï¼ä¸åå表ã
Coreæ ç¾åº
1ã éç¨æ ç¾
<c:out>
<c:out>æ ç¾ç¨äºå¨JSPä¸æ¾ç¤ºæ°æ®ï¼å®æå¦ä¸å±æ§ å± æ§ æ è¿°æ¯å¦å¿ é¡» 缺çå¼
value è¾åºçä¿¡æ¯ï¼å¯ä»¥æ¯EL表达å¼æ常é æ¯ æ
default value为空æ¶æ¾ç¤ºä¿¡æ¯ å¦ æ
escapeXml 为trueåé¿å¼ç¹æ®çxmlå符é å¦ true
ä¾å: æ¨çç¨æ·åæ¯: <c:out value=ââ default=âguestâ/>
æ¾ç¤ºç¨æ·çç¨æ·åï¼å¦ä¸ºç©ºåæ¾ç¤ºguest
<c:out value=""/>
æå®ä»sessionä¸è·åusernameçå¼æ¾ç¤ºï¼
<c:out value="" />
æ¾ç¤ºusernameçå¼ï¼é»è®¤æ¯ä»request(page)ä¸åï¼å¦ærequestä¸æ²¡æå为usernameç对象åä»sessionä¸åï¼ sessionä¸æ²¡æåä»application(servletContext)ä¸åï¼å¦æ没æåå°ä»»ä½å¼åä¸æ¾ç¤ºã
<c:set>
<c:set>æ ç¾ç¨äºä¿åæ°æ®ï¼å®æå¦ä¸å±æ§ å± æ§ æ è¿° æ¯å¦å¿ 须缺çå¼
value è¦ä¿åçä¿¡æ¯ï¼å¯ä»¥æ¯EL表达å¼æ常é å¦
target éè¦ä¿®æ¹å±æ§çåéåï¼ä¸è¬ä¸ºjavabeançå®ä¾ å¦ æ
property éè¦ä¿®æ¹çjavabeanå±æ§ å¦ æ
var éè¦ä¿åä¿¡æ¯çåé å¦ æ
scope ä¿åä¿¡æ¯çåéçèå´ å¦ page
å¦ææå®äºtargetå±æ§,小组源码mui社区app源码 é£ä¹propertyå±æ§ä¹å¿ é¡»æå®ã
ä¾åï¼ <c:set value="" var="test2" scope=âsessionâ />
å°test.testinfoçå¼ä¿åå°sessionçtest2ä¸ï¼å ¶ä¸testæ¯ä¸ä¸ªjavabeançå®ä¾ï¼testinfoæ¯test对象çå±æ§ã
<c:set target="" property="city" value=""/>
å°å¯¹è±¡cust.addressçcityå±æ§å¼ä¿åå°åécityä¸
<c:remove>
<c:remove>æ ç¾ç¨äºå é¤æ°æ®ï¼å®æå¦ä¸å±æ§ å± æ§ æ è¿° æ¯å¦å¿ 须缺çå¼
var è¦å é¤çåé æ¯ æ
scope 被å é¤åéçèå´ å¦ææèå´ï¼å æ¬pageãrequestãsessionãapplicationç
ä¾å: <c:remove var="test2" scope="session"/>
ä»sessionä¸å é¤test2åéã
2ã æµæ§å¶æ ç¾
<c:if>
<c:if>æ ç¾æå¦ä¸å±æ§ å± æ§ æ è¿° æ¯å¦å¿ é¡» 缺çå¼
test éè¦è¯ä»·çæ¡ä»¶ï¼ç¸å½äºif (...){ }è¯å¥ä¸çæ¡ä»¶ æ¯ æ
var è¦æ±ä¿åæ¡ä»¶ç»æçåéå å¦ æ
scope ä¿åæ¡ä»¶ç»æçåéèå´ å¦ page
<c:choose>
è¿ä¸ªæ ç¾ä¸æ¥åä»»ä½å±æ§
<c:when>
<c:when>æ ç¾æ以ä¸å±æ§ å± æ§ æ è¿° æ¯å¦å¿ é¡» 缺çå¼
test éè¦è¯ä»·çæ¡ä»¶ æ¯ æ
<c:otherwise>
è¿ä¸ªæ ç¾åæ ·ä¸æ¥åä»»ä½å±æ§
ä¾åï¼ <c:if test="">
user.wealthy is true.
</c:if>
å¦æuser.wealthyå¼trueï¼åæ¾ç¤ºuser.wealthy is true.
<c:choose>
<c:when test="">
user.generous is true.
</c:when>
<c:when test="">
user.stingy is true.
</c:when>
<c:otherwise>
user.generous and user.stingy are false.
</c:otherwise>
</c:choose>
åªæå½æ¡ä»¶user.generousè¿åå¼æ¯trueæ¶ï¼ææ¾ç¤ºuser.generous is true.
åªæå½æ¡ä»¶user.stingyè¿åå¼æ¯trueæ¶ï¼ææ¾ç¤ºuser.stingy is true.
å ¶å®ææçæ åµï¼å³user.generousåuser.stingyçå¼é½ä¸ä¸ºtrueï¼å ¨é¨æ¾ç¤ºuser.generous and user.stingy are false.
ç±äºJSTL没æå½¢å¦if (){ â¦} else { â¦}çæ¡ä»¶è¯å¥ï¼æ以è¿ç§å½¢å¼çè¯å¥åªè½ç¨<c:choose>ã<c:when>å<c:otherwise>æ ç¾å ±åæ¥å®æäºã
3ã 循ç¯æ§å¶æ ç¾
<c:forEach>
<c:forEach>æ ç¾ç¨äºéç¨æ°æ®ï¼å®æ以ä¸å±æ§ å± æ§ æ è¿°æ¯å¦å¿ é¡» 缺çå¼
items è¿è¡å¾ªç¯çé¡¹ç® å¦ æ
begin å¼å§æ¡ä»¶ å¦ 0
end ç»ææ¡ä»¶ å¦ éåä¸çæåä¸ä¸ªé¡¹ç®
step æ¥é¿ å¦ 1
var 代表å½å项ç®çåéå å¦ æ
varStatus æ¾ç¤ºå¾ªç¯ç¶æçåé å¦ æ
ä¾åï¼ <c:forEach items="" var="vector">
<c:out value=""/>
</c:forEach>
ç¸å½äºjavaè¯å¥ for (int i=0;i<vectors.size();i++) {
out.println(vectors.get(i));
}
å¨è¿évectorsæ¯ä¸ä¸ªjava.util.Vector对象ï¼éé¢åæ¾çæ¯Stringæ°æ®ï¼vectoræ¯å½å循ç¯æ¡ä»¶ä¸String对象ãå®é ä¸è¿éçvectorså¯ä»¥æ¯ä»»ä½å®ç°äºjava.util. Collectionæ¥å£ç对象ã
<c:forEach begin="0" end="" var="i" step="1">
count=<c:out value=""/><br>
</c:forEach>
è¾åº:
count=0
count=
<c:forTokens>
<c:forTokens>æ ç¾æ以ä¸å±æ§ å± æ§ æ è¿° æ¯å¦å¿ é¡» 缺çå¼
items è¿è¡å¾ªç¯çé¡¹ç® æ¯ æ
delims åå²ç¬¦ æ¯ æ
begin å¼å§æ¡ä»¶ å¦ 0
end ç»ææ¡ä»¶ å¦ éåä¸çæåä¸ä¸ªé¡¹ç®
step æ¥é¿ å¦ 1
var 代表å½å项ç®çåéå å¦ æ
varStatus æ¾ç¤ºå¾ªç¯ç¶æçåé å¦ æ
ä¾å
<c:forTokens items="a:b:c:d" delims=":" var="token">
<c:out value=""/>
</c:forTokens>
è¿ä¸ªæ ç¾ç使ç¨ç¸å½äºjava.util.StringTokenizerç±»ãå¨è¿éå°å符串a:b:c:d以ï¼åå¼å¾ªç¯å次ï¼tokenæ¯å¾ªç¯å°å½ååå²å°çå符串ã
4.å¯¼å ¥æ件åURL
JSTLæ ¸å¿æ ç¾åºæ¯æ使ç¨<c:import>æ¥å å«æ件ï¼ä½¿ç¨<c:url>æ¥æå°åæ ¼å¼åURL,使ç¨<c:redirect>æ¥éå®åURLã
<c:import>
<c:import>æ ç¾å å«å¦å¤ä¸ä¸ªé¡µé¢ä»£ç å°å½å页ï¼å®æ以ä¸å±æ§ å±æ§ æ è¿° æ¯å¦å¿ é¡» 缺çå¼
url éè¦å¯¼å ¥é¡µé¢çurl æ¯ æ
context /åè·æ¬å°webåºç¨ç¨åºçåå å¦ å½ååºç¨ç¨åº
charEncoding ç¨äºå¯¼å ¥æ°æ®çå符é å¦ ISO--1
var æ¥åå¯¼å ¥ææ¬çåéå å¦ page
scope æ¥åå¯¼å ¥ææ¬çåéçåéèå´ å¦ 1
varReader ç¨äºæ¥åå¯¼å ¥ææ¬çjava.io.Readeråéå å¦ æ
varStatus æ¾ç¤ºå¾ªç¯ç¶æçåé å¦ æ
<c:url>
<c:url>æ ç¾è¾åºä¸ä¸ªurlå°åï¼å®æ以ä¸å±æ§ å± æ§ æ è¿° æ¯å¦å¿ 须缺çå¼
url urlå°å æ¯ æ
context /åè·æ¬å°webåºç¨ç¨åºçåå å¦ å½ååºç¨ç¨åº
charEncoding ç¨äºå¯¼å ¥æ°æ®çå符é å¦ ISO--1
var æ¥åå¤çè¿çurlåéåï¼è¯¥åéåå¨url å¦ è¾åºå°é¡µ
scope åå¨urlçåéåçåéèå´ å¦ page
ä¾åï¼
<c:import url="/edit.js" var="newsfeed"/>
å°url /edit.jså å«å°å½å页çå½åä½ç½®ï¼å¹¶å°urlä¿åå°newsfeedåéä¸
<a href="<c:url url="/index.jsp"/>"/>
å¨å½å页çå½åä½ç½®è¾åº<a href="/index.jsp"/>ï¼æ¯å½å页çæå¨çä½ç½®ã
<c:redirect>
<c:redirect>æ ç¾å°è¯·æ±éæ°å®åå°å¦å¤ä¸ä¸ªé¡µé¢ï¼å®æ以ä¸å±æ§ å±æ§ æ è¿° æ¯å¦å¿ é¡» 缺çå¼
url urlå°å æ¯ æ
context /åè·æ¬å°webåºç¨ç¨åºçåå å¦ å½ååºç¨ç¨åº
ä¾åï¼
<c:redirect url="/login.jsp"/>
å°è¯·æ±éæ°å®åå°/login.jsp页ï¼ç¸å½äºresponse.setRedirect("/login.jsp");
<c:param>
<c:param>æ ç¾ç¨æ¥ä¼ éåæ°ç»ä¸ä¸ªéå®åæå å«é¡µé¢ï¼å®æ以ä¸å±æ§å± æ§ æ è¿° æ¯å¦å¿ é¡» 缺çå¼
name å¨requeståæ°ä¸è®¾ç½®çåéå æ¯ æ
value å¨requeståæ°ä¸è®¾ç½®çåéå¼ å¦ æ
ä¾åï¼
<c:redirect url="login.jsp">
<c:param name="id" value=""/>
</c:redirect>
å°åæ°ä»¥id为ååä¼ éå°login.jsp页é¢ï¼ç¸å½äºlogin.jsp?id=
JSTLçä¼ç¹
1ãå¨åºç¨ç¨åºæå¡å¨ä¹é´æä¾äºä¸è´çæ¥å£ï¼æ大ç¨åºå°æé«äºWEBåºç¨å¨ååºç¨æå¡å¨ä¹é´ç移æ¤ã
2ã ç®åäºJSPåWEBåºç¨ç¨åºçå¼åã
3ã以ä¸ç§ç»ä¸çæ¹å¼åå°äºJSPä¸çscriptlet代ç æ°éï¼å¯ä»¥è¾¾å°æ²¡æä»»ä½scriptlet代ç çç¨åºãå¨æä»¬å ¬å¸ç项ç®ä¸æ¯ä¸å 许æä»»ä½çscriptlet代ç åºç°å¨JSPä¸ã
4ãå 许JSPè®¾è®¡å·¥å ·ä¸WEBåºç¨ç¨åºå¼åçè¿ä¸æ¥éæãç¸ä¿¡ä¸ä¹ å°±ä¼ææ¯æJSTLçIDEå¼åå·¥å ·åºç°ã
æ»ç»
ä¸é¢ä»ç»çä» ä» æ¯JSTLçä¸é¨åï¼å¦æè¦ä½¿ç¨JSTLï¼åå¿ é¡»å°jstl.jarå standard.jaræ件æ¾å°classpathä¸ï¼å¦æä½ è¿éè¦ä½¿ç¨XML processingåDatabase access (SQL)æ ç¾ï¼è¿è¦å°ç¸å ³JARæ件æ¾å°classpathä¸ï¼è¿äºJARæä»¶å ¨é¨åå¨äºä¸è½½åæ¥çzipæ件ä¸ãè¿ä¸ªzipæ件å¯ä»¥ä»/products/jsp/jstl/
sunå ¬å¸çJSTLç«ç¹
2ã /bayern/appendixA.pdf
JSTLçåèææ¡£ï¼æ¬æå¾å¤å 容é½æ¯ä»è¿ä¸ªPDFæ件éç¿»è¯çã
4ã ï¼ï¼J2EEç¼ç¨æåï¼1.3çï¼ï¼ï¼
ä»ç»äºJSTLçéå½¢ï¼wroxç书é½æ¯ç²¾åã