JSTL set tag or
1) Set bean properties
2) Set Map values
3) Create Scoped variable on page, request, session or application scope.
But <c:set> comes with its own caveat like
This article is in continuation of my earlier post on JSP servlet e.g. Difference between URL Rewriting and URL Encoding in JSP Servlet? and How to define default error page in J2EE web application if haven't already read you may find some useful information.
JSTL Core et> tag
As I said earlier JSTL Set tag or
<c:set var="currency" value="USD" />
JSTL Example : How to create variable in session scope using tag in JSP
Above example of
<c:set var="currency" value="USD" scope="session" />
value of variable can also be a runtime expression e.g jsp expression or EL expression. like the one which is shown in below example of set tag:
<c:set var="currency" value="${user.currency}" scope="session" />
here Container will copy bean property user.currency in "currency" variable.
JSTL Example : How to pass value in tag body in JSP
Another variance of jstl <c:set> tag is that you can supply value in body instead of on attribute line. some time when value is long, giving it on body makes code more readable. here is example of supplying value on jstl set tag body:
<c:set var="currency" scope="session" >
USD,EUR,AUD,INR
</c:set>
JSTL Example : How to remove attribute using in JSP
Keep in mind that
<c:set var="currency" value="${user.currency}" />
<c: set> tag of jstl will remove "currency" attribute from any scope if EL expression ${user.currency} will resolve to null.
JSTL Example : How to set bean property using JSTL tag in JSP
All of above example were for setting attribute or creating variables, but <c:set> can also be used to set bean properties or map value. in that case instead of "var" we need to use "target" and "property" which will define bean and property name to be set. if "target" is map than "property" is name of key and "value" is value for that key. What is worth noting here is that <c:set target=""> must point to a real object and not the name of object as it was with <jsp:useBean> action. if "target" doesn't resolve into object than web container will throw exception. here is example of setting bean property using JSTL <c:set> tag in JSP:
<c:set target="currencyMap" property="USA" value="USD">
this is equivalent to currencyMap.put("USA", "USD"); just like before you can also supply value in body of jst <c:set> tag in jsp. here is another example of setting bean properties using JSTL <c:set> tag :
<c:set target="trade" property="currency" value="USD">
this example will set currency property of bean named "trade" and equivalent to :
trade.setCurrency("USD");
You just need to make sure that target should resolve into real object.
JSTL <c:set> - Points to remember
Here are few important points to remember on
1) <c:set> can remove attribute if value="null".
2) Value for JSTL <c:set> tag can be specified in body also.
3) JSTL set tag value can be runtime expression include JSP expression or EL expression.
4) In target version of <c:set> if target is pointing to bean and property is not valid property container will throw exception.
5) If variable pointed by "var" doesn't exists than JSTL <c:set> tag creates it for you only if value is not null.
6) Scope attribute is optional in JSTL <c:set> tag and default scope is page scope.
These were some examples of JSTL core
Other Java J2EE Tutorials you may like:
Tidak ada komentar:
Posting Komentar