问题
I wanted to know if I can pass a java variable to a . I tried to do something like this inside a jsp file:
<% String folder = request.getParameter("something"); %>
<jsp:include page="../<%=folder%>/myframe.jsp" ></jsp:include>
This does not work. I get an error which says 'Failed to find resource /=folder/myframe.jsp'. Can this be done?
Thanks for any help.
回答1:
You either need to have the page parameter as an entire string or as an entire substitution.
<jsp:include page="<%= myPageToInclude %>" />
<jsp:include page="path/to/my/page.jsp" />
Also when concatenating strings inside a value you need to escape the double quotes. So for your problem :
<% String folder = request.getParameter("something"); %>
<jsp:include page="<%= \"../\" + folder + \"/myframe.jsp\" %>" />
Should work fine, or for easier reading :
<%
String folder = request.getParameter("something");
String page = "../" + folder + "/myframe.jsp";
%>
<jsp:include page="<%= page %>" />
回答2:
Concidering your param name as "something", Try this:
<jsp:include page="../${param.something}/myframe.jsp" />
Other solution is to do that:
<% String folder = request.getParameter("something"); %>
<% pageContext.setAttribute("folder", folder); %>
<jsp:include page="../${folder}/myframe.jsp"></jsp:include>
First is better but both should works.
来源:https://stackoverflow.com/questions/7259250/java-parameter-in-jspinclude