页面提交后<p:focus>停留在同一页面上不起作用</p:focus>

时间:2013-07-30 17:13:43

标签: jsf primefaces

我想在包含带输入字段的表单的页面上使用Primefaces p:focus标记。在第一次提交之前,焦点设置正确。如果我通过p:commandButton提交表单并返回到同一页面以显示错误结果(使用FacesMessage消息),则无法定义焦点(在Firefox中,光标在不知名的地方闪烁)。

如何实现定义的焦点状态?我可以在提交后选择哪个元素具有焦点吗?

以下是一些代码详细信息:

JSF页面:

<p:panel header="Login">
    <h:form>
        <p:focus for="usernameInput" />
        <p:panelGrid columns="2">
            <p:outputLabel for="usernameInput">User name</p:outputLabel>
            <p:inputText id="usernameInput" value="#{loginBean.username}" />
            <p:outputLabel for="passwordInput">Password</p:outputLabel>
            <p:password id="passwordInput" value="#{loginBean.password}" />
        </p:panelGrid>
        <p:commandButton value="Login" action="#{loginBean.login}" />
    </h:form>
</p:panel>
<div>
    <p:messages id="messages" globalOnly="false" autoUpdate="true" showDetail="true" showSummary="true" closable="false" />
</div>

Java类的相关部分:

public String login() {

    FacesContext fc = FacesContext.getCurrentInstance();

    HttpServletRequest req =
            (HttpServletRequest) fc.getExternalContext().getRequest();

    try {
        req.login(username, password);
        return "/home?faces-redirect=true";
    } catch (ServletException e) {
        fc.addMessage(
                null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
                    "error message")));
    }

    return null;
}

0 个答案:

没有答案