简体   繁体   English

接缝问题:无法通过反射设置字段值

[英]Seam Problem: Could not set field value by reflection

I'm having a problem with my Seam code and I can't seem to figure out what I'm doing wrong. 我的Seam代码有问题,我似乎无法弄清楚我做错了什么。 It's doing my head in :) Here's an excerpt of the stack trace: 它正在我的脑袋:)这是一个堆栈跟踪的摘录:

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

I'm trying to get a parameter set on my URL passed into one of my beans. 我正在尝试将我的URL上的参数集传递到我的一个bean中。 To do this, I've got the following set up in my pages.xml: 为此,我在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>

My bean starts like this: 我的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
}

Finally the link I use to link one one page to the next looks like this: 最后,我用来将一个页面链接到下一个页面的链接如下所示:

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

All this seems to work fine. 这一切似乎都很好。 When I hover over the link above in my page, I get a URL ending in something like "?customerId=123". 当我将鼠标悬停在我页面上方的链接上时,我会得到一个以“?customerId = 123”结尾的URL。 So the parameter is being passed over and it's something that can be easily converted into a Long type. 因此参数被传递,并且它可以很容易地转换为Long类型。 But for some reason, it's not. 但由于某种原因,事实并非如此。 I've done similar things to this before in other projects and it's worked then. 我之前在其他项目中做过类似的事情,然后就可以了。 I just can't see what it isn't working now. 我只是看不出它现在没有用的东西。

If I remove the element from my page declaration, I get through to the page fine. 如果我从页面声明中删除元素,我会很好地浏览页面。

So, does anyone have any thoughts? 那么,有没有人有任何想法?

You want to add a converter to your pages.xml file. 您想要在pages.xml文件中添加转换器。 Like this: 像这样:

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

See the seampay example provided with seam for more details. 有关详细信息,请参阅随seam提供的seampay示例。

尝试:... <f:param name="customerId" value="#{customerId.toString()}" /> ...

Our code does something similar, but with the customerId property in the Java class as a String : 我们的代码执行类似的操作,但将Java类中的customerId属性作为String

private String customerId;

public String getCustomerId() {
    return customerId;
}

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

You could try using a property editor. 您可以尝试使用属性编辑器。

Put this into the same package as your bean: 将它放入与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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM