JSP - 错误java.lang.String没有属性

时间:2015-01-14 16:17:22

标签: java jsp jstl

我仍然收到此错误:

  

org.apache.jasper.JasperException:javax.el.PropertyNotFoundException:类'java.lang.String'没有属性'motoId'。

但我已经创造了制定者和吸气剂。

这是我简单的jsp代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <c:set var="list" value=" <%=eng.selectFromDb.appLayer.MotoBO.getAll() %> " />

        <table>
            <tr>
                <th>ID</th>
                <th>Značka</th>
                <th>Model</th>
                <th>Kategoria</th>
                <th>Objem</th>
                <th>Maximalna rychlost</th>
            </tr>

            <c:forEach var= "moto" items=" ${list} " >                        
                <tr>
                    <td>${moto.motoId}</td>
                     <td>${moto.znacka}</td>
                     <td>${moto.Model}</td>
                     <td>${moto.Kategoria}</td>
                     <td>${moto.Objem}</td>
                     <td>${moto.Max_rychlost}</td>
                </tr> 
            </c:forEach>
        </table>
    </body>
</html>

带有setter和getter的课程在这里:

public class MotorBykes {
static final int  EMPTY_ID = -1;

private int motoId = EMPTY_ID;
private String znacka;
private String model;
private String kategoria;
private int objem;
private int max_rychlost;

public MotorBykes(String znacka, String model, String kategoria, int objem, int max_rychlost) {
    this.znacka = znacka;
    this.model = model;
    this.kategoria = kategoria;
    this.objem = objem;
    this.max_rychlost = max_rychlost;
}

public MotorBykes(int motoId, String znacka, String model, String kategoria, int objem, int max_rychlost) {
    this.motoId = motoId;
    this.znacka = znacka;
    this.model = model;
    this.kategoria = kategoria;
    this.objem = objem;
    this.max_rychlost = max_rychlost;
}

public int getMotoId() {
    return motoId;
}


public String getZnacka() {
    return znacka;
}

public void setZnacka(String znacka) {
    this.znacka = znacka;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getKategoria() {
    return kategoria;
}

public void setKategoria(String kategoria) {
    this.kategoria = kategoria;
}

public int getObjem() {
    return objem;
}

public void setObjem(int objem) {
    this.objem = objem;
}

public int getMax_rychlost() {
    return max_rychlost;
}

public void setMax_rychlost(int max_rychlost) {
    this.max_rychlost = max_rychlost;
}

@Override
public String toString() {
    return "MotorBykes{" + "motoId=" + motoId + ", znacka=" + znacka + ", model=" + model + ", kategoria=" + kategoria + ", objem=" + objem + ", max_rychlost=" + max_rychlost + '}';
}

}

dao.getAll()的后面是:

 ensureOpenConnection();

        List<MotorBykes> ret = new ArrayList<>();

        String textSql = "SELECT * FROM motobykes";


        try {
            PreparedStatement ps = connection.prepareCall(textSql);

            ResultSet rs = ps.executeQuery();

            while(rs.next()){
                int motoId = rs.getInt(1);
                String znacka = rs.getString(2);
                String model = rs.getString(3);
                String kategoria = rs.getString(4);
                int objem = rs.getInt(5);
                int max_rychlost = rs.getInt(6);

                MotorBykes moto = new MotorBykes(motoId, znacka, model, kategoria, objem, max_rychlost);
                ret.add(moto);

            }

            rs.close();
            ps.close();

        } catch (SQLException ex) {
            processException("Failed to get all motos", ex);
        }

        return ret;

我知道问题与jstl有关,但我不知道在哪里。感谢帮助。

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

<c:set var="list" value=" <%=eng.selectFromDb.appLayer.MotoBO.getAll() %> " />

您不能在JSTL属性中使用scriptlet表达式。你根本不应该使用scriptlet表达式。访问数据库应该使用Java从控制器完成。结果(模型)应存储在视图的请求属性中以访问和显示它。这是MVC的基本原则。

将以下代码放在控制器中(例如,servlet):

List<MotorBykes> list = MotoBO.getAll();
request.setAttribute("list", list);

从JSP视图中删除<c:set>