English 中文(简体)
JSF和f:ajax用于隐藏/显示div
原标题:JSF and f:ajax for hiding/showing div

我正在考虑在我的网络应用程序上制作一个可隐藏/可显示的菜单。在此之前,我为此广泛使用了PHP和AJAX。然而,由于HTML元素id是在JSF框架中重新生成的,我发现这种方法不再可行,至少在我的范围内是这样。

我已经阅读了JSF中的f:ajax标记并尝试实现它。显然我运气不好。它看起来很容易,但我仍然找不到我做错了什么。

我已经准备了一个原型来测试f-ajax标签的功能,但没有成功。这是代码

   ` <h:body>
     <h:outputLabel>
        <h:outputText value="Click A" />
        <f:ajax event="click" render="textA"/>
    </h:outputLabel>
    <h:outputLabel>
        <h:outputText value="Click B" />
        <f:ajax event="click" render="textB"/>
    </h:outputLabel>
    <h:outputLabel>
        <h:outputText value="Click C" />
        <f:ajax event="click" render="textC"/>
    </h:outputLabel>

    <h:outputText id="textA" value="Click A" />
    <h:outputText id="textB" value="Click B" />
    <h:outputText id="textC" value="Click C" />
    </h:body>`

当我点击特定的标签时,什么也没发生。textA、textB和textC元素首先已经呈现出来。我做错了什么?

提前谢谢。

最佳回答

然而,由于HTML元素id是在JSF框架中重新生成的

如果这很重要,只需自己指定固定的id即可。每个组件都有一个id属性。这样,您就可以在适用的情况下使用普通的JS/jQuery框架。

关于具体问题中的问题,这里有一个工作示例,应该让你开始。

<h:form>
    <f:ajax render="text">
        <h:commandLink value="Click A" action="#{bean.setShow( A )}" /><br/>
        <h:commandLink value="Click B" action="#{bean.setShow( B )}" /><br/>
        <h:commandLink value="Click C" action="#{bean.setShow( C )}" /><br/>
    </f:ajax>

    <h:panelGroup id="text">
        <h:outputText value="Clicked A" rendered="#{bean.show ==  A }" />
        <h:outputText value="Clicked B" rendered="#{bean.show ==  B }" />
        <h:outputText value="Clicked C" rendered="#{bean.show ==  C }" />
    </h:panelGroup>
</h:form>

@ManagedBean
@ViewScoped
public class Bean {

    private String show;

    public String getShow() {
        return show;
    }

    public void setShow(String show) {
        this.show = show;
    }

}
问题回答

阅读您的代码,我首先会给出一些一般性建议:

  • As far as I know ajax works on input elements but not on outputLabel. Use h:commandLink or h:commandButton instead
  • outputText should not be inside outputLabel. outputLabel is not necessary here
  • the outputText elements at the bottom are always shown unless you use the rendered attribute: <h:outputText id="textA" value="Click" rendered="false" />. You can use some EL-expression to make the rendered-attribute conditional.

我认为最好从Java EE教程了解jsf的基本概念。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签