简体   繁体   中英

Is it possible to include an Expression tag inside a page directive in JSP?

I am trying to include a jsp file into another one by using the parameters passed to the jsp through another jsp.

The code is

Template.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <% String t = (String) request.getParameter("title"); %> <title><%=t%></title> <link type="text/css" rel="stylesheet" href="css/jquery.dataTables.min.css"> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/jquery.dataTables.min.js"></script> <script type="text/javascript"> <% String js = (String) request.getParameter("script"); %> <%@ include file="<%=js%>" %> </script> </head> <body> <% String table = (String) request.getParameter("table"); %> <%@ include file="<%=table%>" %> </body> </html> 

Table.jsp

  <table id="Profiletable" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>Name</th> <th>Profile</th> </tr> <thead> </table> 

script.js

 $(document).ready(function() { var table = $('#Profiletable').DataTable( { "ajax":"Profiles.txt", "columns": [ {"data" : "Name"}, {"data" : "Profile"} ] } ); }); 

includer.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <jsp:include page="template.jsp"> <jsp:param name="title" value="Test"/> <jsp:param name="script" value="script.js"/> <jsp:param name="table" value="table.jsp"/> <jsp:include/> 

But the above code is not working. If i hard code the values at <%@ include file="fileName" %> then it works.

Difference between JSP include directive and JSP include action

JSP include directive

<%@ include file="filename" %> 
  • At JSP page translation time , the content of the file given in the include directive is pasted as it is, in the place where the JSP include directive is used. Then the source JSP page is converted into a java servlet class. The included file can be a static resource or a JSP page. Generally JSP include directive is used to include header banners and footers.

  • The JSP compilation procedure is that, the source JSP page gets compiled only if that page has changed. If there is a change in the included JSP file, the source JSP file will not be compiled and therefore the modification will not get reflected in the output.

JSP include action

<jsp:include page="relativeURL"/> 
  • The jsp:include action element is like a function call. At runtime, the included file will be executed and the result content will be included with the source JSP page. When the included JSP page is called, both the request and response objects are passed as parameters.

  • If there is a need to pass additional parameters, then jsp:param element can be used. If the resource is static, its content is inserted into the calling JSP file, since there is no processing needed.


Your Question

This is not possible with the include directive. The directive gets evaluated when the servlet for the JSP is being constructed long before the Java code on the page gets executed.

You can use a variable path with the <jsp:include/> tag, which gets evaluated at run time.

Did you try this:

    <body>
        <jsp:include page="${param.table}"/>
    </body>

Updated:

include directive is static ie,compile time where as el is evaluated at runtime. So use dynamic include ie, to evaluate el expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM