接缝问题:无法通过反射设置字段值
时间:2020-03-06 14:58:01 来源:igfitidea点击:
我的Seam代码有问题,我似乎无法弄清楚我做错了什么。这是我的主意:)这是堆栈跟踪的摘录:
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.oobjects.sso.manager.home.PresenceHome.customerId to java.lang.String
我试图在传递给我的一个bean的URL上获取参数集。为此,我在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>
我的豆子开始像这样:
@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>
所有这些似乎都可以正常工作。当我将鼠标悬停在页面上方的链接上时,我看到的URL以"?customerId = 123"结尾。因此,该参数已传递过来,可以很容易地将其转换为Long类型。但是由于某种原因,事实并非如此。在其他项目中,我之前已经做过类似的事情,然后就起作用了。我只是看不到它现在不起作用。
如果我从页面声明中删除该元素,则可以顺利通过页面。
那么,有人有什么想法吗?
解决方案
尝试:
...<f:param name =" customerId" value ="#{customerId.toString()}" />
...
我们的代码执行类似的操作,但是Java类中的customerId属性为String:
private String customerId;
public String getCustomerId() {
return customerId;
}
public void setCustomerId(final String customerId) {
this.customerId = customerId;
}
我们可以尝试使用属性编辑器。
将其放入与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);
}
}
}
我们想要将转换器添加到pages.xml文件。像这样:
<param name="customerId"
value="#{presenceHome.customerId}"
converterId="javax.faces.Long" />
有关更多详细信息,请参见seam附带的seampay示例。

