簡體   English   中英

如何在 JSP 中不使用 JSTL 來迭代列表?

[英]how to iterate a list without using JSTL in JSP?

當我嘗試在 spring boot 中不使用 JSTL 的情況下從控制器迭代列表時,我遇到以下錯誤。

Whitelabel Error Page 此應用程序沒有針對 /error 的顯式映射,因此您將其視為后備。 12 月 20 日星期二 16:03:05 IST 2022 出現意外錯誤(類型=內部服務器錯誤,狀態=500)。 在范圍內找不到 bean allShoes java.lang.InstantiationException:在范圍內找不到 bean allShoes

我的 JSP 代碼是:

    <jsp:useBean id="allShoes" type="java.util.List" scope="request"></jsp:useBean>
    
<%
        for(Iterator it = allShoes.iterator(); it.hasNext();){
            ShoeData shoe = (ShoeData) it.next();
    %>
    
    <%= shoe.getId() %>
    <%= shoe.getColor() %>
    
    <%} %> 

我的控制器代碼是:

    @RequestMapping(value="/getallitem", method=RequestMethod.GET)
    public String getAllItem() {
        List<ShoeData> allShoes = resource.getAllShoes();
        return "warehouse";
    }

我如何在不使用 JSTL 的情況下解決這個問題?

這不是 JSTL 問題,您的錯誤消息是bean allShoes not found within scope

您的控制器只是創建一個對象allShoes ,但不會將其傳遞到任何地方或將其存儲在某個地方。 您的 jsp 無法獲取 bean allShoes

試試這個,我測試正常,在 Spring Boot 2.x 中我使用List<ShoeData> allShoes=new ArrayList<ShoeData>(); allShoes.add(new ShoeData("101","Red")); 用於臨時測試數據。

@RequestMapping(value="/getallitem", method=RequestMethod.GET)
    public String getAllItem(Map<String, Object> model) {
        //List<ShoeData> allShoes = resource.getAllShoes();
        List<ShoeData> allShoes=new ArrayList<ShoeData>();
        allShoes.add(new ShoeData("101","Red"));
        allShoes.add(new ShoeData("102","Blue"));
        allShoes.add(new ShoeData("103","Black"));
        model.put("allShoes", allShoes);
        return "warehouse";
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM