简体   繁体   中英

how to iterate a list without using JSTL in JSP?

I face an error below when I try to iterate a list from a controller without using JSTL in spring boot.

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Dec 20 16:03:05 IST 2022 There was an unexpected error (type=Internal Server Error, status=500). bean allShoes not found within scope java.lang.InstantiationException: bean allShoes not found within scope

my JSP code is:

    <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() %>
    
    <%} %> 

my Controller code is:

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

how can i solve this issue without using JSTL?

this is not JSTL problems, your error message is bean allShoes not found within scope

your controller just create a object allShoes , but does not pass it to anywhere or store it in somewhere. your jsp can not get bean allShoes .

try this, I am test ok, in Spring Boot 2.x I use List<ShoeData> allShoes=new ArrayList<ShoeData>(); and allShoes.add(new ShoeData("101","Red")); for temp test data.

@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";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM