【问题标题】:Getting object related to the selected value in a struts s:select dropdown在struts中获取与所选值相关的对象:选择下拉菜单
【发布时间】:2016-05-11 11:52:33
【问题描述】:

我在 JSP 中有这个下拉菜单:

<s:select name = "destination"
         label = "the destination" 
          list = "drop" 
     listValue = "nameDest" 
     headerKey = "0" 
   headerValue = "chose a destination" />

动作类中有destination对象:

private Destination destination;
//getters and setters

但是当我提交时,我得到了这个错误:

没有为动作com.iticsys.GBO.actions.UserAction和结果input定义结果

当我删除下拉菜单时,一切正常。所以我认为 Struts 是在尝试将来自nameDest 的选定值的值(一个字符串)放入动作类中的destination 对象中。

那么我怎样才能得到选中的对象呢?

更新:

destination 是一个从 Destination 类实例化的对象:

@Entity
public class Destination {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idDest;
private String nameDest;

drop 是一个目的地列表:

private List<Destination> drop;
public List<Destination> getDrop() {
    return drop;
}
public void setDrop(List<Destination> drop) {
    this.drop = drop;
}

在下拉菜单上进行一些修改(由 Andrea Ligios 建议):

        <tr>
            <td>
                <s:select 
                name="destination.idDest"
                label="the destination" 
                list="drop" 
                listKey="idDest"
                listValue="nameDest" 
                headerKey="0" 
                headerValue="Chose a destination" />
           </td>
        </tr>

我收到了这个错误

1.tag 'select', field 'list', name 'destination.idDest':请求的列表键 'drop' 无法解析为集合/数组/映射/枚举/迭代器类型。示例:人或人。{name}

【问题讨论】:

  • Destination 对象是什么?

标签: jsp drop-down-menu struts2


【解决方案1】:

是的,您将 String 发送到 Destination 变量,类型不匹配,因此拦截器堆栈会引发错误并从您的操作方法的正常执行中更改工作流程调用到为您的操作定义的INPUT 结果。

由于您没有定义任何 INPUT 结果,它会引发您看到的错误消息。

阅读how the INPUT result works(该模式在转换验证中都很常见)。

那么你需要

  1. 定义输入结果
  2. 在您的选择中指定listKey
  3. 在您的选择中指定name 属性,包括键。

例如,如果 Destination 有 idnameDest 字段,则需要设置:

<s:select name = "destination.id"
         label = "the destination" 
          list = "drop" 
       listKey = "id" 
     listValue = "nameDest" 
     headerKey = "0" 
   headerValue = "chose a destination" />

【讨论】:

  • 我做了你建议的修改,现在我得到了这个错误,1.tag 'select', field 'list', name 'destination.idDest': The requested list key 'drop' could not be解析为集合/数组/映射/枚举/迭代器类型。示例:人或人。{name}。并且我想添加下拉列表是一个列表
  • 您必须更改其他内容,因为您之前有drop,它应该在那时给您此错误消息。随意发布您的源列表定义和 getter,以及 pojo 内容以获得更多帮助
  • 一切都很好。请同时添加新的选择代码,根据我的回答改编...问题很可能在那里
  • 你在使用 ModelDriven 吗?
  • 我重启了eclipse,现在可以了,不知道为什么。
猜你喜欢
  • 2023-03-05
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 2015-01-31
  • 2018-11-20
  • 1970-01-01
  • 2021-03-04
  • 2017-07-16
相关资源
最近更新 更多