你好世界facelets 2.0导航

时间:2012-04-01 17:15:45

标签: java jsf facelets

所以,我有一个支持bean,Foo和一个带有客户端,请求和响应的模板。客户是多余的,我只想要一个客户。

客户端:

thufir@dur:~$ 
thufir@dur:~$ cat NetBeansProjects/NNTPjsf/web/foo/request.xhtml 
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html">

    <ui:define name="left">

        <h:form>
            <h:inputText size="2" maxlength="50" value="#{foo.bar}" />
            <h:commandButton id="submit" value="submit" action="response" />
        </h:form>
    </ui:define>

    <ui:define name="content">
        <h:outputText value="#{foo.bar}"></h:outputText>
    </ui:define>

</ui:composition>
thufir@dur:~$ 
thufir@dur:~$ cat NetBeansProjects/NNTPjsf/web/foo/response.xhtml 
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html">

    <ui:define name="left">

        <h:form>
            <h:inputText size="2" maxlength="50" value="#{foo.bar}" />
            <h:commandButton id="submit" value="submit" action="response" />
        </h:form>
    </ui:define>

    <ui:define name="content">
        <h:outputText value="#{foo.bar}"></h:outputText>
    </ui:define>

</ui:composition>
thufir@dur:~$ 

我认为这本身就是好的。

支持bean:

package guessNumber;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

@Named
@SessionScoped
public class Foo implements Serializable {

    private String bar = "bar";
    private String response = "response";

    public Foo() {
    }

    /**
     * @return the bar
     */
    public String getBar() {
        return bar;
    }

    /**
     * @param bar the bar to set
     */
    public void setBar(String bar) {
        this.bar = bar;
    }

    /**
     * @return the response
     */
    public String getResponse() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        session.invalidate();
        response = "hmm";
        return response;
    }

    /**
     * @param response the response to set
     */
    public void setResponse(String response) {
        this.response = response;
    }
}

我想要的只是一个客户端,request_response或者其他东西。这样文本输入表单保留在左侧,结果保留在右侧。这是用组合标签完成的吗?或者,第三个“普通客户”有两个子客户端?

1 个答案:

答案 0 :(得分:2)

您需要在请求页面上更改commandButton以调用辅助bean中的操作方法:

<h:commandButton id="submit" value="submit" action="#{foo.doAction}" />

在操作方法中设置响应:

public String doAction() {
  response = "hmm";
  return "response";
}

操作方法的返回值导航到页面/response.xhtml

但你不需要两页。您可以从操作方法返回null以重新加载当前(请求)页面:

public String doAction() {
  response = "hmm";
  return null;
}

然后可以在右侧显示更改的条形和响应值:

<ui:define name="content">
  <h:outputText value="#{foo.bar}"></h:outputText>
  <h:outputText value="#{foo.response}"></h:outputText>
</ui:define>