为什么submit后,h:inputTextarea里的值没清掉,manage bean 是request,并且在manage bean 里已经设 value = null

解决方案 »

  1.   

    你的消息我收到,花了一个小时测试了一下:
    观察到的现象是在ManagedBean的事件函数中虽然调用过value=null,但是随后JSF runtime会再次调用setXX方法,并且用旧值作为参数,最后页面渲染的时候,JSF runtime又调用getXX方法。所以你就得不到你想要的结果。这个问题几年前用JSF1.2的时候我也碰到过,当时的解决思路,通过ComponentId找到对应的Component,然后调用setValue方法,并且调用saveState保存。参考下面代码片段:Textarea area=(HtmlInputTextarea)FacesHelper.find("form1:d");
    HtmlInputarea.setValue(data);
    area.saveState(FacesContext.getCurrentInstance());我的FacesHelper的辅助函数:public static UIViewRoot getViewRoot(){
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getViewRoot();
      }  public static UIComponent find(String id){
        UIViewRoot root=getViewRoot();
        return root.findComponent(id);
      }
    第二个解决方案,在我使用PrimeFaces和Ajax的情况下,简单的要求JSF runtime重新渲染TextArea即可。
    看下面的xhtml代码:<p:layoutUnit position="center">  
      <h:inputTextarea id="d" title="请输入要发送的数据" value="#{userHome.data}" required="true" style="height:300px;width:800px"/>
      <p:commandButton type="submit" value="下发数据" actionListener="#{userHome.saveText}" style="margin-right:20px;" update="msg d"/>
      <h:outputText id="msg" value="#{userHome.message}" /> 
    </p:layoutUnit>
    归根到底应该是和生命周期有关联。究竟算不算bug我也不知道,目前JSF功力有限,还需要继续研究。
    :)