java parameter in jsp:include

℡╲_俬逩灬. 提交于 2019-12-12 02:02:58

问题


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

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