【问题标题】:JSF update inputText after selectOneMenu choiceJSF 在 selectOneMenu 选择后更新 inputText
【发布时间】:2013-02-22 12:23:51
【问题描述】:

当我从我的 selectOneMenu 中选择另一个皮肤时,我想更改 inputTexts 的值。 一切都很好,我的转换器从菜单中返回了正确的对象,但是 inputTexts 没有更新。

<h:form>
    <h:selectOneMenu id="dropdownSkin"
        value="#{helloBean.currentSkin}" defaultLabel="Select a skin.."
        valueChangeListener="#{helloBean.skinValueChanged}" immediate="true"
        onchange="this.form.submit()" converter="SkinConverter" >
        <f:selectItems value="#{helloBean.mySkinsSI}" var="c"
            itemValue="#{c.value}" />
    </h:selectOneMenu>


    <br />
    <h:inputText id="name" value="#{helloBean.currentSkin.title}"></h:inputText>
    <br />
    <h:inputText id="tcolor" value="#{helloBean.currentSkin.titleBar.textColor}"></h:inputText>
    <br />
    <h:inputText id="bcolor" value="#{helloBean.currentSkin.titleBar.backgroundColorStart}"></h:inputText>
</h:form>

这是我的 Bean 的样子。我对其进行了调试,并且正确设置了 Object currentSkin。现在我需要知道如何更新文本字段的内容。

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

private static final long serialVersionUID = 1L;

private List<ExtendedSkin> mySkins;
private List<SelectItem> mySkinsSI;
private ExtendedSkin currentSkin;

public void skinValueChanged(ValueChangeEvent e) {
    currentSkin = (ExtendedSkin) e.getNewValue();
    FacesContext.getCurrentInstance().renderResponse();
}

public List<ExtendedSkin> getMySkins() {
    mySkins = XMLParser.readExtendedSkins();
    return mySkins;
}

public List<SelectItem> getMySkinsSI() {
    mySkinsSI = new LinkedList<SelectItem>();
    for (ExtendedSkin s : getMySkins()) {
        mySkinsSI.add(new SelectItem(s, s.getTitle()));
    }
    return mySkinsSI;
}

public void setMySkinsSI(List<SelectItem> myItems) {
    this.mySkinsSI = myItems;
}

public ExtendedSkin getCurrentSkin() {
    if (currentSkin == null) {
        currentSkin = getMySkins().get(0);
    }
    return currentSkin;
}

public void setCurrentSkin(ExtendedSkin currentSkin) {
    this.currentSkin = currentSkin;
}
}

【问题讨论】:

  • 尝试将&lt;h:inputText&gt;s 移出表单(作为第一次尝试)。这应该工作
  • 天哪,你做到了,谢谢 :) 将其发布为答案,以便我接受它;)
  • 好的,我会尝试解释问题和可能的解决方案。我在评论中提供的只是一个。
  • 好的,稍后我想我必须再次将 inputTexts 放入表单中,因为我必须使用文本中的值进行预览,也许您也可以尝试使其工作; )
  • 希望我的回答能让您明白一些概念,并展示使用 JSF 2 的优势。

标签: jsf selectonemenu


【解决方案1】:

这里的问题是转换器正在完成填充helloBean.currentSkin 对象的工作,但&lt;h:inputText&gt; 中绑定到helloBean.currentSkin 的值:title、textColor 和backgroundColorStart 将被发送到服务器并替换由converter 加载的实际值。换句话说:

  • 执行转换器并根据所选值构建helloBean.currentSkin。
  • &lt;h:inputText id="name"&gt; 空值被发送到服务器并将注入helloBean.currentSkin.title。其他 2 个 &lt;h:inputText&gt;s 的行为相同。
  • 将使用选定的helloBean.currentSkin 加载视图,并将使用空值加载helloBean.currentSkin.title。其他 2 个 &lt;h:inputText&gt;s 的行为相同。

这个问题有两种可能的解决方案:

  1. 将&lt;h:inputText&gt;s 移到表单外,这样空值就不会发送到服务器。加载视图时,它将保持转换器中加载的值。

    <h:form>
        <h:selectOneMenu id="dropdownSkin"
            value="#{helloBean.currentSkin}" defaultLabel="Select a skin.."
            valueChangeListener="#{helloBean.skinValueChanged}" immediate="true"
            onchange="this.form.submit()" converter="SkinConverter" >
            <f:selectItems value="#{helloBean.mySkinsSI}" var="c"
                itemValue="#{c.value}" />
        </h:selectOneMenu>
    </h:form>
    <br />
    <h:inputText id="name" value="#{helloBean.currentSkin.title}"></h:inputText>
    <!-- rest of Facelets code... -->
    
  2. 由于您在更改下拉列表中的选定值时加载了helloBean.currentSkin,因此您可以使用&lt;h:selectOneMenu&gt; 内的&lt;f:ajax&gt; 标记组件添加ajax 行为,并以更简洁的方式更新字段。我会选择这个解决方案。

    <h:form>
        <!-- Note that there's no need of the onchange JavaScript function -->
        <h:selectOneMenu id="dropdownSkin"
            value="#{helloBean.currentSkin}" defaultLabel="Select a skin.."
            valueChangeListener="#{helloBean.skinValueChanged}" immediate="true"
            converter="SkinConverter" >
            <f:selectItems value="#{helloBean.mySkinsSI}" var="c"
                itemValue="#{c.value}" />
            <f:ajax process="@this" render="name tcolor bcolor" />
        </h:selectOneMenu>
        <br />
        <h:inputText id="name" value="#{helloBean.currentSkin.title}" />
        <h:inputText id="tcolor" value="#{helloBean.currentSkin.titleBar.textColor}" />
        <br />
        <h:inputText id="bcolor"
            value="#{helloBean.currentSkin.titleBar.backgroundColorStart}" />
    </h:form>
    

您可以在this one 等在线教程中了解有关&lt;f:ajax&gt; 的更多信息。

由于您将在页面中使用 ajax 调用,因此您应该将托管 bean 范围从 @SessionScoped 更改为 @ViewScoped。更多信息请点击此处:Communication in JSF 2

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 2013-08-07
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多