You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改getCarLocationDisplay方法返回类型为自定义对象的技术问询

问题描述

原本Order类中的getCarLocationDisplay()方法返回String类型,现在希望改为返回自定义的CarLocationDisplay对象,以便在JSP中通过${viewOrder.carLocationDisplay.id}或${viewOrder.carLocationDisplay.name}形式访问属性。

原有代码

Order类原方法

public String getCarLocationDisplay() {
    String result = "";
    if (carHandler != null) {
        result = carHandler.getAttribute(CarHandler.CAR_LOCATION);
    }
    return result;
}

JSP原有调用方式

<td>
      <a 
        href="cardetails.htm?id=<c:out value="${viewOrder.carLocationDisplay}"/>" 
        class = "urllink"><bean:write name="viewOrder"
        property="carLocationDisplay" /></a>
</td> 

自定义CarLocationDisplay类

public class CarLocationDisplay {
    private CarLocationManager carLocMgr;
    public void setCarLocationManager(CarLocationManager carLocationManager) {
        this.carLocMgr = carLocationManager;
    }
    
    public List<String> getLocationDetails(String id) {
        return carLocMgr.getCarLocationByQuery("id="+id);
    }
}         

自行修改尝试

public CarLocationDisplay getCarLocationDisplay() {
              
            CarLocationDisplay displayObj = null;              
            String result = "";
            if (carHandler != null) {
                result = carHandler.getAttribute(CarHandler.CAR_LOCATION);
                displayObj = new CarLocationDisplay();
                displayObj.getLocationDetails(result);            
            }
        return displayObj;
}

询问:这个实现方向是否正确,还需做哪些调整?


问题解答

你的修改方向是对的,但还有几个关键调整需要完成:

1. 重构CarLocationDisplay类的属性设计

当前CarLocationDisplay仅通过方法返回查询结果,无法直接通过EL表达式访问id、name属性。需要将这些字段设为实例属性,并通过getter方法暴露:

public class CarLocationDisplay {
    private String id;
    private String name;
    // 按需添加其他需要展示的字段
    private CarLocationManager carLocMgr;

    public void setCarLocationManager(CarLocationManager carLocationManager) {
        this.carLocMgr = carLocationManager;
    }

    // 根据ID加载详情并赋值到属性
    public void loadDetails(String locationId) {
        List<String> details = carLocMgr.getCarLocationByQuery("id="+locationId);
        // 假设查询结果的索引0是id,索引1是name,根据实际返回结构调整
        if (details != null && details.size() >=2) {
            this.id = details.get(0);
            this.name = details.get(1);
        }
    }

    // EL表达式依赖getter访问属性,必须提供
    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

2. 完善Order类的getCarLocationDisplay方法

  • 替换原有的getLocationDetails调用为loadDetails,确保查询结果存入对象属性
  • 处理CarLocationManager的注入:如果是容器管理的Bean,不要直接new对象,需通过依赖注入获取;如果手动创建,要确保设置carLocMgr避免空指针
  • 增加空值判断,避免无效查询

修改后的方法示例:

public CarLocationDisplay getCarLocationDisplay() {
    CarLocationDisplay displayObj = null;              
    if (carHandler != null) {
        String locationId = carHandler.getAttribute(CarHandler.CAR_LOCATION);
        if (locationId != null && !locationId.isEmpty()) {
            displayObj = new CarLocationDisplay();
            // 假设Order类已持有CarLocationManager实例,可根据实际场景调整注入方式
            displayObj.setCarLocationManager(this.carLocMgr);
            displayObj.loadDetails(locationId);
        }
    }
    return displayObj;
}

3. 调整JSP页面的调用逻辑

新增非空判断避免对象为null时的异常,同时改为访问对象的属性:

<td>
    <c:if test="${not empty viewOrder.carLocationDisplay}">
        <a href="cardetails.htm?id=<c:out value="${viewOrder.carLocationDisplay.id}"/>" 
           class="urllink">
            <c:out value="${viewOrder.carLocationDisplay.name}"/>
        </a>
    </c:if>
</td> 

4. 强化空值处理

  • 当carHandler为null、locationId为空时,方法返回null,JSP必须做非空判断
  • 在loadDetails中处理查询结果为空的情况,避免数组越界异常

内容的提问来源于stack exchange,提问作者Tan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:56:32