【问题标题】:"Value" not updated in JSF2-AJAX env“值”未在 JSF2-AJAX 环境中更新
【发布时间】:2012-08-26 15:07:36
【问题描述】:

我的环境:W7、Eclipse Indigo 3.7.2、Tomcat 6.0.35、jdk 1.7.02、jsf 2.0(Mojarra 2.03)、JSTL 1.2。 尝试为更复杂的情况实现级联下拉列表,作为最终试验,我编写了一个非常简单的 xhmtl,它从 selecOneMenu 中选择一个项目,并通过 f:ajax 条目立即将其内容放入 outputText 中。菜单显示正确,但是,当我更改选择时,outputText 没有任何反应。我不知道哪里错了:你能帮帮我吗? 谢谢。

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                                    
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">                                    

  <html xmlns="http://www.w3.org/1999/xhtml"                                                        
    xmlns:ui="http://java.sun.com/jsf/facelets"                                                     
    xmlns:h="http://java.sun.com/jsf/html"                                                          
    xmlns:f="http://java.sun.com/jsf/core"                                                          
    xmlns:p="http://primefaces.org/ui">                                                             


  <ui:composition template="/WEB-INF/template/BasicTemplate.xhtml">                                 
    <ui:define name="content">                                                                      
        <f:view>                                                                                      
        <h:form>                                                                                      
            <h:selectOneMenu id="firstDropDown" value="#{twoMenusBean.first}" style="width: 110px; ">   
                    <f:selectItems value="#{twoMenusBean.items}" />                                         
                    <f:ajax render="secondDropDown" immediate="true" ></f:ajax>                             
            </h:selectOneMenu>                                                                          


                <h:outputText id="secondDropDown" value="#{twoMenusBean.first}" ></h:outputText>          

            </h:form>                                                                                   
        </f:view>                                                                                     
    </ui:define>                                                                                    
  </ui:composition>                                                                                 
  </html>                                                                                           

豆子:

package myJava;




  @ManagedBean (name="twoMenusBean")                                   
  @SessionScoped                                                       

  public class twoMenusBean  {                                         

    private List<String> menu1List = new ArrayList<String>();          
    private String first;                                              

    public  twoMenusBean(){                                            

            menu1List.add("item 1");                                       
            menu1List.add("item 2");                                       
            first ="item 1";                                               
    }                                                                  
    public String getFirst(){                                          
        return first;                                                    
    }                                                                  
    public void setFirst(String first){                                
        this.first = first;                                              
    }                                                                  

    public List<SelectItem> getItems(){                                
        List<SelectItem> x = new ArrayList<SelectItem>();                
        for (int i = 0; i < menu1List.size() ;i++) {                     
            x.add(new SelectItem(menu1List.get(i),menu1List.get(i)));      
        }                                                                
        return x;                                                        
    }                                                                  
  }                                                                    

【问题讨论】:

  • 您的 ManagedBeanSessionScoped 是从 import javax.faces.bean.... 导入的吗?没有immediate="true" 可以吗?
  • 导入是:import javax.faces.bean.ManagedBean;导入 javax.faces.bean.SessionScoped。 “立即”没有效果:在任何情况下行为都是一样的。
  • 看看维基标签 stackoverflow.com/tags/selectonemenu/info ,按照那里的说明... stackoverflow.com/tags/selectonemenu/info 并删除行 first ="item 1";
  • 同样的结果。我添加了一行“" 并删除了“第一个”初始化:outpuText 现在导致 (?)空行。
  • &lt;f:selectItem itemValue="#{null}" itemLabel="-- select one --"&gt;&lt;/f:selectItem&gt; 替换为&lt;f:selectItem itemLabel="-- select one --" noSelectionOption="true"/&gt;

标签: ajax jsf-2


【解决方案1】:

删除immediate="true":它有跳过字段更新的副作用(不会调用setFirst)。

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 2011-06-29
    • 2013-08-31
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多