Ján Ambroz
Ján Ambroz

Reputation: 168

JSP - error java.lang.String does not have property

I still getting this error:

org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'motoId'.

But i have create setters and getters.

This is my simple jsp code:

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

And class with setter and getters is here:

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

}

And behind for dao.getAll() is :

 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;

I know that problem is with jstl, but i dont know where. Thanks for help.

Upvotes: 1

Views: 2271

Answers (1)

JB Nizet
JB Nizet

Reputation: 692231

The problem is with this line:

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

You may NOT use scriptlet expressions inside JSTL attributes. You should never use scriptlet expressions at all. Accessing the DB should be done in Java, from the controller. And the result (the model) should be stored in request attributes for the view to access and display it. This is the basic principle of MVC.

Put the following code in your controller (a servlet, for example):

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

The remove the <c:set> from your JSP view.

Upvotes: 1

Related Questions