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

JSTL laid tag or <c:set> likewise called every bit JSTL Core tag library is a adept replacement of <jsp:setProperty> jsp activity which lacks lot of functionality too exclusively allow you lot to laid edible bean property. you lot tin plough over notice not set Map's primal value or create a scoped variable past times using <jsp:setProperty>. jstl <set> tag allows you lot to practise all the materials related to setting or creating variables or attributes. past times using JSTL <c:set> tag you lot tin plough over notice :
1) Set edible bean properties
2) Set Map values
3) Create Scoped variable on page, request, session or application scope.

But <c:set> comes amongst its ain caveat similar <c:set> tin plough over notice take 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 plough over notice practise too how to usage JSTL <c:set> tag.

This article is inwards continuation of my before postal service on JSP servlet e.g.  Difference betwixt URL Rewriting too URL Encoding inwards JSP Servlet? and  How to define default mistake page inwards J2EE spider web application  if haven't already read you lot may uncovering merely about useful information.

JSTL Core <c:set> tag

jsp activity which lacks lot of functionality too exclusively allow you lot to laid edible bean belongings JSTL Set tag examples or <c:set> inwards JSP – Java J2EE TutorialAs I said before JSTL Set tag or <c:set> is usage to laid edible bean properties, map value too to create scoped variables. <c:set> tin plough over notice endure used inwards ii unlike ways either past times using "var" or past times using "target". var is used to define mention of variable needs to endure created or whose values needs to endure set. spell "target" is used to announce Bean Object or Map whose values needs to endure set. here is a unproblematic example of JSTL <c:set> tag

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


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

Above representative of <c:set>  will create an attribute named "currency" amongst value "USD" inwards default scope which is page scope. you lot tin plough over notice likewise create attribute into merely about other scope say session scope, inwards that representative you lot demand to specify orbit amongst <c:set> attribute scope="". hither is an representative of creating variable on session scope using jstl laid tag:

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

value of variable tin plough over notice likewise endure a runtime appear e.g jsp appear or EL expression. similar the i which is shown inwards below representative of laid tag:

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

here Container volition re-create edible bean belongings 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 you lot tin plough over notice provide value inwards trunk instead of on attribute line. merely about fourth dimension when value is long, giving it on trunk makes code to a greater extent than readable. hither is representative of supplying value on jstl laid tag body:

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


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

Keep inwards hear that <c:set> tin plough over notice likewise take 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 take "currency" attribute from whatsoever orbit if EL appear ${user.currency} volition resolve to null.


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

All of higher upwards representative were for setting attribute or creating variables, but <c:set> tin plough over notice likewise endure used to laid edible bean properties or map value. inwards that case instead of "var" nosotros demand to usage "target" too "property" which volition define edible bean too belongings mention to endure set. if "target" is map than "property" is mention of primal too "value" is value for that key. What is worth noting hither is that <c:set target=""> must signal to a existent object too non the name of object every bit it was amongst <jsp:useBean> action. if "target" doesn't resolve into object than spider web container volition throw exception. hither is example of setting edible bean belongings using JSTL <c:set> tag inwards JSP:

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

this is equivalent to currencyMap.put("USA", "USD"); merely similar before you lot tin plough over notice likewise provide value inwards trunk of jst <c:set> tag inwards jsp. here is another representative of setting edible bean properties using JSTL <c:set> tag :

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

this representative volition laid currency belongings of edible bean named "trade" too equivalent to :

trade.setCurrency("USD");

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


JSTL <c:set> - Points to remember

Here are few of import points to recall on <c:set> tag from JSTL nitty-gritty tag library. I convey seen nosotros oftentimes overlooked these modest subtle details which causes difficult to uncovering bugs inwards JSP:

1) <c:set> tin plough over notice take attribute if value="null".
2) Value for JSTL <c:set> tag tin plough over notice endure specified inwards trunk also.
3) JSTL laid tag value tin plough over notice endure runtime appear include JSP appear or EL expression.
4) In target version of <c:set> if target is pointing to edible bean too belongings is non valid belongings container volition throw exception.
5) If variable pointed past times "var" doesn't exists than JSTL <c:set> tag creates it for you lot exclusively if value is non null.
6) Scope attribute is optional inwards JSTL <c:set> tag too default orbit is page scope.


These were merely about examples of  JSTL nitty-gritty <c:set> or laid tag. <c:set> is real convenient too provides lots of primal functionality required inwards JSP pages. I recommend using <c:set> over <jsp:property> or JSP actions. Let me know if you lot come upwards across whatsoever other adept representative of <c:set> tag which nosotros tin plough over notice include here.

Further Learning
Difference betwixt SendRedirect too Forward inwards JSP?

Komentar

Postingan populer dari blog ini

Fixing Java.Net.Bindexception: Cannot Assign Requested Address: Jvm_Bind Inwards Tomcat, Jetty

Fix Protocol Session Or Admin Messages Tutorial

5 Deviation Betwixt Constructor In Addition To Static Mill Method Inward Java- Pros In Addition To Cons