Home > Back-end >  spring boot displaying data in jsp not working
spring boot displaying data in jsp not working

Time:12-07

we are working on a school assigment which and we make an online carshop and we want to display all the cars in the database but we get the following error

There was an unexpected error (type=Internal Server Error, status=500).
/views/order.jsp (line: [74], column: [12]) [${orders.package}] contains invalid expression(s): [javax.el.ELException: Failed to parse the expression [${orders.package}]]
org.apache.jasper.JasperException: /views/order.jsp (line: [74], column: [12]) [${orders.package}] contains invalid expression(s): [javax.el.ELException: Failed to parse the expression [${orders.package}]]

the jsp file that displayst the content of the List

<tabel>
    <tr>
        <th>price</th>
        <th>package</th>
        <th>category</th>
    </tr>
        <c:forEach items="${order}" var="orders">
    <tr>
        <td><c:out value="${orders.price}"/></td>
        <td><c:out value="${orders.package}"/></td>
        <td><c:out value="${orders.category}"/></td>
    </tr>
    </c:forEach>
</table>

the controller that fetches the data from the database and give it to a model

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */

package com.autoszalon.auto.application.controller;

import com.autoszalon.auto.application.domains.User;
import com.autoszalon.auto.application.service.vehicleservice;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;



/**
 *
 * @author Nagy
 */


@Controller
public class vehiclecontroller {
    
    @Autowired
    
    private vehicleservice vehicleservice;
    
   @GetMapping("order")
    
    public String listVehicle(Model jarmumodel){
        
        List<vehiclefinddto> carlist = new ArrayList<>();
        
        vehicleservice.findAllVeichle().forEach(h 
                -> carlist.add(new vehiclefinddto(h.getPrice(), h.getCarpackage(), h.getCategory()))
        );
      jarmumodel.addAttribute("order", carlist);
     
      return "order";
      }
    @PostMapping("orderfilter")
    public String vehichle(vehichledtotemp vehichledto ){
        vehicleservice.transfertovehichlefinddto(vehichledto);
        
    return "order";
    }
    
    }
    

the service of the controller

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.autoszalon.auto.application.service;

import com.autoszalon.auto.application.controller.vehichledtotemp;
import com.autoszalon.auto.application.controller.vehiclefinddto;
import com.autoszalon.auto.application.domains.Carpackage;
import com.autoszalon.auto.application.domains.Veichle;
import com.autoszalon.auto.application.repositorys.Veichlerepository;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Nagy
 */
@Service
public class vehicleservice {
    @Autowired
    public Veichlerepository vehiclerepo;
    
        public Iterable<Veichle> findAllVeichle() {
        return vehiclerepo.findAll();
    }
        public Iterable<Veichle> findAllBycarpackage(Carpackage carpackage){
            return (Iterable<Veichle>) vehiclerepo.findAllBycarpackage(carpackage);
        }
        public vehiclefinddto transfertovehichlefinddto(vehichledtotemp vehdtotemp){
            vehiclefinddto vfinddto = new vehiclefinddto(vehdtotemp.getDoors(),vehdtotemp.getCarpackage(), vehdtotemp.getCategory(), vehdtotemp.getColor(), vehdtotemp.isAllwheels(), (int) vehdtotemp.getPrice());
            return vfinddto;
        }
        public Iterable<Veichle> findAlLbycolor(String color) {
            return vehiclerepo.findcarsbycolor(color);
        }
      
            public Iterable<Veichle> findAlLbyall(boolean Allwheel, float price,int doors, String color) {
        return vehiclerepo.findcarsbyAll(Allwheel, price, doors, color);
    }
            
        }


```
this is the data transfer object 
```

/**
 *
 * @author Nagy
 */
public class vehiclefinddto {
    private final float price;
    private final Carpackage carpackage;
   // private final String color;
  //  private final int doors;
    private final Categoryenum category;
   //private final boolean Allwheels;

    public vehiclefinddto(float price, Carpackage carpackage, Categoryenum category) {
        this.price = price;
        this.carpackage = carpackage;
        this.category = category;
    }

    public vehiclefinddto(int doors, Carpackage carpackage, Categoryenum category, String color, boolean allwheels, int i) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public float getPrice() {
        return price;
    }

    public Carpackage getCarpackage() {
        return carpackage;
    }
/*
    public String getColor() {
        return color;
    }

    public int getDoors() {
        return doors;
    }
*/
    public Categoryenum getCategory() {
        return category;
    }
/*
    public boolean isAllwheels() {
        return Allwheels;
    }
    */
    
    
    
}


```

CodePudding user response:

You are trying to refer wrong variable in JSP.

It's carpackage not package. So try to use this variable.

value="${orders.carpackage}"

The above things will fix your issue but apart from this I would request you to follow proper naming conventions. Class names should be camel case, foreach items should be plural and each item should be singular etc

  • Related