|
Page 3 of 8
3.
Implementation of JSTL Core Tags
First, let’s make a new JSP page. Right
click on Web Pages folder, New and JSP…
Then a new dialog will show up as displayed
above. Let’s call it Jstl_Hello_World.jsp. Then click Finish to create the jsp. Now we have 2 JSPs – index.jsp and
Jstl_Hello_World.jsp.
We will create a table in index.jsp for the
sample of this tutorial. Place these codes inside the tag <body> in
index.jsp
<table border="1">
<tr>
<td>
<a href="Jstl_Hello_World.jsp">Hello World!</a>
</td>
<td>
Print out String "Hello
World!"
</td>
</tr>
</table>
Now for the Jstl_Hello_World.jsp, we are
going to write some JSTL tags to print out the sentence “Hello World”. Do not
forget to uncomment the taglib directive for the JSTL library.
<%@taglib
uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:out value="Hello World!" />
The above codes simply print the sentence
“Hello World” to the JSP.
<c:set var="sentence"
value="Hello World with <c:set/>" />
<c:out value="${sentence}"
/>
Above codes will do similar thing. The
difference is that the sentence was stored into a variable named “sentence” via
<c:set> tag and then display it from by using ${variable_name}
<c:remove var="sentence" />
<c:out value="${sentence}" />
The tag <c:remove> works in opposite
with <c:set>. In the above codes, the <c:out> will not print
anything out anymore because the variable called “sentence” has been removed.
Press Shift+F11 to build the project and F6
to run. We will see something like this for the index.jsp. We may click the “Hello
World” link to see our first JSTL tag
in action.
And
this is the result after we click the link.
Ok, we have completed our first Hello World
JSTL. Now let’s move on to the logical JSTL core tags. We need to create
another link in our index.jsp. The link will contain 3 parameters which hold some
values which are boolean, string and numeric. We are goint to see a simple
demonstration on how we are supposed to use logical JSTL core tags.
|
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.