我有以下JSF视图index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:p="http://primefaces.org/ui"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition template="template.xhtml">
    <ui:define name="top">
        Menu
    </ui:define>
    <ui:define name="content">
        <h:form>
            <p:menubar>
                <p:submenu label="Admin">
                    <p:submenu label="User">
                        <p:menuitem value="Add User" ajax="true">
                            <f:setPropertyActionListener target="#{menuTab.action}" value="StudentAdd"/>
                        </p:menuitem>
                        <p:menuitem value="Edit User" ajax="true">
                            <f:setPropertyActionListener target="#{menuTab.action}" value="StudentEdit"/>
                        </p:menuitem>
                    </p:submenu>
                </p:submenu>
            </p:menubar>
        </h:form>
        <p:tabView>
            <c:forEach items="#{menuTab.listOfObjs}" var="item" varStatus="loop">
                <p:tab  title="#{menuTab.value}" closable="true"/>
        </c:forEach>
    </p:tabView>
    </ui:define>
</ui:composition>




和以下托管bean MenuTab.java

package com.menu;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean(name="menuTab")
@SessionScoped
public class MenuTab implements Serializable {

/** Creates a new instance of MenuTest */
public MenuTab() {
}
private String action;
private String value;
private List<String> listOfObjs = new ArrayList();

public String getAction() {
    return action;
}

public void setAction(String action) {
    this.action = action;
    addValue(action);
}
//--------------------------------------

public void addValue(String act) {
    if (listOfObjs.isEmpty()) {
        setValue(act);
        getListOfObjs().add(getValue());
        System.out.println("First Data add First Data add First Data add::::::::::::::::" + value);
    }
    Iterator idvalu = listOfObjs.iterator();
    while (idvalu.hasNext()) {
        System.out.println("aaaaaaaaaaa=====" + idvalu.next());

        value=act;

        if (idvalu.next().equals(value)) {
            System.out.println("Data Alrady Exite Data =====" + value);
        }
        else {
            setValue(act);
            getListOfObjs().add(getValue());
            System.out.println("bbbbbbbbbbbbbbb=====" + value);
        }

    }

}

/**
 * @return the listOfObjs
 */
public List<String> getListOfObjs() {
    return listOfObjs;
}

/**
 * @param listOfObjs the listOfObjs to set
 */
public void setListOfObjs(List<String> listOfObjs) {
    this.listOfObjs = listOfObjs;
}

/**
 * @return the value
 */
public String getValue() {
    return value;
}

/**
 * @param value the value to set
 */
public void setValue(String value) {
    this.value = value;
}


}

当我单击“添加用户”菜单时,tomcat显示此消息:

aaaaaaaaaaa=====StudentAdd
Jun 24, 2012 3:30:04 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: /index.xhtml @22,110 target="#{menuTab.action}": Error writing 'action' on type com.menu.MenuTab
javax.el.ELException: /index.xhtml @22,110 target="#{menuTab.action}": Error writing 'action' on type com.menu.MenuTab
    at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:139)
    at com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler$SetPropertyListener.processAction(SetPropertyActionListenerHandler.java:206)

最佳答案

javax.el.E​​LException:/index.xhtml @ 22,110 target =“#{menuTab.action}”:在com.menu.MenuTab类型上写入“ action”时出错


这意味着表达式#{menuTab.action}后面的setter方法引发了异常。在堆栈跟踪中进一步查找该异常,然后根据异常中提供的信息相应地修复代码。

快速浏览setAction()代码表明,最可能的原因是NoSuchElementException,因为您在循环内两次调用了Iterator#next()。每次调用next()时,它将移至下一个元素。它不会返回您期望的“当前”元素。

删除以下行:

System.out.println("aaaaaaaaaaa=====" + idvalu.next());


或至少将其分配给变量

String next = idvalu.next();
System.out.println("aaaaaaaaaaa=====" + next);

if (next.equals(value)) {
    // ...

关于java - 警告:/index.xhtml @ 22,110 target =“#{menuTab.action}”:在com.menu.MenuTab类型上写入“action”时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11176519/

10-11 15:00