繁体   English   中英

错误:“ items”在c:forEach jsp中不支持运行时表达式

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

这是我的Servlet

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

这是我的register.jsp页面

    <%@ 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>

这是我的Entity

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

}

我想将列表EMPLOYEES从servlet传递到jsp页面,而abd则想显示每个<c:forEach> Employee对象,但是它总是说异常。 所以请任何人回答我。

还告诉我如果我使用tomcat 8.5我要下载哪个jstl标准jar,我有jstl-jar 1.2,但我不认为它有效,因为无论我怎么做,此错误都会不断出现。

尝试使用

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

代替

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

在你的jsp代码中

似乎您在这里做错了

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

根据您的代码,您将获得item中的对象列表。 您如何打印包含对象的列表? 尝试使用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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