0

您好我正在尝试将这段代码更改为 JSTL。我在这里使用脚本。

<div id="slider">
<form action="gamepage" method="post">
<div class="sliderclass" id="slider1">
     <% SiteDatabase database = new SiteDatabase();
    Game game =(Game) database.getGames();%>
    <% String[] newgames = game.getNewGames();
       String[] imgSrc = {"images/spidermancover.jpg","images/blackopscover.jpg", "images/laststorycover.jpg", "images/ncaa13cover.jpg"
               ,"images/uncharted3cover.jpg", "images/mariokart7cover.jpg"};%>
   <%for(int i =0; i < 6; i++) {%>
    <div class="contentwrapper">
    <button type="submit" style="border: 0; background: transparent" name="game" VALUE="<%=i%>"><img src="<%=imgSrc[i]%>" name="what"/></button><div id="slidertext"><%= newgames[i] %></div></div>
    <%}%>
</div>
</form>
</div>

像这样

<div class="slider">
<div id="slider1" class="sliderclass">
    <jsp:useBean id="games" class="services.SiteDatabase" scope="request">
        <jsp:SetProperty property="gameTitle" name="games" value="gameTitle"/>
    </jsp:useBean>


    <jsp:useBean id="game" class="services.Game" scope="request">
    </jsp:useBean>

    <c:set var="gameTitle" scope="request" value="game"/>
    <c:set var="gameSrc" scope="request"><jsp:getProperty property="gameSrc" name="game"/></c:set>
    <c:set var="gameId" scope="request"><jsp:getProperty property="gameId" name="game"/></c:set>

    <c:forEach var="i" begin="0" end="6">
        <div id="contentwrapper">
            <a href="game.jsp?"><img src="" name="what" /></a>
            <div id="slidertext"><jsp:getProperty property="gameTitle" name="gameTitle" /></div>
        </div>  
    </c:forEach>
 </div>
</div>

脚本工作正常,我只想满足标准并使用 JSTL

4

1 回答 1

0

您可以通过 JSTL 迭代数组/列表<c:forEach/>

<c:forEach var="img" items="images/spidermancover.jpg","images/blackopscover.jpg",
                           "images/laststorycover.jpg", "images/ncaa13cover.jpg",
                           "images/uncharted3cover.jpg","images/mariokart7cover.jpg">

 <p>
  ${img}
 </p>
</c:forEach>

或者

<c:forEach var="game" items="${games.newGames}">
  <p>
   ${game}
  </p>
</c:forEach>
于 2012-08-20T03:07:05.543 回答