1

当鼠标移动到界面的特定元素上时,如何在 JSF 应用程序中显示简单的消息?我已经尝试过这段代码,但它不起作用,没有显示任何消息:

JSF 文件:

<h:form id ="f">
<h:selectManyCheckbox onmouseover="#{hello.message()}" layout="pageDirection" border="1"  value="#{hello.customersSelect}">
    <f:selectItems value="#{hello.customers}"></f:selectItems>
</h:selectManyCheckbox><br />
<h:commandButton action="response.xhtml" value="Click me" />
</h:form>

支持 bean Hello (ManagedBean) 包含方法 message() 如下:

public void message(){
    FacesContext.getCurrentInstance().addMessage("f", new FacesMessage("Done"));
}

我想我应该在某处添加一个标签 h:message,但我无法完成它,尽管我很努力。有什么提示吗?

4

3 回答 3

1

你应该在 javascript 中处理它,因为 mouseOver 是一个 dom 事件

<h:head>    
<h:outputScript library="js" name="rollover.js" />
[...]
<h:form id ="f">
<h:selectManyCheckbox onmouseover="mouseOn('foobar')" layout="pageDirection" border="1"  value="#{hello.customersSelect}">

和 rollover.js :

function mouseOn(text) {
   alert(text)
}
于 2013-03-06T15:29:09.713 回答
1

为了满足您的好奇心,请使用<f:ajax event="mouseover" listener="#{hello.messageListener}" />

JSF 代码

<h:selectManyCheckbox layout="pageDirection" border="1"
    value="#{hello.customersSelect}">
    <f:selectItems value="#{hello.customers}"></f:selectItems>

    <f:ajax event="mouseover" listener="#{hello.messageListener}" render="messages" />
</h:selectManyCheckbox>
<br />
<h:messages id="messages" />

你好 bean 代码

@ManagedBean
@ViewScoped
public class Hello {
    //attributes, constructor and other methods

    public void messageListener(AjaxBehaviorEvent event) { 
        System.out.println("OnMouseOver ajax event.");
        FacesContext.getCurrentInstance().addMessage(
            null, new FacesMessage("Done"));
    }
}

但更重要的是,您确定这是您真正问题的解决方案吗?最好指定功能需求以获得更好的帮助。

于 2013-03-06T15:32:26.083 回答
0

如果您只想显示一条静态消息,该消息在显示页面时不会更改:

<h:form>
    <h:selectManyCheckbox title="#{hello.message}" layout="pageDirection"
        border="1" value="#{hello.customersSelect}">
        <f:selectItems value="#{hello.customers}"></f:selectItems>
    </h:selectManyCheckbox><br />
    <h:commandButton action="response.xhtml" value="Click me" />
</h:form>

还有豆子:

public String getMessage() {
    return "Done";
}

根本没有 JavaScript ;-)

于 2013-03-06T15:44:21.283 回答