简体   繁体   中英

error:“items” does not support runtime expressions in c:forEach jsp

This is my Servlet class

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Register")
public class Register extends HttpServlet {

    static int id=0;

    private static final long serialVersionUID = 1L;

    private static final ArrayList<Employee> EMPLOYEES = new ArrayList<>();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        id++;
        int uage=0;
        long mob=0;
        String uexit = request.getParameter("uexit");
        if(uexit.equals("Exit")) {
            System.out.println("\n\n\n\n\n\n\n\n");
            request.getRequestDispatcher("exit.jsp").forward(request, response);
        }
        else {
            String uname = request.getParameter("uname");
            uage = Integer.parseInt(request.getParameter("uage"));
            mob = Long.parseLong(request.getParameter("umob"));

            if(uname!=null && uage!=0 && mob!=0) {
                Employee employee = new Employee(id, uname, uage, mob);
                EMPLOYEES.add(employee);
                request.getSession().setAttribute("employees" , EMPLOYEES);
                RequestDispatcher rd = getServletContext().getRequestDispatcher("register.jsp");
                rd.forward(request, response);
            }
            else {
                request.getSession().setAttribute("employees", null);
                request.getRequestDispatcher("register.jsp").forward(request, response);
            }
        }
        for(int i=0; i<EMPLOYEES.size(); i++) {
            System.out.print(EMPLOYEES.get(i).id + "\t");
            System.out.print(EMPLOYEES.get(i).name + "\t");
            System.out.print(EMPLOYEES.get(i).age + "\t");
            System.out.print(EMPLOYEES.get(i).mob + "\t");
            System.out.println();
        }
    }

This is my register.jsp page

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
    <%@page import="java.util.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>MyProject</title>
    </head>
    <h2 align="center">
        <font color="#BF360C" size="10" face="Times New Roman"><b><i>
                    Register</i></b></font>
    </h2>
    <body style="background-color: #AED6F1">
        <form action="Register" method="POST">
            <table align="center" cellpadding="20" width="600"
                style="background-color: #33C3DD;">
                <tr>
                    <th><font size="5" face="Times New Roman"><b><i>Name:</i></b></font></th>
                    <td colspan="2" align="center"><input type="text" name="uname"></td>
                </tr>

                <tr>
                    <th><font size="5" face="Times New Roman"><b><i>Age:</i></b></font></th>
                    <td colspan="2" align="center"><input type="text" name="uage"></td>
                </tr>

                <tr>
                    <th><font size="5" face="Times New Roman"><b><i>Mobile
                                    Number:</i></b></font></th>
                    <td colspan="2" align="center"><input type="text" name="umob"></td>
                </tr>

                <tr>
                    <th colspan="3" align="center"><input type="submit"
                        name="uexit" value="Register"><br /> <br /> <input
                        type="submit" name="uexit" value="Exit"></th>
                </tr>

            </table>
        </form>
        <br>
        <br>
        <table cellpadding="20" cellspacing="5" width="900"
            style="background-color: #FDFEFE;" align="center">

            <tr style="background-color: #FDFDFD">
                <th style="background-color: #4468D1"><font size="5"
                    face="Times New Roman"><b><i>Sr. No.</i></b></font></th>
                <th style="background-color: #4468D1"><font size="5"
                    face="Times New Roman"><b><i>Name</i></b></font></th>
                <th style="background-color: #4468D1"><font size="5"
                    face="Times New Roman"><b><i>Age</i></b></font></th>
                <th style="background-color: #4468D1"><font size="5"
                    face="Times New Roman"><b><i>Mobile Number:</i></b></font></th>
                <th style="background-color: #4468D1"><font size="5"
                    face="Times New Roman"><b><i>Edit</i></b></font></th>
            </tr>
                <c:forEach items="${employees}" var="item">
                <tr>
<td style="background-color: #4468D1"><c:out value = "${item.id}"/></td>
                <td style="background-color: #4468D1"><c:out value = "${item.name}"/></td>
                <td style="background-color: #4468D1"><c:out value = "${item.age}"/></td>
                <td style="background-color: #4468D1"><c:out value = "${item.mob}"/></td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>

This is my Entity class

public class Employee {
    public int id;
    public  String name;
    public  int age;
    public  long mob;

    public Employee() {}

    public Employee(int id, String name, int age, long mob) {
        this.name= name;
        this.age= age;
        this.mob= mob;
        this.id=id;
    }

    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name=name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age=age;
    }
    public long getMob() {
        return mob;
    }
    public void setMob(int mob) {
        this.mob = mob;
    }

}

I want to pass list EMPLOYEES from the servlet to jsp page abd want to show each Employee object with <c:forEach> but it keeps on saying exception. so please anyone answer me.

Also tell me which jstl standard jar to download if im using tomcat 8.5 i have jstl-jar 1.2 but i dont think it's working because this error is keeps on coming no matter how i do it.

Try to use

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

instead of

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>

in your jsp code

Seems you are doing something wrong here

<tr>
    <td>${item}</td>
</tr>

According to your code you are getting list of object in item . How you are printing a list which containing object ? Try using

<tr>
     <td>${item.id}</td>
     <td>${item.name}</td>
     <td>${item.age}</td>
     <td>${item.mob}</td>
</tr>

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