如何在视图中获得ManagedBean中方法的返回值



我试图在ManagedBean视图中获得方法(提交)的返回值。到目前为止,我所做的是:

Managed Bean:

package net.javabeat.jsf;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
@ManagedBean(name = "HelloBean", eager = true)
@SessionScoped
public class HelloBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
        private String nachName;

    public String getName() {
        return name;
    }
    public String getNachName() {
        return nachName;
    }
    public void setNachName(String nachName) {
        this.nachName = nachName;
    }

    public void setName(String name) {
        this.name = name;
    }
        public String submit(){
        return this.name+" "+this.nachName;
        }
}

and view:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:b="http://bootsfaces.net/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title></title>
    </h:head>
    <h:body>
        <h:outputStylesheet library="css" name="style.css" />
        <h:form class="first" style="padding:25px; margin-right: 500px; overflow: auto;" >
         <b:panel look="primary" title="Suchen"  style="width: 450px; height:250px; background-color: white;">
    <h:panelGrid columns="2" cellpadding="5" style="padding: 25px;">
         <h:outputText value="Name:"/>
        <b:inputText >
        </b:inputText>
        <h:outputText value="Nachname:"/>
        <b:inputText >
        </b:inputText>  
    </h:panelGrid>
             <b:commandButton id="suchen" type="button" action="#{hellobean.submit()}" value="suchen" styleClass="buttonSecond">  
    </b:commandButton>
             </b:panel>
</h:form>
          <h:outputText value="#{hellobean.submit()}"/>
    </h:body>
</html>

但是我无法获得方法的返回值最后一个输出文本。可能我在某个地方犯了一个新手的错误,但我不知道在哪里…有什么想法吗?

在托管bean中定义一个变量:

private String printOnXhtml;

创建getter和setter

然后在托管bean中,将函数更改为-

public String submit(){
printOnXhtml = this.name+" "+this.nachName;
     }

在xhtml中,将outputText更改为-

<h:outputText value="#{hellobean.printOnXhtml}" id="outputText"/>

相关内容

  • 没有找到相关文章

最新更新