从jsp inlude

时间:2015-11-18 19:30:47

标签: java jsp foreach jspinclude

我有以下jsp

           ...  
           <table id="table" class="sortable">
                        <thead>
                        <tr>
                            <td class="head">Дата Запроса</td>
                            <td class="head">Сумма Запроса</td>
                            <td class="head">Баланс Владельца</td>
                            <td class="head">Статус Запроса</td>
                            <td class="head">Email</td>
                            <td class="head">-----</td>
                        </tr>
                        </thead>

                        <tbody>
                        <c:forEach var="event" items="${events}">
                            <tr>
                                <td>
                                    <jsp:useBean id="dateValue" class="java.util.Date"/>
                                    <jsp:setProperty name="dateValue" property="time"
                                                     value="${event.timestamp}"/>
                                    <fmt:formatDate value="${dateValue}"
                                                    pattern="MM/dd/yyyy HH:mm"/>
                                </td>
                                <td>${event.amount}</td>
                                <td>${event.sourceUser.userAccounts.iterator().next().currentAmount}</td>
                                <td>${event.status}</td>
                                <td>${event.sourceUser.email}</td>
                                <td><c:if test="${event.status != 'SUCCESS'}">
                                    <input type="button" class="withDraw"
                                           value="подтвердить вывод"/>
                                </c:if>

                                    <div class="edit-holder  adt" id="">

                                        <div class="edit-box">
                                            <div class="title">Подтверждение вывода средств владельцем</div>
                                            <form action="/admin/confirmWithdrawRequest"
                                                  method="post">
                                                <div>
                                                    <input type="hidden" value="${event.id}" name="eventId"/>
                                                    <label>Статус</label>
                                                    <select name="status">
                                                        <c:forEach items="${statuses}" var="status">
                                                            <option <c:if test="${status == event.status}">
                                                                        selected="selected"
                                                                    </c:if>
                                                                    >${status}</option>
                                                        </c:forEach>
                                                    </select>
                                                    <div class="clear"/>
                                                </div>
                                                <div>
                                                    <label>Email</label>
                                                    <input type="text" class="with-draw-input" name="email"
                                                           value="${event.sourceUser.email}"
                                                           readonly/>
                                                    <div class="clear"/>
                                                </div>
                                                <div>
                                                    <label>Текущий баланс</label>
                                                    <input type="text" class="with-draw-input" name="balance"
                                                           value="${event.sourceUser.userAccounts.iterator().next().currentAmount}"
                                                           readonly/>
                                                    <div class="clear"/>
                                                </div>
                                                <div>
                                                    <label>Сумма запроса</label>
                                                    <input type="text" class="with-draw-input" name="email"
                                                           value="${event.amount}"
                                                           readonly/>
                                                    <div class="clear"/>
                                                </div>
                                                <div>
                                                    <label>Комментарий модератора</label>
                                                    <textarea class="moderationComment"
                                                              name="moderationComment">${event.moderationComment}</textarea>
                                                    <div class="clear"/>
                                                </div>
                                                <input type="submit" class="btn" value="сохранить"/>

                                                <div class="clear"></div>
                                            </form>
                                            <div class="close"></div>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    ...

现在编译好了,我看到了预期的结果: enter image description here

我想将最后的td内容提取到分离的jsp中,如下所示:

<tbody>
                            <c:forEach var="event" items="${events}">
                                <tr>
                                    <td>
                                        <jsp:useBean id="dateValue" class="java.util.Date"/>
                                        <jsp:setProperty name="dateValue" property="time"
                                                         value="${event.timestamp}"/>
                                        <fmt:formatDate value="${dateValue}"
                                                        pattern="dd.MM.yyyy HH:mm:ss"/>
                                    </td>
                                    <td>${event.amount}</td>
                                    <td>${event.sourceUser.userAccounts.iterator().next().currentAmount}</td>
                                    <td>${event.status}</td>
                                    <td>${event.sourceUser.email}</td>
                                    <td><jsp:include page="confirmWithDrawModal.jsp"/>
                                    </td>
                                </tr>
                            </c:forEach>
                            </tbody>

confirmWithDrawModal.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<c:if test="${event.status != 'SUCCESS'}">
  <input type="button" class="withDraw"
         value="подтвердить вывод"/>
</c:if>

<div class="edit-holder  adt" id="">

  <div class="edit-box">
    <div class="title">Подтверждение вывода средств владельцем</div>
    <form action="/admin/confirmWithdrawRequest"
          method="post">
      <div>
        <input type="hidden" value="${event.id}" name="eventId"/>
        <label>Статус</label>
        <select name="status">
          <c:forEach items="${statuses}" var="status">
            <option <c:if test="${status == event.status}">
              selected="selected"
            </c:if>
                    >${status}</option>
          </c:forEach>
        </select>
        <div class="clear"/>
      </div>
      <div>
        <label>Email</label>
        <input type="text" class="with-draw-input" name="email"
               value="${event.sourceUser.email}"
               readonly/>
        <div class="clear"/>
      </div>
      <div>
        <label>Текущий баланс</label>
        <input type="text" class="with-draw-input" name="balance"
               value="${event.sourceUser.userAccounts.iterator().next().currentAmount}"
               readonly/>
        <div class="clear"/>
      </div>
      <div>
        <label>Сумма запроса</label>
        <input type="text" class="with-draw-input" name="email"
               value="${event.amount}"
               readonly/>
        <div class="clear"/>
      </div>
      <div>
        <label>Комментарий модератора</label>
                                                        <textarea class="moderationComment"
                                                                  name="moderationComment">${event.moderationComment}</textarea>
        <div class="clear"/>
      </div>
      <input type="submit" class="btn" value="сохранить"/>

      <div class="clear"></div>
    </form>
    <div class="close"></div>
  </div>
</div>

在重构之后,我看到以下结果:

enter image description here

正如您所见,变量未呈现。

如何解决?

2 个答案:

答案 0 :(得分:0)

我找到了2个解决方案:

  1. 使用<%@ include file="confirmWithDrawModal.jsp" %>
  2. 使用此建议https://stackoverflow.com/a/8003143/2674303
  3. 我不确定哪种方法更好

答案 1 :(得分:0)

.jsp文件被转换为.java文件,该文件经过编译和运行,提供最终发送到浏览器的输出。

当你在jsp 中包含另一个jsp 时,那些被翻译和编译的jsp取决于你使用的 include 的形式。

<jsp:include page="incpage.jsp">分别翻译并编译 mainpage.jsp incpage.jsp ,并将incpage.jsp的输出插入包含位置。考虑到这种情况,incpage.jsp是mainpage.jsp的独立,所以(当然)无法访问这些变量。

<%@include file="incpage.jsp">更像是 C #include,其中incpage.jsp的内容在翻译和编译之前被复制到mainpage.jsp 中地点。在这种情况下,如果inpage具有.jsp扩展名,则无关紧要。

如果您希望包含的文件作为主文件的一部分,就像您所做的那样,您将使用第二种形式的包含 - <%@include ...>