我在使用操作类用 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/