0

我试图让一些代码在 XHTML/JSF/Spring 应用程序中工作,通过它我将一个 ID 发送到一个 bean 函数并期望一个字符串作为回报。我还没有找到一个可以理解的教程,也没有找到关于 SO 的任何答案。

XHTML:

<h:form>
    <h:inputText id="inputId" value="#{npBean.idString}"/>
    <a4j:commandButton value="get def" render="out">        
        <f:param value="#{npBean.idString}" name="id" />    
        <f:setPropertyActionListener target="#{npBean.definition}"/>
    </a4j:commandButton>

    <a4j:outputPanel id="out">
        <h:outputText id="outputId" value="#{npBean.def}" 
                                        rendered="#{not empty npBean.def}"/>
    </a4j:outputPanel>

</h:form>

爪哇:

public String getDefinition(int id)
{
    def = this.getXService().getXData(id).getDefinition(); 
    return def;
}

显示的所有值在 bean 中都有它们的 getter 和 setter。

4

1 回答 1

1

我们主要做的事情:

  1. value将组件映射<h:inputText>到托管 bean(称为)中的属性(使用 getter/setter myBean
  2. 通过使用组件的reRender属性<a4j:commandButton>,我们在点击按钮时,指向页面上的哪个组件需要重新渲染(刷新)。
  3. 单击按钮时,invokeService()执行 managedBean 中的方法并更新 managedBean 的其他属性。
  4. <h:panelGroup>下面,我们有几个<h:outputText>组件,并使用rendered我们指定何时必须在页面上显示组件的属性。
  5. 探索托管 bean,唯一需要的是属性的访问器,它保存服务调用的结果。

这是 *.xhtml 示例:

<ui:composition 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:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich">

    <a4j:form>
        <h:panelGrid columns="3">
            <h:outputText value="String value:" />
            <h:inputText value="#{myBean.value}" />
            <a4j:commandButton value="Click" reRender="out">
                <a4j:actionListener listener="#{myBean.invokeService}" />
            </a4j:comandButton>
        </h:panelGrid>
    </a4j:form>
    <rich:spacer height="7"/>
    <br />
    <h:panelGroup id="out">
        <h:outputText value="Service returned: " rendered="#{not empty myBean.result}" />
        <h:outputText value="#{myBean.result}" />
    </h:panelGroup>

</ui:composition>

托管豆:

@ManagedBean(name = "myBean")
@SessionScoped //for example
public class MyBean {
   private String value;

   private String result;

   public String getValue() {
      return value;
   }

   public void setValue(String value) {
      this.value = value;
   }

   public String getResult() {
      return result;
   }

   public void invokeService(ActionEvent actionEvent) {
      this.result = "Hello, " + value + "!";
   }
}

正如@Luiggi 所提到的,访问器方法必须符合以下约定(如果我们假设您private <some-type> property;在托管 bean 中有 a 。)

 public <some-type> getProperty { 
     return property; 
 }

 public void setProperty(<some-type> property) {
     this.property = property:
 }

为了了解RichFaces组件是如何工作的,结合好的代码示例,我建议你打开这个地址并玩弄组件。

于 2013-05-23T17:39:31.477 回答