我试图通过使用通过另一个jsp传递给jsp的参数将jsp文件包含到另一个文件中。
代码是
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/>
但上面的代码不起作用。如果我硬编码值
<%@ include file="fileName" %>
那就行了。
答案 0 :(得分:3)
<%@ include file="filename" %>
在JSP页面转换时间中,include指令中给出的文件内容按原样粘贴,在JSP include指令的位置用来。然后将源JSP页面转换为java servlet类。包含的文件可以是静态资源或JSP页面。通常,JSP include伪指令用于包含页眉横幅和页脚。
JSP编译过程就是,只有在页面发生变化时才会编译源JSP页面。如果包含的JSP文件发生更改,则不会编译源JSP文件,因此修改不会反映在输出中。
<jsp:include page="relativeURL"/>
jsp:include动作元素就像一个函数调用。在运行时,包含的文件将执行,结果内容将包含在源JSP页面中。调用包含的JSP页面时,请求和响应对象都将作为参数传递。
如果需要传递其他参数,则可以使用 jsp:param 元素。如果资源是静态的,则将其内容插入到调用JSP文件中,因为不需要处理。
使用include指令无法做到这一点。在页面上的Java代码执行之前很久就构造了JSP的servlet时,将对该指令进行评估。
您可以使用带有<jsp:include/>
标记的变量路径,该变量在运行时进行评估。
答案 1 :(得分:1)
你试过这个:
<body>
<jsp:include page="${param.table}"/>
</body>
<强>更新强>
include指令是静态的,即编译时在运行时评估el的时间。因此,使用动态包括,即评估el表达。