我需要在运行时在JSP中包含html或图像。我将在运行时了解jsp文件名。所以我无法对JSP include进行更改。
我该怎么做?

最佳答案

我不知道你说的运行时是什么意思?我也做过类似的事情。在控制器里我有。model.addAttribute("jspContent", "test.jsp")
然后在包含的jsp文件中:
<jsp:include page="${jspContent}" ></jsp:include>
编辑:
阅读你的评论。我想这取决于你使用的其他技术。您可以将要包含的名称添加到会话中,然后在接收重定向的控制器中读取该名称。将其添加到模型并从会话中清除。或者,如果你碰巧使用Spring,我昨天才知道:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes
编辑2:
我的意思是这样的,我不确定它是否合适,但这是一个想法:)
在正在重定向的控制器中,执行以下操作:

session.setAttribute("jspContentFromRedirect", "test.jsp");
servletResponse.sendRedirect(urlToRedirectTo);

然后在接收控制器中:
String jspContent = session.getAttribute("jspContentFromRedirect");
if(jspContent != null){
    model.addAttribute("jspContent", jspContent);
    session.setAttribute("jspContentFromRedirect", null);
    }

像这样的东西

07-27 18:13