Lee Theobald
Lee Theobald

Reputation: 8587

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. 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. To do this, I've got the following set up in my 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:

@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". So the parameter is being passed over and it's something that can be easily converted into a Long type. 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?

Upvotes: 6

Views: 6347

Answers (4)

Joe Dean
Joe Dean

Reputation: 3376

You want to add a converter to your pages.xml file. Like this:

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

See the seampay example provided with seam for more details.

Upvotes: 7

MetroidFan2002
MetroidFan2002

Reputation: 29878

You could try using a property editor.

Put this into the same package as your 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);
        }
    }
}

Upvotes: 0

Peter Hilton
Peter Hilton

Reputation: 17344

Our code does something similar, but with the customerId property in the Java class as a String:

private String customerId;

public String getCustomerId() {
    return customerId;
}

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

Upvotes: 0

Chobicus
Chobicus

Reputation: 2084

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

Upvotes: 0

Related Questions