Primefaces:组件DataTable syncColumnWidths的异常

时间:2012-07-06 02:25:36

标签: java jsf primefaces

我在PrimeFaces数据表中遇到问题,

在页面xhtml,我有两个用于SQL和函数的单选按钮,然后我有一个输入文本来选择我想要调用的函数,如“select * from t_tenant”

这是第xhtml页的代码:

 <p:panel>

        <p:commandButton value="#{piece['org.jlego.commonui.run']}" actionListener="#{dbQueryController.run}" ajax="true" update="queryEditor grid growl exportButton panelMessage" />
        <p:commandButton value="#{piece['org.jlego.commonui.clear']}" update="queryEditor panelMessage grid  growl exportButton" actionListener="#{dbQueryController.clear}"/>

        <br/>

        <h:panelGrid columns="2">
            <h:outputText value="#{piece['org.jlego.commonui.type']}" />
            <h:selectOneRadio id="queryType" value="#{dbQueryController.type}">
                <f:selectItem itemValue="#{piece['org.jlego.commonui.sql']}" itemLabel="#{piece['org.jlego.commonui.sql']}" />
                <f:selectItem itemValue="#{piece['org.jlego.commonui.function']}" itemLabel="#{piece['org.jlego.commonui.function']}" />
            </h:selectOneRadio>
        </h:panelGrid>

        <p:inputTextarea id="queryEditor" style="width: 800px; height: 200px; font-family: Courier" 
           value="#{dbQueryController.query}" />

        <p:panel id="panelMessage">
            <h:outputText value="#{dbQueryController.message}" />
        </p:panel>

        <br/>
 <...><...>
<p:dataTable id="grid" value="#{dbQueryController.queryList}" var="item"
                        scrollable="true" scrollWidth="1000" scrollHeight="300" resizableColumns="true">

                <p:columns value="#{dbQueryController.columns}" var="column" columnIndexVar="colIndex">  
                    <f:facet name="header">  
                        #{column.header}  
                    </f:facet>  

                    #{item[column.property]}  
                </p:columns>          
       </p:dataTable>
    </p:panel>

这是控制器的代码:

@Component
@Scope("view")
public class DbQueryController extends AbstractController {

private static final Logger log = LoggerFactory.getLogger(DbQueryController.class);

@Autowired
private ServiceContext serviceContext;
private String type;
private String query;
private String message;
private List<JSONObject> queryList;

private String columnTemplate = "";
private List<ColumnModel> columns;

public DbQueryController() {
    type = "SQL"; // Default value
    queryList = new ArrayList<JSONObject>();
    columns = new ArrayList<ColumnModel>();
}

@PostConstruct
private void init() {

}

 /**
 * When "Run" button clicked
 */
public String run() {
    log.debug("run() called");

    // Validate Query Must Be Filled
    if (query == null || query.equals("")){
        FacesUtils.addErrorMessage(getLabel("org.jlego.commonui.noStatementQueryFound"));
        return "";
    }

    queryList = new ArrayList<JSONObject>();
    message = "";

          JSONObject input = new JSONObject();
    input.put("type", type);
    input.put("query", query);

    try {
        JSONObject output = serviceContext.executeService("jlego#getResultQuery", input);
        JSONObject result = JSONObjectUtil.getResult(output);
        String status = JSONObjectUtil.getResultStatus(output);

        if (status != null && status.equals(GeneralConstants.OK)) {
            queryList = (List<JSONObject>) JSONObjectUtil.convertToArray(result.get("queryList"));
            if (queryList == null) {
                queryList = new ArrayList<JSONObject>();
            }
            message = queryList.size() + " data found";
            if (result.get("columns") instanceof JSONArray) {
                JSONArray arr = (JSONArray) result.get("columns");
                columnTemplate = "";

                StringBuilder build = new StringBuilder();
                for (int i = 0; i < arr.size(); i++) {
                    build.append(arr.get(i)).append(" ");
                }
                columnTemplate = build.toString();

            } else if (result.get("columns") instanceof String) {
                String str = (String) result.get("columns");
                columnTemplate = str;
            }

            createDynamicColumns();

        } else if (status != null && status.equals(GeneralConstants.FAIL)) 
   {
            FacesUtils.addErrorMessage((String) result.get("message"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        FacesUtils.addErrorMessage(e.getMessage());
    }
    return "";
} 

  /**
 * When "Clear" button clicked
 */
public void clear() {
    setQuery("");
    queryList = null;
    columnTemplate = "";
    columns.clear();
    message = "";

}
<....>

/**
 * @return the queryList
 */
public List<JSONObject> getQueryList() {
    return queryList;
}

/**
 * @return the columnTemplate
 */
public String getColumnTemplate() {
    return columnTemplate;
}

 public List<ColumnModel> getColumns() {
    return columns;
}

static public class ColumnModel implements Serializable {

    private String header;
    private String property;

    public ColumnModel(String header, String property) {
        this.header = header;
        this.property = property;
    }
   <...>
 private void updateColumns() {
    // reset table state
    UIComponent table = FacesContext.getCurrentInstance().getViewRoot().findComponent(":rootform:grid");
    table.setValueExpression("sortBy", null);

    // update columns
    createDynamicColumns();
}

public void createDynamicColumns() {
    log.debug(columnTemplate);

    String[] columnKeys = columnTemplate.split(" ");
    columns.clear();

    for (String columnKey : columnKeys) {
        String key = columnKey.trim();
        columns.add(new ColumnModel(columnKey, columnKey));
    }

}

问题是,当我填写输入文本查询以选择函数SQL如“select * from t_tenant”并且我单击按钮Run时,数据表已经显示但是表的网格无法从数据库整体显示数据,并且我有例外。

以下是我得到的异常消息:

Jul 6, 2012 9:13:14 AM com.sun.faces.context.PartialViewContextImpl processPartial
INFO: java.lang.NullPointerException
java.lang.NullPointerException
at org.primefaces.component.datatable.DataTable.syncColumnWidths(DataTable.java:1031)
at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:60)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:787)
at javax.faces.component.UIData.processDecodes(UIData.java:1162)
at org.primefaces.component.datatable.DataTable.processDecodes(DataTable.java:554)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:506)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at javax.faces.component.UIData.visitTree(UIData.java:1411)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
at javax.faces.component.UIForm.visitTree(UIForm.java:344)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
at org.jlego.web.WebLoginSessionFilter.doFilter(WebLoginSessionFilter.java:95)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

如果我使用的是primefaces 3.3.1

感谢您的帮助

此致

0 个答案:

没有答案