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

蹲街弑〆低调 提交于 2020-06-16 07:43:24

问题


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.


回答1:


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.




回答2:


Did you try this:

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

Updated:

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



来源:https://stackoverflow.com/questions/23835653/is-it-possible-to-include-an-expression-tag-inside-a-page-directive-in-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!