0

使用此 StackOverflow 答案尝试实现无提交表单。该页面由一个selectomenu控件和一个表格组成。在更改选择时,我希望刷新表体。页面加载没有问题,但是在进行选择时出现以下错误:

10:22:48,241 ERROR [[FacesServlet]] Servlet.service() for servlet FacesServlet threw exception: java.lang.NullPointerException
    at com.sun.faces.context.PartialViewContextImpl.createPartialResponseWriter(PartialViewContextImpl.java:441) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.context.PartialViewContextImpl.access$300(PartialViewContextImpl.java:71) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.context.PartialViewContextImpl$DelayedInitPartialResponseWriter.getWrapped(PartialViewContextImpl.java:582) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at javax.faces.context.PartialResponseWriter.startDocument(PartialResponseWriter.java:115) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:199) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:123) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:505) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:445) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:636) [rt.jar:1.6.0_18]

我正在使用 JBoss-AS-7.1.1,selectonmenu看起来像这样:

<h:selectOneMenu id="ship"
             value="#{shipManager.current}">
    <f:selectItems value="#{shipManager.list}"
               var="ship"
               itemValue="#{ship}"
               itemLabel="#{ship.name}" />
    <f:ajax listener="#{shipManager.changeShip}" />
</h:selectOneMenu> 

如果我删除了 ajax 组件,那么尽管(当然)shipManager没有更新,但没有问题。

做出选择后,我想添加该renderer部分以刷新我的表格主体。

非常感谢您提供的任何帮助。

史蒂夫

这里要求的是我的 XHTML 代码:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">

  <h:head>
    <title>Voyage Cruises</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link type="text/css" rel="stylesheet" media="all" href="/css/screen.css"/>
    <link type="text/css" rel="stylesheet" media="all" href="/css/cruise.css"/>
    <script src="http://open.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js"></script> 
    <script src="js/map.js" ></script>
    <script src="js/cruise.js" ></script>
  </h:head>

  <h:body>
    <div id="page">
      <div id="float-map">
    <div id="map"></div>
      </div>

      <h:form id="ship-form">

    <div id="ship-controls">
      <div class="float-ctrls">
        <a href="/ports.html" title="Find Ports"><img src="/images/action_search.png" alt="Find Ports"/></a>
      </div>
      <div>
        <span id="ship-selector" >
          <h:selectOneMenu id="ship"
                   value="#{shipManager.current}">
        <f:selectItems value="#{shipManager.list}"
                   var="ship"
                   itemValue="#{ship}"
                   itemLabel="#{ship.name}" />
        <f:ajax execute="ship" render="@form" listener="#{shipManager.changeShip}" />
          </h:selectOneMenu> 
        </span>
        <select id="month" />
        <select id="year" />
        <br />
        <span class="input-container"><b>Tonnage:</b><input id="tonnage" type="text" size="6" value="30,277" /></span>
        <span class="input-container"><b>Audit Time:</b><input id="audit" type="text" size="2" value="6" /></span>
        <span class="input-container"><b>Fleet:</b><input id="fleet" type="text" size="20" value="P&amp;O" /></span>
      </div>
    </div>

    <table class="cruise">
      <thead>
        <tr>
          <th class="date">Date</th>
          <th class="port">Port</th>
          <th class="arrive">Arrive</th>
          <th class="depart">Depart</th>
        </tr>
      </thead>
      <tbody>
        <ui:repeat var="cruise"
               value="#{cruiseManager.list}" 
               varStatus="table">
          <tr class="#{table.even ? 'even' : 'odd'}">
        <td class="date">
          <h:outputFormat value="{0, date,d MMM}">
            <f:param value="#{cruise.date}" />
          </h:outputFormat>
        </td>
        <h:panelGroup rendered="#{null != cruise.port}">
          <td class="port">#{cruise.port.name}</td>
          <td class="arrive">
            <h:outputFormat rendered="#{null != cruise.arrival}"
                    styleClass="#{cruise.arrivalEstimated ? 'est-time' : 'act-time'}"
                    value="{0, date,HH:mm}">
              <f:param value="#{cruise.arrival}" />
            </h:outputFormat>
          </td>
          <td class="depart">
            <h:outputFormat rendered="#{null != cruise.departure}"
                    styleClass="#{cruise.departureEstimated ? 'est-time' : 'act-time'}"
                    value="{0, date,HH:mm}">
              <f:param value="#{cruise.departure}" />
            </h:outputFormat>
          </td>
        </h:panelGroup>
        <h:panelGroup rendered="#{null == cruise.port}">
          <td class="at-sea" colspan="3">At Sea</td>
        </h:panelGroup>
          </tr>
        </ui:repeat>
      </tbody>
    </table>

      </h:form>
    </div>
  </h:body>
</html>

和 ShipManager:

package com.nutrastat.voyager.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;

import com.nutrastat.voyager.db.HarbourMasterLocal;
import com.nutrastat.voyager.db.ShipEntity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ManagedBean
@ViewScoped
public class ShipManager 
    implements Serializable {

    static final long serialVersionUID = -3017226836104715117L;

    private transient final Logger log;

    private ShipEntity currentShip;

    @EJB
    private HarbourMasterLocal harbourMaster;

    public ShipManager() {
    log = LoggerFactory.getLogger(getClass());
    }

    public ShipEntity getCurrent() {
    if (log.isInfoEnabled())
        log.info("getCurrent(): " + currentShip);
    return currentShip;
    }

    public void setCurrent(ShipEntity ship) {
    if (log.isInfoEnabled())
        log.info("setCurrent(" + currentShip + "): " + ship);
    currentShip = ship;
    }

    public void changeShip(AjaxBehaviorEvent evt) {
    if (log.isInfoEnabled())
        log.info("Ship now: " + currentShip);
    }

    public List<ShipEntity> getList() {
    try {
        return harbourMaster.listShips();

    } catch (Exception cause) {
        log.error("Getting the list of ships", cause);
        return new ArrayList<ShipEntity>(0);
    }
    }
}
4

2 回答 2

3

您的堆栈跟踪看起来不完整,因为它实际上并未引用您的代码(这是我希望抛出 npe 的地方)。尽管如此,您的选择菜单没有转换器,所以除了任何其他问题,您的表单可能仍然无法保存或注册选择

  1. 实现一个 JSF 转换器并在该 selectonemenu 上声明它。当一个选择类型组件需要选择和保存复杂的POJO类型时这是必须的你可以看这个例子

  2. 尝试摆脱<h:form/>标记中的那个巨大的东西。该表单中有太多其他组件可能会给您带来问题。以不同的形式将组件分解成更小的块

于 2013-01-03T13:57:11.330 回答
0

如果您的 bean 的范围是 @RequestScoped 则将其更改为 @ViewScoped ,通常 ajax 最好使用 @ViewScoped 完成...导致每次提交时创建 bean 的新实例...

@ManagedBean(name="shipManager")
@ViewScoped

还要确保 changeShip 方法以 AjaxBehaviorEvent 作为参数

(IE)

 public void changeShip (AjaxBehaviorEvent event){
//** do your stuf 
 }

并在 f:Ajax 中指定执行和渲染属性

<f:ajax  execute="ship" render="@form"  listener="#{shipManager.changeShip}" />
于 2013-01-03T12:03:04.933 回答