1

问题:

PrimeFaces“p:focus”标签没有聚焦
-即,当 xhtml 页面呈现给用户时,没有输入字段处于焦点。

问题:

为什么不?如何让“p:focus”在这个简单的应用程序中正常工作?

(注意:使用 Java 6、Mojarra v2.1.11、PrimeFaces v3.4.2)

索引.jsp

<?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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:p="http://primefaces.org/ui">
    <f:view contentType="text/html">
        <h:head>
            <title>testcal2 - index.xhtml</title>
            <meta charset="utf-8" />
        </h:head>
        <h:body>
            <h:form id="queryForm">

                <f:event type="postValidate" listener="#{testBean.validate}" />

                <h:panelGroup id="msgsx">
                    <p:messages showSummary="true"/>
                </h:panelGroup>

                <p:panel id="queryPanel"  header="Test p:focus..." style="width:100%;">

                    <p:focus context="queryPanel"/>

                    <h:panelGroup id="msgs" style="height:1.5em; text-align: center;display:block">
                        <p:message id="lastName_msg"    for="lastName"     style="display:none;" showSummary="false"/>
                        <p:message id="birthDate_msg"   for="birthDate"    style="display:none;" showSummary="false"/>
                        <p:message id="startDate_msg"   for="startDate"    style="display:none;" showSummary="false"/>
                    </h:panelGroup>

                    <h:panelGroup id="querypanelgroup" style="display:inline-block;">

                        <h:panelGroup>
                            <h:panelGroup style="text-align:right;vertical-align:middle;display:inline-block;width:150px">
                                <p:outputLabel style="margin-right: 5px;"  value="Last Name:" for="lastName"/>
                            </h:panelGroup>
                            <h:panelGroup style="margin-left: 4px; vertical-align:middle;display:inline-block;width:250px;">
                                <p:inputText
                                    id="lastName"
                                    value="#{testBean.parmMap['lastName']}"
                                    requiredMessage="last name required"
                                    size="95"
                                    maxlength="95"
                                    onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(0).show();return false;" >
                                </p:inputText>
                            </h:panelGroup>
                        </h:panelGroup>

                        <br/>
                        <br/>

                        <h:panelGroup>
                            <h:panelGroup style="text-align:right;vertical-align:middle;display:inline-block;width:150px">
                                <p:outputLabel style="margin-right: 5px;"  value="Birth Date:" for="birthDate"/>
                            </h:panelGroup>
                            <h:panelGroup style="margin-left: 4px; vertical-align:middle;display:inline-block;width:250px;">
                                <p:inputText
                                    id="birthDate"
                                    value="#{testBean.parmMap['birthDate']}"
                                    requiredMessage="birth date required"
                                    converter="dpConverter"
                                    styleClass="datePicker"
                                    onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(1).show();$(this).mask('99/99/9999');return false;">
                                    <p:ajax event="change" process="@this" update="@this"/>
                                </p:inputText>
                            </h:panelGroup>
                        </h:panelGroup>

                        <br/>
                        <br/>

                        <h:panelGroup>
                            <h:panelGroup style="text-align:right;vertical-align:middle;display:inline-block;width:150px">
                                <p:outputLabel style="margin-right: 5px;"  value="Start Date:" for="startDate"/>
                            </h:panelGroup>
                            <h:panelGroup style="margin-left: 4px; vertical-align:middle;display:inline-block;width:250px;">
                                <p:inputText
                                    id="startDate"
                                    value="#{testBean.parmMap['startDate']}"
                                    requiredMessage="start date required"
                                    converter="dpConverter"
                                    styleClass="datePicker"
                                    onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(2).show();$(this).mask('99/99/9999');return false;">
                                    <!-- optional to populate another field with same value...
                                    onchange="$('...hashSymbolHere...queryForm\\:endDate').val($(this).val());">
                                    -->
                                    <p:ajax event="change" process="@this" update="@this"/>
                                </p:inputText>
                            </h:panelGroup>
                        </h:panelGroup>

                        <br/>
                        <br/>

                        <p:commandButton
                            id="submit"
                            value="Submit"
                            oncomplete="applyDatePicker();"
                            type="submit"
                            update="@form"
                            process="@form"
                            action="#{testBean.submitQuery}"
                            style="width:150px;"
                            styleClass="button"/>

                        <p:commandButton
                            value="Reset"
                            update="@form"
                            onclick="location.reload();return true;"
                            process="@this"
                            actionListener="#{testBean.reset}"
                            immediate="true"
                            ajax="false"/>

                    </h:panelGroup>
                </p:panel>
            </h:form>

            <h:outputStylesheet library="styles"    name="query.css"      />
            <h:outputScript      library="primefaces" name="/jquery/jquery.js" />
            <h:outputScript      library="primefaces" name="/jquery/plugins/ui/jquery-ui.custom.js" />
            <h:outputScript     library="primefaces" name="/jquery/plugins/inputmask/maskedinput.js" />
            <h:outputScript      library="js" name="index.js" target="head" />

        </h:body>
    </f:view>
</html>

index.js

$(document).ready(function()
{
    applyDatePicker();
});


function applyDatePicker(){

    $('.datePicker').datepicker(
    {
        showOn: 'button',
        buttonText: "Choose",
        showButtonPanel: true,
        showOptions: {direction: 'up'},
        changeYear: true,
        changeMonth: true,
        yearRange: "c-120:c+0"
    });
}

TestBean.java

package aaa.bbb.ccc.war;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("testBean")
@Scope("request")
public class TestBean implements Serializable
{
    public TestBean()
    {
        parmMap = this.getParmMap();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("parmMap", parmMap);
    }

    public void reset(ActionEvent event)
    {
        LinkedHashMap<String, Object> m = new LinkedHashMap<String, Object>();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("parmMap");
        setParmMap(m);
    }

    public String submitQuery()
    {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("hitlistData");

        if (this.getParmMap().isEmpty())
        {
            return "";
        }

        return "/page2.xhtml?faces-redirect=true";
    }

    private static LinkedHashMap<String, Object> parmMap;
    public LinkedHashMap<String, Object> getParmMap()
    {
        LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("parmMap");

        if (null == map)
        {
            map = new LinkedHashMap<String, Object>();
        }
        return map;
    }

    public void setParmMap(LinkedHashMap<String, Object> map)
    {
        parmMap = map;
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("parmMap", parmMap);
    }
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

    public void validate(ComponentSystemEvent e) throws ParseException
    {
        LinkedHashMap parmMap = this.getParmMap();
        UIForm queryForm = (UIForm) e.getComponent();
        FacesContext fc = FacesContext.getCurrentInstance();

        UIInput lastName_c = (UIInput) queryForm.findComponent("lastName");
        String lastName = (String) (lastName_c.getValue());

        UIInput birthDate_c = (UIInput) queryForm.findComponent("birthDate");
        String birthDate = (String) birthDate_c.getValue();

        UIInput startDate_c = (UIInput) queryForm.findComponent("startDate");
        String startDate = (String) startDate_c.getValue();

        try
        {
            if (null != lastName && lastName.trim().length() > 0)
            {
                if (null == birthDate || birthDate.length() < 1)
                {
                    birthDate_c.setValid(false);
                    fc.addMessage(birthDate_c.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, birthDate_c.getRequiredMessage(), birthDate_c.getRequiredMessage()));
                }
                else
                {
                    birthDate_c.setValid(true);
                }
            }
            else
            {
                birthDate_c.setValid(true);
            }

            if (null == startDate || startDate.trim().length() < 1)
            {
                startDate_c.setValid(false);
                fc.addMessage(startDate_c.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, startDate_c.getRequiredMessage(), startDate_c.getRequiredMessage()));
            }
            else
            {
                startDate_c.setValid(true);
            }

            if (fc.getMessageList().size() > 0)
            {
                fc.renderResponse();
            }
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
    }
}
4

1 回答 1

3

这是因为您在false这里重新关注焦点:

<p:inputText
    ...
    onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(0).show();return false;" >
</p:inputText>

返回false会导致事件的默认行为被阻止。

要解决您的问题,只需return false;onfocus输入组件的属性中删除。

<p:inputText
    ...
    onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(0).show();" >
</p:inputText>
于 2013-01-17T18:51:38.887 回答