This is from JSTL, which is a JSP technology.
The JSP (Java Server Pages) is the standard Java technology for producing HTML pages and dynamic websites.
Jsps are compiled for Servlets by a special compiler in the application container.
The Servlets are Java classes responsible for processing HTTP requests and producing HTTP responses.
A page produced by a JSP includes static parts and dynamic parts.
It can be said that JSP is a powerful template processing framework. That is, it builds strings that have been partially modeled to be filled with dynamic parts. More or less like a String.format
or String.printf
, but with much more versatility and flexibility and designed to process complex logic in the assembly process of String
resultant.
In JSP, there are two ways to define the dynamic parts, one is through scriptlets (considered obsolete) and the other is through Taglibs, which are custom tag libraries. Each of these custom tags corresponds to a dynamic text generation part in JSP, being rendered by means of complex logic implemented through Java classes.
To JSTL (Javaserver Pages Standard Tag Library) is the standard JSP library that contains several of these custom tags. Among these tags are:
<c:if>
- Processes the contents of the tag if a certain condition is true. Otherwise, skip the body of the tag. As the name suggests, it is the equivalent of if
in the JSP.
<c:out>
- Evaluates a runtime expression and concatenates its result in the text being generated dynamically.
<c:forEach>
- Repeats tag content multiple times by iterating a collection of elements. As the name suggests, this is the equivalent in JSP to Enhanced-for java.
<c:choose>
- Contains several tags <c:when>
and optionally a tag <c:otherwise>
. This is the Java equivalent of a sequence of if
s and else if
s and else
.
<c:when>
- Similar to a if
or else if
that is inside a <c:choose>
- Processes the contents of the tag if an expression is true, such as if
. The difference of <c:when>
and of <c:if>
is that only the first <c:when>
from within the <c:choose>
who has the true expression will have his body processed, to the detriment of others.
<c:otherwise>
- Equivalent to a else
- Process the body if none <c:when>
antecedent within the <c:choose>
have the true condition.
There are also several other tags in JSTL, but these above are the main and most used.
To process JSTL, JSP uses EL (Expression Language) which is a simple sublinguage that evaluates runtime expressions. EL is able to call methods, manipulate Java Beans-like objects, perform arithmetic calculations, and more.
Normally JSP and JSTL are used to generate HTML or XML dynamically, but nothing prevents you from using it for other things, such as generating SQL, and this is what happens in your case.
I believe your code is preceded by something like this:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
This is why it declares that JSTL is being imported into your JSP. It also declares that JSTL will be imported using the prefix c
.
In your code, I believe it contains <c:choose>
, <c:when>
and <c:otherwise>
instead of just <choose>
, <when>
and <otherwise>
. That prefix c
is important, and it is the prefix that was defined there in the <%@ taglib
. So let’s see this code:
SELECT
<c:choose>
<c:when test="filtro.veiculo.name() == 'VAGAO'">
tipoVagaoTrad.Xfpdstsv AS TIPO_VAGAO,
serieVagao.Xflcdsvg AS SERIE,
vagao.xfrcdvag AS VAGAO,
frotaVagao.Xf5cdflo AS FROTA,
frotaVagao.Xf5tpflo AS TIPO_FROTA_VAGAO,
frotaVagaoTrad.Xm9dscfl AS DESCRICAO_FROTA,
vagao.xfrindrg AS REGIME,
</c:when>
<c:otherwise>
modeloLocomotiva.X,
locomotiva.xf0cdloc || '-' || empProp.XN4CDEMP AS LOCOMOTIVA,
frotaLocomotiva.Xf2cdflo AS FROTA,
frotaLocomotiva.Xf2tpflo AS TIPO_FROTA_LOCOMOTIVA,
frotaLocomotivaTrad.Xm8dscfl AS DESCRICAO_FROTA,
locomotiva.xf0indrg AS REGIME,
folhaEspLocomotiva.Xf3poliq AS HP_LOCOMOTIVA,
</c:otherwise>
</c:choose>
That one "filtro.veiculo.name() == 'VAGAO'"
is an EL expression that picks up the object filtro
, accesses the method getVeiculo()
and then access the method name()
and compare the result with the String
"VAGAO"
, getting a boolean
as a result, this being boolean
the result of the expression.
At EL, it’s okay to compare String
s using EL, because he always uses the equals
behind the scenes.
If the EL result is true, the condition of the <c:when>
will be true and the block between the <c:when>
and the </c:when>
will be placed in the output text.
If the result of EL is false (and consequently the condition of <c:when>
), the <c:choose>
follows the next block and enters the <c:otherwise>
, and therefore the content between the <c:otherwise>
and the </c:otherwise>
will be placed in the output text.
So what this code does is this:
If the filter vehicle name is "VAGAO
", then the following SQL is generated:
SELECT
tipoVagaoTrad.Xfpdstsv AS TIPO_VAGAO,
serieVagao.Xflcdsvg AS SERIE,
vagao.xfrcdvag AS VAGAO,
frotaVagao.Xf5cdflo AS FROTA,
frotaVagao.Xf5tpflo AS TIPO_FROTA_VAGAO,
frotaVagaoTrad.Xm9dscfl AS DESCRICAO_FROTA,
vagao.xfrindrg AS REGIME,
Otherwise, generates the following SQL:
SELECT
modeloLocomotiva.X,
locomotiva.xf0cdloc || '-' || empProp.XN4CDEMP AS LOCOMOTIVA,
frotaLocomotiva.Xf2cdflo AS FROTA,
frotaLocomotiva.Xf2tpflo AS TIPO_FROTA_LOCOMOTIVA,
frotaLocomotivaTrad.Xm8dscfl AS DESCRICAO_FROTA,
locomotiva.xf0indrg AS REGIME,
folhaEspLocomotiva.Xf3poliq AS HP_LOCOMOTIVA,
More information about JSTL can be found at:
Ah, and all this has nothing to do with Hibernate mapper.xml
.
NOTE: In the question as it was originally, I did not identify that this would be JSTL (although I noticed the similarity) because of the tags <select>
and <where>
which were in the original question, since these tags are not JSTL, the attribute was missing test
on the tag when
and the tags were missing the prefix c:
.
Where did you get these tags from? What tool does it use? I’ve never seen SQL being written with tags.
– Victor Stafusa
You have to have more details on this question. Let’s guess where this xml came from?
– Renato Afonso
Now that you edited it, it was much better. A pity that your question ended up getting so much negative vote because it was so bad initially. Anyway, I’ve already given the vote of reopening (only one left) and I already have an answer that I will post when it is reopened. I assume that from there is JSTL, a technology that is used in JSP.
– Victor Stafusa
I already have the answer ready, I’m just waiting for the reopening to post.
– Victor Stafusa