【问题标题】:Seam Problem: Could not set field value by reflection接缝问题:无法通过反射设置字段值
【发布时间】:2010-09-14 11:28:20
【问题描述】:

我的 Seam 代码有问题,我似乎无法弄清楚我做错了什么。这让我很头疼:) 这是堆栈跟踪的摘录:

Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.oobjects.sso.manager.home.PresenceHome.customerId to java.lang.String

我正在尝试将我的 URL 上的参数集传递给我的一个 bean。为此,我在 pages.xml 中进行了以下设置:

<page view-id="/customer/presences.xhtml">
  <begin-conversation flush-mode="MANUAL" join="true" />
  <param name="customerId" value="#{presenceHome.customerId}" />
  <raise-event type="PresenceHome.init" />
  <navigation>
    <rule if-outcome="persisted">
      <end-conversation />
      <redirect view-id="/customer/presences.xhtml" />
    </rule>
  </navigation>
</page>

我的 bean 是这样开始的:

@Name("presenceHome")
@Scope(ScopeType.CONVERSATION)
public class PresenceHome extends EntityHome<Presence> implements Serializable {
  @In
  private CustomerDao customerDao;

  @In(required = false)
  private Long presenceId;

  @In(required = false)
  private Long customerId;

  private Customer customer;

  // Getters, setters and other methods follow. They return the correct types defined above
}

最后,我用来将一个页面链接到下一个页面的链接如下所示:

<s:link styleClass="#{selected == 'presences' ? 'selected' : ''}"
    view="/customer/presences.xhtml" title="Presences" propagation="none">
    <f:param name="customerId" value="#{customerId}" />
    Presences
</s:link>

这一切似乎都很好。当我将鼠标悬停在页面中的上述链接上时,我得到一个以“?customerId=123”结尾的 URL。所以参数被传递了,它可以很容易地转换为 Long 类型。但由于某种原因,它不是。我之前在其他项目中做过类似的事情,然后它就起作用了。我只是看不出它现在有什么问题。

如果我从我的页面声明中删除该元素,我可以正常进入页面。

那么,有人有什么想法吗?

【问题讨论】:

    标签: java seam


    【解决方案1】:

    您想在 pages.xml 文件中添加转换器。像这样:

    <param name="customerId" 
          value="#{presenceHome.customerId}" 
    converterId="javax.faces.Long" />
    

    有关更多详细信息,请参阅随 seam 提供的 seampay 示例。

    【讨论】:

      【解决方案2】:

      您可以尝试使用属性编辑器。

      把它和你的 bean 放在同一个包中:

      import java.beans.PropertyEditorSupport;
      
      public class PresenceHomeEditor extends PropertyEditorSupport {
          public void setAsText(final String text) throws IllegalArgumentException {
              try {
                  final Long value = Long.decode(text);
                  setValue(value);
              } catch (final NumberFormatException e) {
                  super.setAsText(text);
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        我们的代码做了类似的事情,但在 Java 类中使用 customerId 属性作为 String

        private String customerId;
        
        public String getCustomerId() {
            return customerId;
        }
        
        public void setCustomerId(final String customerId) {
            this.customerId = customerId;
        }
        

        【讨论】:

          【解决方案4】:

          尝试: ... &lt;f:param name="customerId" value="#{customerId.toString()}" /&gt; ...

          【讨论】:

            猜你喜欢
            • 2019-04-22
            • 2014-10-07
            • 1970-01-01
            • 2015-06-15
            • 2011-06-25
            • 2023-04-05
            • 1970-01-01
            • 2019-05-15
            • 2020-06-10
            相关资源
            最近更新 更多