因此,我有一个支持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:~$
我认为可以,就其本身而言。
后备豆:
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或其他东西。这样,文本输入表单将保留在左侧,而结果将保留在右侧。这是用成分标签完成的吗?或者,第三个“一般客户”有两个子客户?
最佳答案
您需要在请求页面上更改commandButton才能在后备bean中调用一个操作方法:
<h:commandButton id="submit" value="submit" action="#{foo.doAction}" />
在操作方法中设置响应:
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>