How to use variables declared in other JSP pages after include?

2

I have a jsp page with only two includes in the whole code:

<%@ include file = "../_global/prefixos.jsp" %> 
<%@ include file = "../_global/testaProfessor.jsp" %>

In prefixos.jsp I have the following:

<%@ taglib prefix="c"    uri="http://java.sun.com/jsp/jstl/core" %>

In testaProfessor.jsp I have the following:

<jsp:useBean id="validaLogin" class="dao.usuario.TestaLoginUsuario" />

<c:set scope="request" var="usuarioLogado" value="${validaLogin.testaLoginUsuario(pageContext.request)}" />

<c:set scope="request" of page testaProfessor.jsp does not have access to <%@ taglib prefix="c" of page prefixos.jsp

What should I do to resolve this problem?

EDITION

The answer given by @Bruno César, although it may help in other situations, in the context of my problem does not help.

The case is that if I include a file within a b, what is in a can be used in b. But that's not what I need.

I need two files a and b to be included in a file c and the contents of the file a to be available to file b as well. Not just the c file.

Any ideas?

Example:

index.jsp file

<%@ include file = "../_global/prefixos.jsp" %> 
<%@ include file = "../_global/testaProfessor.jsp" %>

I need the contents of prefixos.jsp to be available for testaProfessor.jsp

preixos.jsp

<%@ taglib prefix="c"    uri="http://java.sun.com/jsp/jstl/core" %>

testaProfessor.jsp

<jsp:useBean id="validaLogin" class="dao.usuario.TestaLoginUsuario" />

<c:set scope="request" var="usuarioLogado" value="${validaLogin.testaLoginUsuario(pageContext.request)}" />
    
asked by anonymous 15.08.2015 / 15:19

1 answer

1

In order to reuse taglibs declarations, you should use the include directive, something like this:

<%@ include file="<nomeDoArquivo.ext>" %>

This directive makes the static inclusion, being evaluated at page translation time, ie when the JSP has not yet been compiled.

<jsp:include /> is dynamic and is used more for dynamic data coming from other JSP pages.

As a good practice you can separate all taglibs into a taglibs.jsp file, for example, with content close to this:

<%@ taglib prefix="c"    uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn"   uri="http://java.sun.com/jsp/jstl/functions" %>

On the other pages, just include this file, always before the first use, something like this:

<%@ include file="/templates/taglibs.jsp" %>

An example page would look like this:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>   

<%@ include file = "WEB-INF/templates/taglibs.jsp" %>

<!DOCTYPE html>
<html>
<head>
    <title>Exemplo usando include Estático</title>
</head>
<body>
    <!-- conteúdo da página -->
 </body>
</html>

You should always note that a prefix for taglib should be declared only once, so if you include the file taglibs.jsp on a X page that is included in a% , do not add Y back to page taglibs.jsp .

In case you want to reuse dynamic variables, in case you are using JSTL, simply change the scope of your variable.

The default scope for Y is <c:set> (see section page of specification ), so the variable does not" survive "on another page when you do the inclusion.

So to change this, just use something like this:

<c:set var="sala" value="${listaSala.getSala(usuarioLogado.sala.id)}" scope="request" />
<c:set var="usuarioLogado" value="${validaLogin.testaLoginProfessor(pageContext.request)}" scope="request" />
    
15.08.2015 / 17:04