将Scriptlet数组转换为JSTL数组

时间:2012-08-19 23:47:54

标签: java jsp

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

<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>

scriplet工作正常,我只想达到标准并使用JSTL

1 个答案:

答案 0 :(得分: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>