Create a new JSP file. Follow exactly the same way as we created the Jstl_Hello_World.jsp but name it Jstl_Core_Tags.jsp. Then we need to modify our index.jsp to contain the link to our newly created JSP. So please add below codes to our index.jsp just right after our first link to open the Hello World JSTL tags.
Now, we could see the new JSTL tag in index.jsp here. is normally used to embed a parameter values to the URL link. While, tag is basically used for creating a link of URL and stores the link in variable named “url”. In fact, this combination of and tags will do the same as the link that we hard coded before. The final link should be in form of Jstl_Core_Tags_2.jsp;jsessionid=…?param_1_name=param_1_value¶m_2_name
(small) (medium) (large) Let’s see how it works. Press Shift+F11 to build the project and F6 to run.
JSTL if tag and EL operators
What we do here is basically to send some parameters so that they are accessible from the target page.
On the target page, we can retrieve these parameters via ${param.parameter_name} and we are going to use the logical JSTL tags to compare some values. Add below codes to Jstl_Core_Tags.jsp
A test shows that parameter valid is ${param.valid}
This tag is used to determine whether parameter valid value is true or false. If the value is true then it prints out the sentence. On the other hand, if it is false, it should not print anthing out. You may like to try to modify the valid value to be false in the index.jsp. The sentence should not be printed out if the value is false. Until now, you should know that if you would like to access any parameters, you may use the keyword “param”. It is similar to the request.getParameter.
A test shows that parameter name contains value of ${param.name} via != operator
The above tag is to check whether parameter name is null or not. If it is not null then it prints out the sentence and it prints nothing if the value is null.
A test shows that parameter name contains value of ${param.name} via ne operator
Above codes are working similarly with the previous codes. We just want to test JSP notation “ne” for not equal function as what “!=” does.
A test shows that parameter name contains value of ${param.name} via empty operator
Above codes are another feature of JSP notation to check a value whether it is empty or not. It is basically pretty much the same as the second and third tag do. The difference lies on the value null and empty. What is the difference between empty and null? Well, empty is not necessarily be null but null always be empty. Null means that the variable is simply not existed while empty means that the variable is existed and initialized but it contains nothing. Please be careful when dealing with null values as it may cause you the famous NullPointerException.
A test shows that parameter mark contains value of ${param.mark} which lies in range of 0 to 10
Above codes are used to check whether parameter with the name of “mark” is within the range of 0 to 10. If yes then it prints the sentence.
Again, press Shift+F11 to build the project and F6 to run. The index.jsp will now looks like this
Try to click on the second link (JSTL Core Tags) and it should look like below screenshot.
You may be interested to try out some different parameters values for this JSTL tag. Remember, you will learn a lot by practice. Let’s move forward to more advance JSTL core tags.
Create another JSP page. This time, we can name it with Jstl_Core_Tags_2.jsp. We also need to create another link in index.jsp to open up this page as well. Add below codes to create the additional new link in index.jsp.
Play around with JSTL forEach, forTokens, choose, when, and otherwise
Now, we could see the new JSTL tag in index.jsp here.
=param_2_value. This is how we are creating a dynamic link using JSTL tag. After this, we can use to display it.
In this case, we have 2 parameters named test_for_each and test_for_tokens. Next, we need to write JSTL forEach and JSTL forTokens tags in Jstl_Core_Tags_2.jsp to see how they work.
Above codes will take values of the parameter “test_for_each” and stores it in variable called “part_each”. Within the tag forEach, we print the variable “part_each” using . It loops through default separator ‘,’ (comma) until it reaches the end. is extremely useful when you are sending some Collection object to the JSP and you need to iterate the content of the Collections to your JSP.
Above codes are the sample of more advanced forEach loop. We first declare a variable “i” and we also define the beginning, ending and stepping value for the variable “i”. The above tag means that it should loop from 1 to 20 in increment of 2. To make it more interesting, we could add a condition to check whether the current value of variable “i” is in the range of small, medium or large by using JSTL tag , and . This one (, and ) is equivalent to switch case default syntax in conventional programming language.
This tag works in same way as forEach tag. But it has additional delims attribute to tokenize values with certain delimiters. What does it mean? It means that it will separate or tokenize the values within its items attribute with the delimiter. So if you have the String of “1,2,3” and the delimiter is “,”, the result would be three items i.e. 1, 2 and 3. How about if we want to separate the values for multiple delimiters? You can initialize it using multiple delimiters as delims=”;|()” to tokenize the values.
And this is how the result looks like.
Next, we are going to explain on and . These 2 tags are simple. simply redirects us to another page and is similar to that will extend imported page to current page.
Comments
Post a Comment