我需要在运行时在 JSP 中包含 html 或图像。我将在运行时了解 jsp 文件名。所以我无法对 JSP 包含进行更改。
我该怎么做?
最佳答案
我不确定你所说的运行时是什么意思?我做过类似的事情。在我做的 Controller 中。 model.addAttribute("jspContent", "test.jsp")
然后在包含的jsp文件中:
<jsp:include page="${jspContent}" ></jsp:include>
编辑:
阅读您的评论。我想这取决于您使用的其他技术。您可以将名称添加到 session 中,然后在接收重定向的 Controller 中读取它。将其添加到模型并从 session 中清除。或者如果你碰巧使用 Spring 我昨天才知道这个:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes
编辑2: 我的意思是这样的,我不完全确定它是否合适,但这是一个想法 :) 在重定向的 Controller 中,您可以执行以下操作:
session.setAttribute("jspContentFromRedirect", "test.jsp");
servletResponse.sendRedirect(urlToRedirectTo);
然后在接收 Controller 中:
String jspContent = session.getAttribute("jspContentFromRedirect");
if(jspContent != null){
model.addAttribute("jspContent", jspContent);
session.setAttribute("jspContentFromRedirect", null);
}
像这样
关于html - 如何在运行时在 JSP 中包含 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9020883/