提交

时间:2016-04-21 10:08:32

标签: jsf jsf-2

我有2个班GamesFouls,游戏中有很多犯规。用户使用游戏id发送参数以找到他想要的game,然后创建currentGame变量。其类型为Game。我有datatable打印当前游戏中的犯规

setCurentGame setter从页面正文onload调用。

到目前为止一切正常,但是当我添加一个新的foul时,没有添加到数据表中,我的假设是游戏已被检索(因为当前游戏是在之前创建的)所以新的犯规是在数据库中而不是在Game currentGame中。

有没有什么方法可以在每次添加犯规时更新当前游戏?

我尝试刷新页面,甚至在创造犯规后再次打电话给看见者,但没有任何效果

datatable看起来像这样:

<p:dataTable id="datalist" value="#{gameController.currentGame.foulCollection}" var="item">

                    <f:facet name="{exporters}">

                    </f:facet>
                    <p:ajax event="rowSelect"   update="createButton viewButton editButton deleteButton"/>
                    <p:ajax event="rowUnselect" update="createButton viewButton editButton deleteButton"/>

                    <p:column width="32" sortBy="#{item.id}" filterBy="#{item.id}">
                        <f:facet name="header">
                            <h:outputText value="#{bundle.ListFoulTitle_id}"/>
                        </f:facet>
                        <h:outputText value="#{item.id}"/>
                    </p:column>

                    <p:column sortBy="#{item.quarter}" filterBy="#{item.quarter}">
                        <f:facet name="header">
                            <h:outputText value="#{bundle.ListFoulTitle_quarter}"/>
                        </f:facet>
                        <h:outputText value="#{item.quarter}"/>
                    </p:column>
                    <p:column sortBy="#{item.foultime}" filterBy="#{item.foultime}">
                        <f:facet name="header">
                            <h:outputText value="#{bundle.ListFoulTitle_foultime}"/>
                        </f:facet>
                        <h:outputText value="#{item.foultime}"/>
                    </p:column>





                    <f:facet name="footer">
                        <p:commandButton id="createButton" icon="ui-icon-plus"   value="#{bundle.Create}" actionListener="#{foulController.prepareCreate}" update=":FoulCreateForm" oncomplete="PF('FoulCreateDialog').show()"/>

                    </f:facet>
                </p:dataTable>

GameController bean:

package onisiforos.fouls.classes;

import onisiforos.fouls.db.Game;
import onisiforos.fouls.classes.util.JsfUtil;
import onisiforos.fouls.classes.util.JsfUtil.PersistAction;
import onisiforos.fouls.bean.GameFacade;

import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@Named("gameController")
@SessionScoped
public class GameController implements Serializable {

    @EJB
    private onisiforos.fouls.bean.GameFacade ejbFacade;
    private List<Game> items = null;
    private Game selected;
    private Game currentGame;

    public Game getCurrentGame() {
        return currentGame;
    }



    public void setCurrentGame() {
        int gid =
        Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("gameid"));
        Game cg = getGame(gid);
        this.currentGame = cg;
    }




    public GameController() {
    }

    public Game getSelected() {
        return selected;
    }

    public void setSelected(Game selected) {
        this.selected = selected;
    }

    protected void setEmbeddableKeys() {
    }

    protected void initializeEmbeddableKey() {
    }

    private GameFacade getFacade() {
        return ejbFacade;
    }

    public Game prepareCreate() {
        selected = new Game();
        initializeEmbeddableKey();
        return selected;
    }



        public void create() {
            setCurrentGame()
            persist(PersistAction.CREATE, ResourceBundle.getBundle("/Bundle").getString("GameCreated"));
            updateCurrentGame();
            if (!JsfUtil.isValidationFailed()) {
                items = null;    
            }
        }

    public void update() {
        persist(PersistAction.UPDATE, ResourceBundle.getBundle("/Bundle").getString("GameUpdated"));
    }

    public void destroy() {
        persist(PersistAction.DELETE, ResourceBundle.getBundle("/Bundle").getString("GameDeleted"));
        if (!JsfUtil.isValidationFailed()) {
            selected = null; // Remove selection
            items = null;    
        }
    }

    public List<Game> getGames() {
        if (items == null) {
            items = getFacade().findAll();
        }
        return items;
    }

    private void persist(PersistAction persistAction, String successMessage) {
        if (selected != null) {
            setEmbeddableKeys();
            try {
                if (persistAction != PersistAction.DELETE) {
                    getFacade().edit(selected);
                } else {
                    getFacade().remove(selected);
                }
                JsfUtil.addSuccessMessage(successMessage);
            } catch (EJBException ex) {
                String msg = "";
                Throwable cause = ex.getCause();
                if (cause != null) {
                    msg = cause.getLocalizedMessage();
                }
                if (msg.length() > 0) {
                    JsfUtil.addErrorMessage(msg);
                } else {
                    JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
                }
            } catch (Exception ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            }
        }
    }

    public Game getGame(java.lang.Integer id) {
        return getFacade().find(id);
    }

    public List<Game> getItemsAvailableSelectMany() {
        return getFacade().findAll();
    }

    public List<Game> getItemsAvailableSelectOne() {
        return getFacade().findAll();
    }

    @FacesConverter(forClass = Game.class)
    public static class GameControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            GameController controller = (GameController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "gameController");
            return controller.getGame(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Game) {
                Game o = (Game) object;
                return getStringKey(o.getId());
            } else {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "object {0} is of type {1}; expected type: {2}", new Object[]{object, object.getClass().getName(), Game.class.getName()});
                return null;
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您的 setCurrentGame 方法似乎不会被调用,您可以在 create()方法中手动调用它,以便在犯规后从数据库中更新当前游戏对象已创建。