0

在 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>

castId当用户单击提交按钮时,jQuery 从下拉列表中检索值,下拉列表的代码是:

<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 = [];
    }              
}

我的支持豆是:

@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远程命令中的字段引起的,因为如果我删除该字段,它就可以正常工作。我很困惑为什么从下拉列表中检索到的选定值没有发送到远程命令。有谁知道问题出在哪里?

4

1 回答 1

0

仍然不确定哪里出了问题,但我发现了一个四处走走,而不是在 primefaces 扩展中使用远程命令,我尝试了 primefaces 中的原始远程命令并且它有效。

修改后的代码为:

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

对于 javascript 部分,它变为:

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

在支持 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);
于 2013-03-18T01:01:59.123 回答