Pass variable from daughter page to parent page in jsp

Asked

Viewed 269 times

1

I’m making a simple crud in java using jsp and servlets. And I came across the following situation:

I have a layout.jsp which makes the include of another dynamic page, which in this case will be the excluir.jsp. The title of the page is set within the excluir.jsp then the page of layout.jsp does not see the value of my variable title.

I ask: how to do what layout.jsp can take the value of title.

Obs: I’m trying my best, don’t put this information on servlet. Yes I know that if this variable is defined in servlet the layout .jsp can take value.

jsp layout.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt"       prefix="c"   %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"       prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml"       prefix="x"   %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql"       prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"  %>
<!doctype html>
<html lang="pt-BR">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><c:out value="${title}" /></title>
    <link href="/css/bootstrap.min.css" rel="stylesheet" />
    <link href="/css/theme.css" rel="stylesheet" />
</head>
<body>
<c:import url="menu.jsp" />
<div class="container">
    <c:import url = "${page}" />
</div>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>

delete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt"       prefix="c"   %>
<c:set var="title" scope="request" value="Excluir página"/>
<h1><c:out value="${title}" /></h1>
  • Have you tried using jsp:include? Here you include jsp pages and pass parameters to daughters.

  • On the other hand, I have the page daughter (it has the value ) and the parent page that receives the variable

  • Ahhhhh. As in the request, using a request.getParameter("title") on the parent page does not return the value?

  • No, it comes in white(null)

1 answer

1


Declare your c:import as a variable somewhere before where you are using the attributetitle. Example:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt"       prefix="c"   %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"       prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml"       prefix="x"   %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql"       prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"  %>

<c:import var = "importedPage" url = "${page}" />   <%-- Aqui você declara seu import --%>

<!doctype html>
<html lang="pt-BR">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><c:out value="${title}" /></title>
    <link href="/css/bootstrap.min.css" rel="stylesheet" />
    <link href="/css/theme.css" rel="stylesheet" />
</head>
<body>
<c:import url="menu.jsp" />
<div class="container">
    ${importedPage}                                 <%-- Aqui você usa seu  import --%>
</div>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>
  • Perfect! thank you very much.

Browser other questions tagged

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