Jstl Ready Tag Examples Or ≪C:Set≫ Inwards Jsp – Coffee J2ee Tutorial

JSTL laid tag or <c:set> likewise called equally JSTL Core tag library is a skillful replacement of <jsp:setProperty> jsp activeness which lacks lot of functionality as well as entirely allow y'all to laid edible bean property. y'all tin not set Map's fundamental value or create a scoped variable yesteryear using <jsp:setProperty>. jstl <set> tag allows y'all to practise all the materials related to setting or creating variables or attributes. yesteryear using JSTL <c:set> tag y'all tin :
1) Set edible bean properties
2) Set Map values
3) Create Scoped variable on page, request, session or application scope.

But <c:set> comes alongside its ain caveat similar <c:set> tin withdraw attributes or variable if provided value is null or throw exceptions which nosotros volition here. nosotros will also lots of examples of JSTL tag <c:set> to empathize what Set tin practise as well as how to exercise JSTL <c:set> tag.

This article is inwards continuation of my before post on JSP servlet e.g.  Difference betwixt URL Rewriting as well as URL Encoding inwards JSP Servlet? and  How to define default mistake page inwards J2EE spider web application  if haven't already read y'all may honor around useful information.

JSTL Core <c:set> tag

jsp activeness which lacks lot of functionality as well as entirely allow y'all to laid edible bean holding JSTL Set tag examples or <c:set> inwards JSP – Java J2EE TutorialAs I said before JSTL Set tag or <c:set> is exercise to laid edible bean properties, map value as well as to create scoped variables. <c:set> tin endure used inwards 2 unlike ways either yesteryear using "var" or yesteryear using "target". var is used to define advert of variable needs to endure created or whose values needs to endure set. land "target" is used to announce Bean Object or Map whose values needs to endure set. here is a uncomplicated example of JSTL <c:set> tag

<c:set var="currency" value="USD" />


JSTL Example : How to create variable inwards session reach using <c:set> tag inwards JSP

Above illustration of <c:set>  will create an attribute named "currency" alongside value "USD" inwards default scope which is page scope. y'all tin likewise create attribute into around other scope say session scope, inwards that instance y'all demand to specify reach alongside <c:set> attribute scope="". hither is an illustration of creating variable on session scope using jstl laid tag:

<c:set var="currency" value="USD" scope="session" />

value of variable tin likewise endure a runtime facial expression e.g jsp facial expression or EL expression. similar the 1 which is shown inwards below illustration of laid tag:

<c:set var="currency" value="${user.currency}" scope="session" />

here Container volition re-create edible bean holding user.currency inwards "currency" variable.


JSTL Example : How to overstep value inwards <c:set> tag trunk inwards JSP

Another variance of jstl <c:set> tag is that y'all tin provide value inwards trunk instead of on attribute line. around fourth dimension when value is long, giving it on trunk makes code to a greater extent than readable. hither is illustration of supplying value on jstl laid tag body:

<c:set var="currency" scope="session" >
        USD,EUR,AUD,INR
</c:set>


JSTL Example : How to withdraw attribute using <c:set> inwards JSP

Keep inwards hear that <c:set> tin likewise withdraw a variable or attribute if value resolves to "null" during runtime. e.g. inwards below example

<c:set var="currency" value="${user.currency}" />

<c: set> tag of jstl volition withdraw "currency" attribute from whatsoever reach if EL facial expression ${user.currency} volition resolve to null.


JSTL Example : How to set edible bean holding using JSTL <c:set> tag inwards JSP

All of to a higher house illustration were for setting attribute or creating variables, but <c:set> tin likewise endure used to laid edible bean properties or map value. inwards that case instead of "var" nosotros demand to exercise "target" as well as "property" which volition define edible bean as well as holding advert to endure set. if "target" is map than "property" is advert of fundamental as well as "value" is value for that key. What is worth noting hither is that <c:set target=""> must signal to a existent object as well as non the name of object equally it was alongside <jsp:useBean> action. if "target" doesn't resolve into object than spider web container volition throw exception. hither is example of setting edible bean holding using JSTL <c:set> tag inwards JSP:

<c:set target="currencyMap" property="USA" value="USD">

this is equivalent to currencyMap.put("USA", "USD"); simply similar before y'all tin likewise provide value inwards trunk of jst <c:set> tag inwards jsp. here is another illustration of setting edible bean properties using JSTL <c:set> tag :

<c:set target="trade" property="currency" value="USD">

this illustration volition laid currency holding of edible bean named "trade" as well as equivalent to :

trade.setCurrency("USD");

You simply demand to brand certain that target should resolve into existent object.


JSTL <c:set> - Points to remember

Here are few of import points to cry upward on <c:set> tag from JSTL meat tag library. I convey seen nosotros frequently overlooked these pocket-size subtle details which causes difficult to honor bugs inwards JSP:

1) <c:set> tin withdraw attribute if value="null".
2) Value for JSTL <c:set> tag tin endure specified inwards trunk also.
3) JSTL laid tag value tin endure runtime facial expression include JSP facial expression or EL expression.
4) In target version of <c:set> if target is pointing to edible bean as well as holding is non valid holding container volition throw exception.
5) If variable pointed yesteryear "var" doesn't exists than JSTL <c:set> tag creates it for y'all entirely if value is non null.
6) Scope attribute is optional inwards JSTL <c:set> tag as well as default reach is page scope.


These were around examples of  JSTL meat <c:set> or laid tag. <c:set> is real convenient as well as provides lots of fundamental functionality required inwards JSP pages. I recommend using <c:set> over <jsp:property> or JSP actions. Let me know if y'all come upward across whatsoever other skillful illustration of <c:set> tag which nosotros tin include here.

Further Learning
Difference betwixt SendRedirect as well as Forward inwards JSP?

0 Response to "Jstl Ready Tag Examples Or ≪C:Set≫ Inwards Jsp – Coffee J2ee Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel