Servlet & JSP – 29 – JSTL Functions

Merhaba Arkadaslar,
Bu bolumde JSTL Functions konusunu inceleyecegiz. JSTL Functions OCEWCD sinavinda yer almamaktadir, fakat bu konuya da deginmek istedim.

  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:startsWith()
  • fn:endsWith()
  • fn:indexOf()
  • fn:length()
  • fn:toLowerCase()
  • fn:toUpperCase()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:replace()
  • fn:trim()
  • fn:join()
  • fn:split()
  • fn:escapeXml()

JSTL funtions kullanabilmek icin oncelikle ilgili taglibi JSP dosyamiza eklememiz gereklidir. taglib uzerinde ctrl tusu ile tikladigimizda fn.tld dosyasina gidebiliriz. Burada tanimlanmis olan function’lari inceleyebiliriz. Daha once EL Function yazmistik.

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

fn:contains()

	<c:set var="myattribute" value="injavawetrust" />

	<c:if test="${fn:contains(myattribute, 'java')}">
		<p>Founded! java</p>
	</c:if>
 <function>
    <description>
      Tests if an input string contains the specified substring.
    </description>
    <name>contains</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>boolean contains(java.lang.String, java.lang.String)</function-signature>
    <example>
      <c:if test="${fn:contains(name, searchString)}">
    </example>
 </function>

fn:containsIgnoreCase()

fn:containsIgnoreCase , fn:contains ‘a benzer. Aradaki fark buyuk-kucuk harf konusunu dikkate almaz.

	<c:if test="${fn:containsIgnoreCase(myattribute, 'JAVA')}">
		<p>Founded! JAVA</p>
	</c:if>
....
<function-signature>boolean containsIgnoreCase(java.lang.String, java.lang.String)</function-signature>
....

fn:startsWith()

	<c:set var="myattribute" value="injavawetrust" />

	<c:if test="${fn:startsWith(myattribute, 'in')}">
		<p>String starts with in</p>
	</c:if>
....
<function-signature>boolean startsWith(java.lang.String, java.lang.String)</function-signature>
...

fn:endsWith()

	<c:if test="${fn:endsWith(myattribute, 'trust')}">
		<p>String ends with trust</p>
	</c:if>
....
<function-signature>boolean endsWith(java.lang.String, java.lang.String)</function-signature>
...

fn:indexOf()

	<c:set var="myattribute" value="This is test String." />
	
	<p>Index: ${fn:indexOf(myattribute, "test")}</p>
...
<function-signature>int indexOf(java.lang.String, java.lang.String)</function-signature> 
...

fn:length()

<p>Length of String  : ${fn:length(myattribute)}</p>
...
<function-signature>int length(java.lang.Object)</function-signature>
...

fn:toLowerCase()

	<c:set var="myattribute" value="TO LOWER CASE TEST" />
	
	<p>${fn:toLowerCase(myattribute)} </p>
<function-signature>java.lang.String toLowerCase(java.lang.String)</function-signature>

fn:toUpperCase()

	<c:set var="myattribute2" value="to upper case test" />
	
	<p>${fn:toUpperCase(myattribute2)} </p>
<function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature>

fn:substring()

	<c:set var="myattribute" value="injavawetrust" />

	<p>${fn:substring(myattribute, 2, 6)}</p>
...
<function-signature>java.lang.String substring(java.lang.String, int, int)</function-signature>
...

fn:substringAfter()

fn:substringAfter , ilgili String degerini varsa bulur sonrasindaki kismi substring olarak doner.

<p>${fn:substringAfter(myattribute,'java')}</p>
<function-signature>java.lang.String substringAfter(java.lang.String, java.lang.String)</function-signature>

fn:substringBefore()

fn:substringBefore , ilgili String degerini varsa bulur oncesindeki kismi substring olarak doner.

<p>${fn:substringBefore(myattribute,'java')} </p>
...
<function-signature>java.lang.String substringBefore(java.lang.String, java.lang.String)</function-signature>
...

fn:replace()

	<c:set var="myattribute" value="This is first String." />
	<p>${fn:replace(myattribute, 'first', 'second')}"</p>

<function-signature>java.lang.String replace(java.lang.String, java.lang.String, java.lang.String)</function-signature>

fn:trim()

fn:trim , String degerdeki bastaki ve sondaki bosluklari temizler. Bunu kontrol etmek icin oncesindeki ve sonrasindaki uzunluk degerini karsilastirabiliriz.

<c:set var="string1" value="    there are whitespaces     " />
<p>String (1) Length : ${fn:length(string1)}</p>

<c:set var="string2" value="${fn:trim(string1)}" />
<p>String (2) Length : ${fn:length(string2)}</p>

<function-signature>java.lang.String trim(java.lang.String)</function-signature>

fn:join()

fn:join ile array’e bir seperator eklenebilir. Arraydeki elemanlari yazdirirken araya ; ekleyebiliriz.

	<%
		String[] myArray = { "test1", "test2", "test3" };
		request.setAttribute("myArray", myArray);
		
	%>

	
	${fn:join(myArray, ';')}
...
<function-signature>java.lang.String join(java.lang.String[], java.lang.String)</function-signature>
...

fn:split()

	<c:forEach var="x" items="${fn:split(splitIt, ',')}">
		<c:out value="${x}"></c:out>
	</c:forEach>

<function-signature>java.lang.String[] split(java.lang.String, java.lang.String)</function-signature>

fn:escapeXml()

escapeXml kullanildiginda <b> </b> HTML olarak degerlendirilmez.

	<c:set var="string1" value="This <b>is second String.</b>" />

	<p>With escapeXml() Function:</p>

	<p>string (1) : ${fn:escapeXml(string1)}</p>

	<p>Without escapeXml() Function:</p>

	<p>string (1) : ${string1}</p>
<function-signature>java.lang.String escapeXml(java.lang.String)</function-signature>

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *