本文介绍了带有f:setPropertyActionListener的JSF NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用f:setPropertyActionListener收到此错误,我无法弄清楚为什么:

I'm getting this error with f:setPropertyActionListener and i can't figure out why:

HTTP Status 500 - For input string: "selectedItem"

exception:
javax.servlet.ServletException: For input string: "selectedItem"
javax.faces.webapp.FacesServlet.service(FacesServlet.java:667)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause:
java.lang.NumberFormatException: For input string: "selectedItem"
java.lang.NumberFormatException.forInputString(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
javax.el.ListELResolver.coerce(ListELResolver.java:157)
javax.el.ListELResolver.getType(ListELResolver.java:50)
com.sun.faces.el.DemuxCompositeELResolver._getType(DemuxCompositeELResolver.java:215)
com.sun.faces.el.DemuxCompositeELResolver.getType(DemuxCompositeELResolver.java:242)
org.apache.el.parser.AstValue.getType(AstValue.java:60)
org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:168)
com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler$SetPropertyListener.processAction(SetPropertyActionListenerHandler.java:209)
javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
javax.faces.component.UICommand.broadcast(UICommand.java:300)
javax.faces.component.UIData.broadcast(UIData.java:1108)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:654)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

表类:

// imports omitted
public class Table<E> extends ArrayList<E> {

    private E selectedItem;

    public E getSelectedItem() { return selectedItem; }

    public void setSelectedItem(E value) { selectedItem = value; }
}

MyTable Bean:

MyTable Bean:

// Imports omitted
@ManagedBean
@ViewScoped
public class MyTable extends Table<File> {

    @PostConstruct
    public void initBean() {
        // Loading some files into the list
    }
}

这是XHTML:

<html> <!-- Namespaces and stuff omitted -->
    <h:head>...</h:head>
    <h:body>
      <h:form>
        <h:dataTable var="item" value="#{myTable}">
          <h:column>
            <h:commandButton value="Try Me!">
              <f:setPropertyActionListener value="#{item}" target="#{myTable.selectedItem}"/>
                <!-- I'm getting a warning from eclipse here: property not found -->
            </h:commandButton>
          </h:column>
        </h:dataTable>
      </h:form>
    </h:body>
</html>


$ b )。任何提示都被接受,谢谢!

I'm using Eclipse Luna (Java EE IDE) with Tomcat 8 and JSF 2.2.11 (mojarra). Any hints are accepted, thank you!

推荐答案

你用自己的花哨的实现将你的自我编码成一个角落。请查看。您的代码在步骤3中阻塞:

You kind of coded your self into a corner with your fancy bean implementation. Take a look at the processing steps for the f:setActionPropertyListener. Your code is choking at step 3:

,原因如下:


  1. EL处理器已经确定 myTable 是一个列表。因此,它已将表达式 myTable.selectedItem 的评估委托给 javax.el.ListELResolver class

  1. The EL processor has determined that myTable is a List. Because of this, it has delegated the evaluation of the expression myTable.selectedItem to the javax.el.ListELResolver class

ELResolver在遇到 myTable 基础对象时,确定它是一个列表,并自动假定以下字符串是指一个列表索引,即myTable。 selectedItem ,其中 selectedItem 作为列表索引(根据EL规范, [] 可互换列表)。您可以在操作中看到它。虽然在tomcat源中可能并不明显,但如果您在,您有以下注释:

The ELResolver, on encountering the myTable base object, determines it's a List and automatically assumes that the following string is referring to a list index, i.e. myTable.selectedItem, where selectedItem is supposed to be a list index (per the EL specification, the [] and . are interchangeable for lists). You can see it in action here. While it may not be immediately apparent in the tomcat source, if you check the comment in a similar implementation in Jboss for example, you have the following comment:


property argument这里是指表达式的 selectedItem 部分

"property argument" here is referring to the selectedItem portion of your expression


  1. EL处理器现在尝试将字符串 selectedItem 转换为一个整数(用作列表索引),这反过来会在一个 500

  1. The EL processor now attempts to convert the string selectedItem to an integer (to be used as a list index) which in turn explodes in a 500

您将使您的工作变得更容易结合您的数据结构和支持bean,如Rami。 Q建议。更清洁的方式IMO

You'll make your work a whole lot easier by not combining your data structure and your backing bean, like Rami. Q suggested. Much cleaner that way IMO

这篇关于带有f:setPropertyActionListener的JSF NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!