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

Asked

Viewed 957 times

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)}" />

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

What should I do to solve 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 a within a b, what this in a can be used in b. But that’s not what I need.

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

Any idea?

Example:

index file.jsp

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

I need the contents of prefixos.jsp 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)}" />
  • How you are declaring the variable?

  • I added data at the end of the question @Bruno César

  • You want to take advantage of the taglib statement, that’s it?

  • From what I understand... you can include the variable in the request scope, for example: request.setAttribute("foo", "valorDeFoo");.

  • 1

    @re22 then, that’s what I understood initially. After updating the answer already changed, it seems that he wants to repurpose the taglib statement. Anyway I’ll update a reply.

1 answer

1

To make the reuse of declarations of Taglibs you should you should use the directive include, something like that:

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

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

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

As good practice you can separate all Taglibs in an archive taglibs.jsp, for example, with a content next 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 it includes the archive taglibs.jsp on a page X which is included on a page Y, do not include again taglibs.jsp on the page Y.

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

The standard scope for <c:set> is page (see section 2.2.1 of specification), then the variable does not "survive" on another page when you make 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" />
  • um, so I guess that’s it. In php I do include 3 pages in 1 file and the code of the first included is available for the second included. In this case, then, instead of using <jsp:include page=", I should use the jstl <%@includefile=" which does the same as php include

  • Well, come on. I put it like this below in a.jsp prefix file: <jsp:useBean id="constants" class="util.Constants" /> <%@pageimport="java.util. *" %> <%@ taglib prefix="u" Uri="/WEB-INF/myutils.tld" %> <%@ taglib prefix="c" Uri="http://java.sun.com/jsp/jstl/core" %> But still, the <c:set Scope="request" is not finding the tablib

  • @Carlosrocha I think he did not understand the answer, maybe it is not a single doubt also... In the reply I give you a full example of what should contain the file and you included different things. See this and better understand the life cycle of things in JSP.

  • Check it out. I created a jsp file and put 2 lines in it only <%@includefile = ".. /_global/prefixes.jsp" %> and <%@ include file=".. /_global/testaAdmin.jsp" %>. prefix.jsp put a <%@ taglib prefix="c" Uri="http://java.sun.com/jsp/jstl/core" %> In the admin forehead I put <c:set var="userLogin" value="${validaLoginProfessor(pageContext request.)}" Scope="request" /> But it doesn’t work. Give warning of!

  • @Carlosrocha read the answer again and see when to use one or the other include, as said, you have to know when it is for static use or for dynamic use. Finally, study the link that you included in the previous comment. Thank you.

  • I read it again and the only thing I saw differently is that you used EL in the call to the arquvo: ${filename.ext}. But it doesn’t work that way. And without the other internal file they don’t recognize the tablib!

  • I changed the question with the proposed changes!

  • @Carlosrocha good, this is my last comment on this issue: 1. you have tried to make inclusion of prefixos.jsp in testaProfessor.jsp and not on the end page, which does not use the prefix c?; 2. I do not use EL, but any property, that is an example. Good studies.

  • I have not tried as it is not only testaProfessor.jsp that will use the Aglibs but mainly the asked pageConteudo.jsp will use. If you need to add the taglib in the questionConteudo.jsp too then it will look like <jsp include. What do you think?

  • @Carlosrocha unfortunately I don’t know how your project is, and I don’t intend to look. I will insist again and ask you to understand how the community works. This answer is a possible solution and can be used in N different designs. The shape you will use in yours is on your own, you have to know how to use the solution in your project in the way it looks best. You’re still confusing the includes, which means you don’t understand the difference between them. Read the specification, it’s the last thing I can tell you, read and understand/learn how things work.

  • Okay, I have further simplified the question to see if you understand my question. For only then will you be able to give an adequate answer!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.