【问题标题】:Removing Java code from JSP page从 JSP 页面中删除 Java 代码
【发布时间】:2015-05-28 09:14:38
【问题描述】:

我使用这个 jsp、servlet 和 struts。 (Jsp 版本 2.0,servlet 2.4,struts 1)。 另一个循环中有一个“for”循环。

...
<%
    String[] profileNames = { "Page", "Report", "XML API" };
    for (String profileName : profileNames) {

        LRUCache profile = ObjectStore.getStore(profileName + " Profile");
        pageContext.setAttribute("profile", profile);
        pageContext.setAttribute("profileName", profileName);
%>

<table class="sortable" id="<%=profileName%>">
    <tr class=title>
        <th class=contentTable>description</th>
        <th class=contentTable>qty</th>
    </tr>
    <c:forEach var="profile" items="${profileItems}">
        <tr>
            <td>${profile.object.description}</td>
            <td>${profile.object.qty}</td>
        </tr>
    </c:forEach>
</table>
<%
    }
%>

我需要这个没有 java 代码的 jsp 页面。如何实施?

感谢您的帮助!

【问题讨论】:

    标签: java jsp servlets


    【解决方案1】:

    在 Servlet(或您用于处理请求/响应的任何其他类似技术)上发送您当前在 pageContext 中“放入”的相同属性:profileprofileName

    类似于:Servlet 类上的request.setAttribute("key", value);

    更新:我在这里进行了快速搜索,您可能真的想看看这个one

    【讨论】:

    • 感谢您的帮助!我可以像这样在jsp页面(2.0版)中调用带有参数的java方法吗:ObjectStore.getStore(profileName + " Profile");
    • 当然,首先声明正确的&lt;%@ page import="path.to.YourClass" %&gt;(在你的情况下为ObjectStore)指令,你可以这样做,但我强烈建议你只使用数据你的 JSP,从长远来看更容易维护,尽管我也知道有时需求和/或时间有点疯狂。基本上,你做你必须做的事情,例如在Servlet,然后发送数据并渲染这些东西。这个link 真的很有用。祝你有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多