Home > Software engineering >  how to iterate a list without using JSTL in JSP?
how to iterate a list without using JSTL in JSP?

Time:12-21

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?

CodePudding user response:

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";
    }
  • Related