如果我第一次添加stackElement
就可以了,输出是:
list size = 1
但在添加第二个元素后,我得到错误:
错误渲染视图[/tab.xhtml]: javax.el.PropertyNotFoundException:/tab.xhtml @65,50 value="#{stackElement.name}":类. lang。字符串'没有'name'属性. ...引起的:javax.el.PropertyNotFoundException:类的java.lang。字符串"没有name属性
[io.undertow错误。(default task-4) UT005023: Exceptionjava.lang.IllegalStateException:CDATA标签不能嵌套…
为什么会发生?
页<h:form>
<p:commandButton update="stack" value="Use in expression" action="#{bean.tab}">
<f:param name="i" value="13" />
</p:commandButton>
<p:orderList id="stack" value="#{expression.list}" var="stackElement"
itemLabel="#{stackElement}" itemValue="#{stackElement}" controlsLocation="none" >
<!-- <p:ajax event="reorder" listener="#{expression.onReorder}" /> -->
<p:column>
<h:outputText value="#{stackElement.name}" />
</p:column>
</p:orderList>
</h:form>
憨豆@ManagedBean @RequestScoped
public class Bean implements Serializable {
@ManagedProperty(value="#{expression}")
private Expression ex;
public void tab() {
ex.addStackElement( new StackElement((int) System.currentTimeMillis(), "tab") );
}
// getters-setters
憨豆@ManagedBean @SessionScoped
public class Expression implements Serializable {
private List<StackElement> list = new ArrayList<StackElement>();
public void addStackElement(StackElement stackElement) {
list.add(stackElement);
System.out.println("list size = " + list.size());
}
// getters-setters
模型public class StackElement {
private int id;
private String name;
public StackElement(int id, String name) {
this.id = id;
this.name = name;
}
// getters-setters
转换器
@FacesConverter("myConverter")
public class MyConverter implements Converter {
@Override
public Object getAsObject(FacesContext fc, UIComponent component, String value) {
if(value != null && value.trim().length() > 0) {
try {
Expression service = (Expression) fc.getExternalContext().getApplicationMap().get("expression");
return service.getStackElementByName(value);
} catch(NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid element name."));
}
}
else {
return null;
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
if(object != null) {
return String.valueOf(((StackElement) object).getName());
}
else {
return null;
}
}
}
你的代码有几个问题。
- 你应该在你的
- 表达式bean属于sessionscope。但是你是从ApplicationScope中得到的。
<p:orderList>
组件上使用converter="myConverter"
。改为
表达式服务= (Expression) fc.getExternalContext().getSessionMap().get(" Expression ");