How to include .jsp file in the solution?

3

I'm developing Java web applications using JSP's. I have a file called header.jsp , which contains links to CSS files and other things that are important to my system that is in the root directory.

I have a folder that has other .jsp files, but when I try to make a header.jsp inclusion file, it does not work.

    
asked by anonymous 11.11.2014 / 04:24

1 answer

3

So, suppose you want to include header.jsp inside a file called clientes.jsp (you did not specify the name, so I invented one to be able to exemplify). There are basically 4 ways to do this:

Alternative 1: Include directive

Just use the following in clientes.jsp :

<%@ include file="header.jsp" %>

This will copy-and-paste the code from header.jsp into clientes.jsp before compiling it into a servlet java. Inclusion occurs at compile time.

Alternative 2: Standard action jsp: include

You use this way in clientes.jsp :

<jsp:include page="header.jsp" />

This will make the compiled servlet of header.jsp be invoked from within the compiled servlet of clientes.jsp . Inclusion occurs at runtime when the request is being processed.

You can pass parameters in jsp:include :

<jsp:include page="header.jsp">
    <jsp:param name="teste" value="Parâmetro de teste" />
</jsp:include>

In the header.jsp file, you can get the value of the parameter (s) using EL:

${param.teste}

Alternative 3: Tag c: import from JSTL

This alternative is similar to the form used with the standard action:

<c:import url="header.jsp" />

<c:import> allows you to import up to external documents to your application container:

<c:import url="http://example.com/header.jsp" />

And you can also use parameters:

<c:import url="header.jsp">
    <c:param name="teste" value="Parâmetro de teste" />
</c:import>

In the header.jsp file, you can get the value (s) of the parameter (s) using EL, in exactly the same way as the standard action:

${param.teste}

Alternative 4: Tagfiles

This form is the most complicated, but it is also the most flexible, robust and powerful. You can put the header.tag file inside the WEB-INF/tags folder. Note that the file extension is tag and not jsp in this case.

So, in your clientes.jsp you just put this in the beginning:

<%@ taglib prefix="meuProjeto" tagdir="/WEB-INF/tags" %>

And in the middle, where you want to put the header:

<meuProjeto:header/>

If you want to pass one (or more) parameter (s), use the following:

<meuProjeto:header teste="Tagfiles são legais"/>

And there in your header.tag file, you put this in the beginning:

<%@ attribute name="teste" required="true" rtexprvalue="true" %>

And in the same file header.tag , you retrieve the parameter (s) with these extremely simple EL expressions:

${teste}

The required specifies whether the attribute is required or not (optional). The rtexprvalue indicates whether the value will be evaluated at runtime when the request is being processed or not.

Note that when using tagfiles, the result resembles a function call, where you say which parameters are required or not and the clientes.jsp code to include header.tag is super smooth and simple. In addition, the parameters are defined with a static (non-dynamic) scope, which makes it much easier for the developer to avoid problems of having to guess the parameter name or to collide with the name of something else and also force a error if a required parameter is forgotten or if a nonexistent parameter is used.

If the content of the parameter is too large, you can declare a body for it. To do this, put the following at the beginning of your header.tag :

<%@ tag body-content="tagdependent" %>

And to interpret the body:

<jsp:doBody/>

No clientes.jsp you invoke this way:

<meuProjeto:header teste="tagfiles sao legais">
    Este é um exemplo de uma tagfile aonde é usado um corpo bem grande.
    Lorem ipssum dolor sit amet consectetuer adispiting elit.
</meuProjeto:header>

The body of the tag is interpreted when <jsp:doBody> is invoked, it is not just copied and pasted, but rather a form of a lambda in JSP. So you can do this:

<meuProjeto:header teste="tagfiles sao legais">
    <c:if test="${variavelQualquer == 1}">
        O valor da variável é 1.
    </c:if>
    <c:if test="${variavelQualquer == 2}">
        O valor da variável é 2.
    </c:if>
</meuProjeto:header>

Finally, you can put your tag files inside any subfolder of WEB-INF/tags , and usually that's enough. Alternatively, it is possible to place them within META-INF/tags velopers of JARs that are within WEB-INF/libs , but in this case you will need to add a TLD file and get a bit more complicated.

Source: "Head First Servlets & JSP" by Bryan Basham, Kathy Sierra and Bert Bates. I used the first edition, which is already old and outdated (2004), but it is still very good and I recommend it a lot.

    
12.11.2014 / 00:19