org.primefaces.extensions.component.remotecommand.RemoteCommandRenderer.decode中的java.lang.NullPointerException

时间:2013-03-15 07:44:40

标签: jsf primefaces primefaces-extensions

在Facelet上,我尝试使用<pe:remoteCommand>来更新backingbean值:

<pe:remoteCommand name="saveMapArea" process="@this" actionListener="#{defineMapArea.createMapArea}">
    <pe:assignableParam name="areaString"  assignTo="#{defineMapArea.tagMapArea.mapArea}"/>  
    <pe:assignableParam name="areaName" assignTo="#{defineMapArea.tagMapArea.name}" />
    <pe:assignableParam name="castId" assingTo="#{defineMapArea.tagMapArea.castId}" />           
</pe:remoteCommand>
当用户点击提交按钮时,jQuery从下拉列表中检索

castId值,下拉列表的代码为:

<h:selectOneMenu id="area_assign_device">
    <f:selectItem itemLabel="Select Icast" itemValue="" />
    <f:selectItems value="#{defineMapArea.castList}" var="cast" itemLabel="#{cast.name}" itemValue="#{cast.castId}"/>
</h:selectOneMenu>

提交按钮的代码是:

<p:commandButton value="Submit" type="button" onclick="CreateArea.saveArea()"/>

调用remotecommand'saveMapArea'的代码是:

saveArea : function() {
    if(CreateArea.validate()) {
        // ...some other code to init map area...

        var name=jQuery('#area_name_text').val();
        var castId=jQuery('#area_assign_device').val();
        saveMapArea(mapArea, name, castId);
        CreateArea.points = [];
    }              
}

我的支持bean是:

@ManagedBean
@RequestScoped
public class DefineMapArea extends BaseJsfBean {

    private static final long serialVersionUID = 1L;
    private static Log log = LogFactory.getLog(DefineMapArea.class);
    private TagMapArea tagMapArea;
    private List<TagMapArea> areaList;
    private String areaListJson;

    @ManagedProperty(value="#{tagCustomService}")
    private ITagCustomService tagCustomService;  

    @Override
    protected void init() throws Exception {
        // ..some initializations...
    }  

    public void createMapArea() {
        log.info("Area Persisting : " + tagMapArea.getMapArea());               
        tagMapArea.setTagMapImageId(2);
        tagMapArea.setMapAreaType("test");
        tagMapArea.setDescription("test");
    }

    // ....all the getters and setters....
}

TagMapArea是JPA实体,包含castId字段的部分是:

@Column(name = "cast_id", nullable = false)
protected Integer castId;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="cast_id", nullable=false, insertable=false, updatable=false)
protected TagCast cast;    

问题是当我点击提交按钮时,它会抛出NullPointerException这样:

java.lang.NullPointerException
    at org.primefaces.extensions.component.remotecommand.RemoteCommandRenderer.decode(RemoteCommandRenderer.java:82)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1377)
    at org.apache.myfaces.context.servlet.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:731)
    at org.apache.myfaces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:214)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:932)

我很确定这是由远程命令中的castId字段引起的,因为如果删除该字段,它就可以了。我很困惑为什么从下拉列表中检索到的选定值不会被发送到remotecommand。有没有人知道问题出在哪里?

1 个答案:

答案 0 :(得分:0)

仍然不确定哪里出了问题,但是我找到了一个走动,而不是在primefaces扩展中使用远程命令,我在primefaces中尝试了原始远程命令并且它可以工作。

更改后的代码是:

<p:remoteCommand name="saveMapArea" actionListener="#{defineMapArea.createMapArea}" />

并且对于javascript部分,它变为:

saveMapArea([{name:'mapArea', value:mapArea},{name:'name',value:name},{name:'castId',value:castId}]);

在backing bean的createMapArea方法中,获取值的代码片段是:

    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    String mapArea = (String)map.get("mapArea");
    String name = (String)map.get("name");
    String castId = (String)map.get("castId");
    log.info("Area Persisting : " + mapArea);
    log.info("name: " + name + " castId: " + castId);