javax.el.PropertyNotFoundException: Target Unreachable, 'BracketSuffix' 返回 null



我试图使用一个简单的例子在List<String>中插入多个IP。但是我收到以下错误。

javax.el.PropertyNotFoundException: Target Unreachable, 'BracketSuffix' 返回 null

这是我的 JSF 2.2 页面:

<h:form id="form">
    <ui:repeat value="#{exampleBean.ipAddresses}" var="s"
        varStatus="status">
        <h:inputText value="#{exampleBean.ipAddresses[status.index]}" />
    </ui:repeat>
    <h:inputText value="#{exampleBean.newIp}" />
    <h:commandButton value="Add" action="#{exampleBean.add}" />
    <h:commandButton value="Save" action="#{exampleBean.save}" />
</h:form>

这是我的支持豆:

@ManagedBean
@ViewScoped
public class ExampleBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<String> ipAddresses;
    private String newIp;
    @PostConstruct
    public void init() {
        ipAddresses= new ArrayList<String>();
    }
    public String save() {
        System.out.println(ipAddresses.toString());
        return null;
    }
    public void add() {
        ipAddresses.add(newIp);
        newIp = null;
    }
    public List<String> getIpAddresses() {
        return ipAddresses;
    }
    public String getNewIp() {
        return newIp;
    }
    public void setNewIp(String newIp) {
        this.newIp = newIp;
    }
}

这是如何造成的,我该如何解决?

javax.el.PropertyNotFoundException: Target Unreachable, 'BracketSuffix' 返回 null

异常消息错误。这是服务器正在使用的 EL 实现中的一个错误。在您的特定情况下,它的真正含义是:

javax.el.PropertyNotFoundException: Target Unreachable, 'ipAddresses[status.index]' 返回 null

换句话说,数组列表中没有这样的项目。这表明 bean 在表单提交时被重新创建,因此将所有内容重新初始化为默认值。因此,它的行为就像一个@RequestScoped。很可能您导入了错误的@ViewScoped注释。对于@ManagedBean,您需要确保@ViewScoped是从相同的javax.faces.bean包导入的,而不是JSF 2.2引入的javax.faces.view专门用于CDI @Named bean

的软件包。
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

另请参阅:

  • 识别和解决 javax.el.PropertyNotFoundException: Target Unreachable
  • 使用 JSF 2.2 时,在每个回发请求上重新创建@ViewScoped Bean

更新:根据评论,您使用的是WebSphere 8.5,它通常附带古老的MyFaces 2.0.x版本。我用MyFaces 2.0.5重现了你的问题。它的<ui:repeat>未能记住迭代状态的视图状态,这就是为什么即使您正确使用 @ViewScoped bean,您的构造仍然失败的原因。我可以通过改用<c:forEach>来解决它。

<c:forEach items="#{exampleBean.ipAddresses}" var="s" varStatus="status">
    ...
</c:forEach>

另一种解决方案(显然,除了将MyFaces升级到更新/体面的版本之外(是将不可变String包装在可变的javabean中,例如

public class IpAddress implements Serializable {
    private String value;
    // ...
}

这样您就可以使用List<IpAddress>而不是List<String>,因此您不再需要触发MyFaces错误的varStatus

private List<IpAddress> ipAddresses;
private IpAddress newIp;
@PostConstruct
public void init() {
    ipAddresses= new ArrayList<IpAddress>();
    newIp = new IpAddress();
}

<ui:repeat value="#{exampleBean.ipAddresses}" var="ipAddress">
    <h:inputText value="#{ipAddress.value}" />
</ui:repeat>
<h:inputText value="#{exampleBean.newIp.value}" />

最新更新