我有一个组件在我的视图,我想从我的bean更新。
它只工作一次。如果我按下第二个按钮,什么也不会发生,但是actionlistener中的bean方法被调用了。
我用firebug检查了浏览器日志,没有错误或其他奇怪的东西。
我的Bean在ViewScope中。我使用3.5.
JSFPage:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<h:form id="myRootForm">
<p:inputText id="text1" styleClass="myChangeClass" disabled="true" />
</h:form>
<h:form>
<p:commandButton value="Activate Insert"
actionListener="#{controller.activate()}" update=":myRootForm" />
<p:commandButton value="Deactivate"
actionListener="#{controller.resetFields()}" update=":myRootForm" />
</h:form>
</h:body>
</html>
这是EJB代码:
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;
import java.io.Serializable;
import javax.inject.Named;
@ViewAccessScoped
@Named("controller")
public class Controller {
private UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
public void activate() {
HtmlInputText text = (HtmlInputText)
viewRoot.findComponent("myRootForm:text1");
text.setDisabled(false);
RequestContext.getCurrentInstance().update(text.getClientId());
}
public void deactivate() {
HtmlInputText text = (HtmlInputText)
viewRoot.findComponent("myRootForm:text1");
text.setDisabled(true);
RequestContext.getCurrentInstance().update(text.getClientId());
}
}
* *更新:我使这个例子更好读一些。当我使用"RequestScope"时,它正在工作,但我需要viewScope来保存我的信息。
为了更好地理解为什么我想这样做:我有几个输入组件,我想在几种模式下可用。模式在StyleClass(即myChangeClass)中指明。当我按下按钮时,我遍历表单的组件树并搜索样式类并激活按钮。我不想通过在bean中为每个组件保留一个属性来做到这一点。
我不知道你到底想达到什么目的。我在下面添加了一个例子来帮助你。
XHTML代码
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:form id="myRootForm">
<p:inputText id="text1" disabled="#{tBean.enabled}" />
<p:commandButton value="Activate Insert"
actionListener="#{tBean.enable}" update=":myRootForm" />
<p:commandButton value="Deactivate" actionListener="#{tBean.disable}"
update=":myRootForm" />
</h:form>
</h:body>
</html>
ManagedBean代码
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
@ManagedBean(name = "tBean")
@ViewScoped
public class TestBean {
private boolean enabled;
public void enable(ActionEvent e) {
enabled = false;
}
public void disable(ActionEvent e) {
enabled = true;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}