Add values to arraylist use JSTL

前端 未结 3 672
慢半拍i
慢半拍i 2020-12-19 10:32

is it possible to add values to an ArrayList instead of using a HashMap

something like:



        
3条回答
  •  忘掉有多难
    2020-12-19 11:05

    JSTL is not designed to do this kind of stuff. This really belongs in the business logic which is (in)directly to be controlled by a servlet class.

    Create a servlet which does like:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        List animals = new ArrayList();
        animals.add("Sylvester");
        animals.add("Goofy");
        animals.add("Mickey");
        request.setAttribute("animals", animals);
        request.getRequestDispatcher("/WEB-INF/animals.jsp").forward(request, response);
    }
    

    Map this on an url-pattern of /animals.

    Now create a JSP file in /WEB-INF/animals.jsp (place it in WEB-INF to prevent direct access):

    
        ${animal}

    No need for jsp:useBean as servlet has already set it.

    Now call the servlet+JSP by http://example.com/context/animals.

提交回复
热议问题