【问题标题】:hello world facelets 2.0 navigation你好世界 facelets 2.0 导航
【发布时间】:2012-04-02 22:54:38
【问题描述】:

所以,我有一个支持 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 什么的。这样文本输入表单就在左边,结果在右边。那是用合成标签完成的吗?或者,第三个“普通客户”有两个子客户?

【问题讨论】:

    标签: java jsf facelets


    【解决方案1】:

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

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

    在action方法中设置响应:

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

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

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

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

    然后bar和response的变化值可以显示在右侧:

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

    【讨论】:

    • 只有两个提示:如果您想留在同一页面上,您可以将您的操作方法定义为void doAction() {}。此外,在这种情况下,&lt;h:outputText&gt; 可能不需要;您可以将 EL 直接放在 Facelet 上。希望这会有所帮助;)
    • 答案和表扬看起来很棒,我可能会在几个小时内尝试并回到这里。谢谢!
    猜你喜欢
    • 2021-11-19
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多