java - 如何使用 Struts2 中 Action 类的数组填充 SelectBox?

标签 java html jsp drop-down-menu struts2

我在使用操作类用 struts 填充组合框时遇到了一些问题..

这是我的表单元素:

<s:select label = "Estado" 
           name = "estados" 
          value = "#estados"
      headerKey = "-1"  
    headerValue = "---Seleccione---" 
           list = "estados"/>

这是 Action 类:

public class PrepararMedicosAction extends ActionSupport 
                                implements Preparable, ParameterAware{

    public List <Estado> estados;

    private IFachada lookupFachadaRemote() {
        try {
           Context c = new InitialContext();
           return (IFachada) c.lookup("java:global/Fachada/Fachada!com.interfaces.IFachada");
        } catch (NamingException ne) {
           Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
           throw new RuntimeException(ne);
        }
    }

    public String display() {
        return NONE;
    }

    @Override
    public void prepare() throws Exception {
        this.estados= fachada.getEstados();
    }
}

最佳答案

Struts2的Select属性:

name: the source object (the List in your case) containing the data you want to show;

listKey: the field used as key in the option element

listvalue: the field used as description in the option element, visibile on the combobox.

value: the referred element to preset the value.

如果name指的是 List<String> ,您可以使用list属性来显示键和值中的字符串;相反,就像您的情况一样, name指的是 List<Object> ,您必须指定哪个属性是,哪个是

示例(让我们将“Estados”翻译为“States”):

public class State {
    private Long id;
    private String stateName;

    // Getters and Setters here...
}

然后公开 List<State> states通过getStates()方法,

通过 getMyPreferredState() 以及您的首选状态方法。

在jsp中您将编写:

<s:select name = "states" 
         value = "myPreferredState" 
       listKey = "id" 
     listValue = "stateName" 
     headerKey = "-1" 
   headerValue = "Please choose a state..." 
/>

关于java - 如何使用 Struts2 中 Action 类的数组填充 SelectBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12829053/

相关文章:

java - 定义新类实例时重复命名的类有什么意义?

java - JUnit中测试多线程与Java中Main函数的区别

javascript - 如何在数组上整体使用 preg_replace

java - JSP 中的换行符

java - 逻辑迭代排序选项

java - Java 如何知道要使用接口(interface)的哪个实现?

java - 从 Parse 对象获取失败

javascript - 如何将我的元素放在中间

java - 如何从Struts 2中动态生成的JSP页面获取数据?

java - java spring mvc 所需的 MultipartFile 参数 'file' 不存在