会话开始后ConversationScope值未更新

时间:2015-07-01 09:33:56

标签: jsf cdi conversation-scope

我有一个有两部分的jsf页面:一个显示记录列表的表和一个添加新记录的对话框。我添加了一些验证功能,但我无法让它们正常工作。 我需要它:

1)第一次出现对话框时不出现验证错误

2)如果发生任何验证错误,请将其保持打开状态并显示错误消息。

3)如果没有成功执行验证错误和后端,请更新表。

这就是我所拥有的:

<h:body>
<h:form id="form01">
    <p:dataTable id="tbl1" value="#{welcomeController.teams}" var="team" >
        <p:column headerText="Id">
            <h:outputText value="#{team.seq}" />
        </p:column>
        <p:column headerText="Name">
            <h:outputText value="#{team.name}" />
        </p:column>
    </p:dataTable>

    <p:dialog id="teamDialog" closable="false" visible="#{welcomeController.addMode}"
            widgetVar="teamDialog_w" modal="true" resizable="false" draggable="true" 
            header="New Team Detail">
        <p:messages />
        <p:panelGrid columns="2">
            <h:outputText value="Name" />
            <p:inputText id="name" value="#{welcomeController.newTeam.name}" />
        </p:panelGrid>
        <p:commandButton value="Submit" ajax="true" actionListener="#{welcomeController.addNewTeam}" 
            update=":form01"oncomplete="teamDialog_w.hide(); console.log(args);" />
    </p:dialog>

    <p:commandButton value="ADD" actionListener="#{welcomeController.startAdd}" 
            oncomplete="teamDialog_w.show(); console.log(args);" update="teamDialog" />

</h:form>

豆子:

@Named
@ConversationScoped
public class WelcomeController implements Serializable {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private static final long serialVersionUID = 1L;

    private List<TeamDto> teams;

    @Inject SessionManager sessionMan;
    @Inject DatabaseUtil dbCache;
    @Inject TeamService teamService;
    @Inject Conversation conversation;

    private TeamDto newTeam = new TeamDto();

    private boolean addMode = false;

    public List<TeamDto> getTeams() throws IOException {
        if (teams == null || teams.size() == 0) {
            teams = teamService.getAll();
        }
        return teams;
    }

    public void setTeams(List<TeamDto> teams) {
        this.teams = teams;
    }

    public void reload() {
        conversationBegin();
    }

    public void conversationBegin() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    public void conversationEnd() {
        if(!conversation.isTransient()){
            conversation.end();
        }
    }

    public void startAdd() {
        reload();
        newTeam = new TeamDto();
        addMode = true;
    }

    public TeamDto getNewTeam() {
        return newTeam;
    }

    public void setNewTeam(TeamDto newTeam) {
        this.newTeam = newTeam;
    }

    public void addNewTeam() throws IOException, ValidatorException {
        if (newTeam.getName().isEmpty()) {
            sessionMan.addGlobalMessageFatal("INVALID INFO", null);
            return;
        }
        teamService.addTeam(newTeam);
        teams.add(newTeam);
        newTeam = new TeamDto();
        addMode = false;
    }

    public boolean isAddMode() {
        return addMode;
    }

    public void setAddMode(boolean addMode) {
        this.addMode = addMode;
    }
}

我有两个问题:

1)在我提交一个空字符串后,我希望对话框仍然打开(因为addMode为true),但它不是。为什么?

2)如果我把&#34; ADD&#34;像这样的按钮:

<p:commandButton value="ADD" actionListener="#{welcomeController.startAdd}" oncomplete="teamDialog_w.show(); console.log(args);" >
    <f:ajax render="teamDialog" />
</p:commandButton>

至少当我再次打开对话框时,我可以看到错误消息。但在我的代码中,我无法看到错误消息。为什么会这样? Aren他们是等同的?

我的理解有什么不对吗?

请帮忙。非常感谢你。

1 个答案:

答案 0 :(得分:0)

对话框应该在JSF中以自己的形式运行,这是至关重要的。

为防止对话框关闭验证,您可以在Primefaces中使用:

  

if (args &amp;&amp; !args.validationFailed){PF('eventDialog').hide();}

像这样:

<p:commandButton value="ADD"
            actionListener="#{welcomeController.startAdd()}"
            oncomplete="if (args &amp;&amp; !args.validationFailed){PF('teamDialog').hide();}"
            update="your_dialog_formID:messages, other_updated_IDs" />

这里     if (args &amp;&amp; !args.validationFailed){PF('teamDialog').hide();} 您可以obvoiusly添加else子句并执行#3“如果没有验证错误并且后端执行成功,请更新表。”

这里BalusC给出了很好的解释原因:Keep p:dialog open when a validation error occurs after submit