javax.el.PropertyNotFoundException: /tab.xhtml @65,50 value="#{stackElement.name}": class 'java.lang.String' 没有 属性 'name'
javax.el.PropertyNotFoundException: /tab.xhtml @65,50 value="#{stackElement.name}": The class 'java.lang.String' does not have the property 'name'
如果我第一次加上stackElement
就可以了,输出是:
list size = 1
但是在添加第二个元素后出现错误:
Error Rendering View[/tab.xhtml]: javax.el.PropertyNotFoundException:
/tab.xhtml @65,50 value="#{stackElement.name}": The class
'java.lang.String' does not have the property 'name'. ... Caused by:
javax.el.PropertyNotFoundException: The class 'java.lang.String' does
not have the property 'name'.
ERROR [io.undertow.request] (default task-4) UT005023: Exception
handling request to /Play/tab.xhtml: java.lang.IllegalStateException:
CDATA tags may not nest ...
为什么会这样?
页
<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;
}
}
}
您的代码几乎没有问题。
- 您应该在
<p:orderList>
组件上使用 converter="myConverter"
。
- 表达式bean 属于SessionScoped。但是您是从 ApplicationScope 获取它的。
改为
表达式服务=(表达式)fc.getExternalContext().getSessionMap().get("expression");
进行这些更改后它的工作。
如果我第一次加上stackElement
就可以了,输出是:
list size = 1
但是在添加第二个元素后出现错误:
Error Rendering View[/tab.xhtml]: javax.el.PropertyNotFoundException: /tab.xhtml @65,50 value="#{stackElement.name}": The class 'java.lang.String' does not have the property 'name'. ... Caused by: javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'name'.
ERROR [io.undertow.request] (default task-4) UT005023: Exception handling request to /Play/tab.xhtml: java.lang.IllegalStateException: CDATA tags may not nest ...
为什么会这样?
页
<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;
}
}
}
您的代码几乎没有问题。
- 您应该在
<p:orderList>
组件上使用converter="myConverter"
。 - 表达式bean 属于SessionScoped。但是您是从 ApplicationScope 获取它的。
改为
表达式服务=(表达式)fc.getExternalContext().getSessionMap().get("expression");
进行这些更改后它的工作。