Sans JSTL,
scriptlet and custom tag hell,
things much better now.
So what is JSTL? It's a standard custom tag library that can provide extremely useful functionality to your JSP pages. Common programming tasks like looping and flow control, XML processing, and database access can be implemented much more elegantly using these tags. Without them - in many cases anyway - you'd be reduced to using scriptlets, or writing your own custom tags.
So, enough with the distractions, and on with the core tags. These are the ones we need to know about for the exam, there are 14 in total:
<c:out>, <c:set>, <c:remove>, <c:if>, <c:choose><c:when>, <c:otherwise>, <c:catch>, <c:forEach><c:forTokens>, <c:url>, <c:import>, <c:redirect>, <c:param>
As you can probably guess from the names, these solve fairly fundamental, and common problems, ie looping, flow control, including other pages etc. Each tag can have a whole bunch of attributes, and can sometimes contain stuff in the tag body. So where to start?
Firstly there are some commonly used attributes. "var" is probably the most commonly used, and is used to provide the name of an attribute you want to store the result of whatever it us you've just evaluated. For example:
<c:catch var="cheeseError" >
<c:set var="favouriteCheese" value="gorgonzola" />
<c:if var="isFavouriteStilton" test="${favouriteCheese == 'stilton'}" />
<c:import var="allTheCheeses" url="http://en.wikipedia.org/wiki/List_of_British_cheeses" />
</c:catch>
Where you see "var" you also often see "scope". This is used, as you might guess to specify the scope of the attribute, any one of the usual four values are allowed, so you could have scope="page", scope="request", scope="session", scope="context". The two iteration tags, <c:forEach> <forTokens>, and also <c:catch> have a "var" attribute, but no "scope" attribute.
"test" appears in the flow control tags <c:if> and <c:when>
"value" appears in any of the tags that involve setting a value of some kind. <c:set>, <c:param> and <c:url>. If this attribute is omitted, the value can be placed in the body of the tag.
So what do they all do?
This should be mostly obvious from their names, but here's a more detailed run down.
<c:out>
This very useful tag can be used to output a value, and has some distinct advantages over embedding EL or an expression I here you ask? Well, expressions and EL don't have default values, and expressions and EL don't automagically escape any nefarious xml tagging for you!
<c:out> has three attributes, "value" which is mandatory, and default, and escapeXml which are optional. You can use "default" to set a default value that gets output when value resolves to null. "escapeXml" can have a value of either true or false. If it's set to true <, > &, ', and " get changed to matching entity references. If escapeXml is omitted, you get esapeXml="true" implicitly.
<c:set>
<c:remove>
Guess what, you can use these for setting and removing attributes. <c:set> has the "value", "var", and "scope" like you might expect. But it can also work on bean properties, in which case you can use "target" and "property" to name the bean and it's property you want to set.
<c:remove> does the what you'd expect, although doesn't have any bean manipulation powers. Has a mandatory "var" attribute, and an optional "scope" attribute.
<c:if>
If an expression - declared using the "test" attribute - resolves to true, process the body of the tag. If it resolves to false, skip the tag body! All very logical and self explanatory. But wait, there's more! You can store the boolean result of the test expression in a variable for use later. The <c:if> doesn't even need to have a body. Use the usual "var" and "scope tags to do this.
<c:choose>
<c:when>
<c:otherwise>
You can consider these a bit like a Java switch statement, or an if - else if - else chain.
<c:choose> has no attributes, and is use to mark the begining and end of the conditional operation. It must have at least one <c:when> and at most one <:c:otherwise> tags.
<c:when> has just one attribute "test", which as one may guess determines whether the body of the when is executed. You can have multiple <c:when>s. You can only have one <c:otherwise> which has no attributes, and acts like as a default, should all the corresponding <c:when>s fail. Have an example:
<c:choose>
<c:when test="${someValue == someOtherValue}">
Some JSP that might or might not get processed.
</c:when>
<c:when test="${someValue == aDifferentValue}">
Some other JSP that might or might not get processed.
</c:when>
<c:otherwise>
None of the c:whens fired, now you have a default value!
</c:otherwise>
</c:choose>
<c:forEach>
<c:forTokens>
These as you may guess are used for iteration, and are very similar.
Both have "items", "begin", "end", "step", "var", and "varStatus".
"begin" specifies index to begin with, "end" specifies the index to end on, and "step" is step you want to take on each iteration. None of these are mandatory.
"var" is the name of an attribute to hold the object retrieved at the current step of the iteration, and "varStatus" is the name of an attribute to hold a javax.servlet.jsp.jstl.core.LoopTagStatus (this provides access to information about the current iteration you are processing). Neither of these are mandatory either.
So what can you iterate over? Well pretty much anything, and even nothing. You specify the object you want to iterate over using the "items" attribute, this could be a java Array, a Collection, an Enumeration, a String. It can also be left out, in which case the tag iterates over nothing, but assuming you've supplied a "begin", "end", and/or "step" attributes it can be used to repeat an action a specific number of times.
You can even iterate over String. If you're using <c:forEach>, the string is assumed to be a comma separated list of substrings. If you want to use a different delimiter you can use <c:forTokens> which can only operate on String objects, and has an extra attribute - "delim"- which can be used to, surprise, surprise, specify a particular delimiter.
<c:url>
This is used to generate a url, and either output it, or store it in a variable. It has the usual "var" and "scope" attributes to store the url if you want. It can take a "context" attribute which you can use to specify a local url to a foreign context, and finally a "value" attribute, which contains the url. This tag has one useful property in that it can automatically add the "jsessionid=" parameter if url rewriting is being used for session logging.
<c:import>
This tag can be used to import the contents of another page into the current page. It's a but like <jsp:include> except (as usual) has several advantages.
Advantage one: it doesn't have to be a locally accessible resource, it could be from any where on the intertubes. Advantage two: you can store the contents in an attribute for processing later!
This tag has the usual attributes you'd expect. "url" is the only mandatory one, and I shouldn't need to tell you what you put in it! There are the usual "var" and "scope" attributes. It also has "context" which works like the "context" attribute in the <c:url> tag. "charEncoding", is used to specify the character encoding of the import source, and finally "varReader" which you can use to specify the name of a reader object that you can use to process the resource. Access to this reader is scoped to only be available in the body of the tag.
<c:redirect>
Does a simple redirect. Only has "url" and "context" attributes, which do what you'd expect.
<c:param>
Finally <c:param> this tag can be placed in the bodies of <c:url>, <c:import>, and <c:redirect> to specify extra request parameters. It has a mandatory "name" attribute, and an optional "value" attribtue, both do what you'd expect.
That's it for the JSTL core tags. Hope it was useful. As usual there's plenty more info on Sun's site: http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
19 comments:
http://quotehawks.com/
http://www.getmobileprice.com
https://hussaincaters.com/
https://musainterior.com
http://thenethawks.com/mobile-app-development-companies-in-pakistan/
https://aiwah.pk/
http://www.unikaas.com
https://unikaas.com/replica-clothing-supplier/
http://bikeparts.pk
http://desiseries.com
We are one of the best in providing pre written research papers for sale services as we submit our papers on time and students will have a satisfactory time to go over the essay trying to counter check for any mistake or error which might need urgent correction. Seek services of our research papers help team and get the best results.
I appreciate your post thanks for sharing the information.
affordable printed Tie Boxes - Sleek Boxes
best Custom Hair Extension Packaging Boxes with logo
best Custom Handover Packaging Boxes with logo
Amazing product thanks for sharing with us It is very informative.
Branding Lingerie Boxes
bulk custom-made Holiday Party Boxes
Thanks for picking out the time to discuss this,
Punjabi Bagh Escorts
Escorts in Mahipalpur
Aerocity Escort Service
karol bagh Call Girls
Call Girls in Lajpat Nagar
Escorts Service in Malviya Nagar
Connaught Place Escorts Service
I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such valuable help again.
Travel Booking Software provider in india
Flight Booking Software provider in india
Bus Booking Software provider in india
Cab Booking Software provider in india
Love to rea this and found it really informative,share more content like this Now it's time to avail african gowns for ladies for more information.
This work is being done in an overly modern manner. I want to share a current website because programming is so well-liked these days. If you want to learn how to write a dissertation in a novel way, visit the nursing dissertation writing service right away. You make lovely decisions that seem to place them above you.
I really apricate your work in this article. Its all about JSTL is really helpful and useful. I hope you will share more updates and good information. Now it's time to avail jupiter florida airport for more information.
You have shared a very informative article with us and this article is very helpful for students. I will share this article with my friends. Now it's time to avail Windows Installation Services In Marion County FL for more information
Great blog post about JSTL! It's amazing to see how it simplifies complex Java code. On another note, it's important to prioritize our health. For those who need medical advice, remember to talk to a doctor online free now. Stay healthy and keep up the good work!
I have read your article. This article contains very valuable information for programming students. Keep sharing tech related article with us. Now it's time to avail Luxury Limo Servcies in Los Angles for more information.
Excellent post. Your article proves the experience that you have got in the tech field. Keep sharing more tech-related articles with us. Now it's time to avail https://goodwaytransportation.com for more information.
I have browsed for this topic and suddenly I landed here thank you so much for sharing valuable content with your readers. This article is very helpful for us. Keep sharing more articles on tech. Now it's time to avail Metro Limo Detroit for more information.
Thanks for sharing this coding information. It is very useful for me and students which learn coding. Now its time to avail Kabou taxi cab services in Southfield Mi for more information.
Tom's Programming Blog is a goldmine of insights! Exploring ERP software in Saudi Arabia through his perspective is enlightening. An excellent resource for anyone seeking to navigate the tech landscape in the Kingdom.
Exploring JSTL is a dynamic dive into web development. Just as visual coding intrigues, considering sonic components could enhance the learning journey. Imagine the satisfying 'ping' sound when code runs successfully! Your post delves into technical exploration, and I'm curious about the potential auditory cues that could complement programming achievements. A fascinating read for aspiring developers!
I was searching to read that kind of article and I finally found it. I am very much impressed with your content that you have shared in this article. Great post. Now its time to avail Jump Start Services in Washington DC for more information
Exploring different programming languages like Java, Scala, and Objective C is an exciting journey for any developer. Whether you're coding late into the night or taking a break, staying comfortable in heavy tees makes the experience even better. It's all about balancing focus and comfort while diving deep into the world of code.
Post a Comment