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

时间:2015-07-27 18:17:28

标签: jsf jsf-2

我试图在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;
        }
}

并查看:

<?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>

但是我无法设法获取方法内部的返回值 最后的输出文字。 可能我在某个地方犯了一个新手的错误,但我无法弄清楚在哪里......有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在托管bean中定义变量:

private String printOnXhtml;

制作GETTERS和SETTERS。

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

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

在你的xhtml中,将outputText更改为 -

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