Spring Boot在jsp中显示数据不起作用[关闭]

Posted

技术标签:

【中文标题】Spring Boot在jsp中显示数据不起作用[关闭]【英文标题】:spring boot displaying data in jsp not working [closed] 【发布时间】:2022-01-12 02:01:58 【问题描述】:

我们正在做一个学校作业,我们创建了一个在线汽车商店,我们想要显示数据库中的所有汽车,但我们收到以下错误

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]]

显示List内容的jsp文件

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

从数据库中获取数据并将其提供给模型的控制器

/*
 * 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";
    
    
    
    

控制器的服务

/*
 * 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;
    
    */
    
    
    



```

【问题讨论】:

您在 for each 中将变量映射到 order,然后在代码中再次使用 orders,应该是 order 【参考方案1】:

您试图在 JSP 中引用错误的变量。

这是汽车包裹而不是包裹。所以尝试使用这个变量。

value="$orders.carpackage"

以上内容将解决您的问题,但除此之外,我会要求您遵循正确的命名约定。类名应该是驼峰式,foreach 项目应该是复数,每个项目应该是单数等

【讨论】:

以上是关于Spring Boot在jsp中显示数据不起作用[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 错误消息不起作用

sec:authentication 在 Spring Boot Security 中不起作用

spring boot starter 安全发布方法不起作用

Spring boot参数更新模型数据库不起作用

基于 Spring Security Java 的配置不起作用。它会一直显示 index.jsp 页面

基于 Spring mvc Java 的配置不起作用。控制台显示没有错误但我的jsp页面没有显示