更新ui:在p:commandButton中重复

时间:2014-05-19 12:29:39

标签: java jsf uirepeat

我有一个复合组件可以使用ui:repeat来显示一个列表。

以下代码:

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

<composite:implementation>

    <h:panelGroup layout="block" id="panel_topic" styleClass="panel_topic">

        <div class="title_topic">#{msg['tages.title']}</div>

        <ui:repeat value="#{tages._texts}" var="txt">

            <div class="tage">

                <p:commandLink type="button" styleClass="close" aria-hidden="true"
                               action="#{tages.removeText}" update="panel_topic">
                    <f:setPropertyActionListener target="#{tages._tar}" value="#{txt}"/>

                    <h:outputText escape="false" value="&amp;times;"/>

                </p:commandLink>

                <h:inputText id="text" styleClass="text" value="#{txt.text}"></h:inputText>

                <div class="color_item" style="background-color:#{txt.color}"></div>

            </div>

        </ui:repeat>

        <p:commandButton id="add_item" styleClass="add_item" actionListener="#{tages.addsTextss}" value="#{msg['tages.addItem']}"
                         update="panel_topic" >
        </p:commandButton>

    </h:panelGroup>

</composite:implementation>

我的tage bean:

@ManagedBean(name = "tages")
@SessionScoped
public class Tages implements Serializable {

  private int _size = 0;
  private List<Tage> _texts = null;
  private Tage _tar = null;

private String[] _colors = new String[]{"red", "blue", "pink"};

public Tages() {

}

@PostConstruct
public void init() {
    _texts = new ArrayList<Tage>();
}

public void addsTextss() {
    Tage _tage = new Tage();
    _tage.setColor(_colors[_size]);
    _tage.setId(++_size);
    get_texts().add(_tage);
}

public void removeText() {
    if (_size - 1 <= get_tar().getId()) {
        --_size;
    }
    get_texts().remove(get_tar());
}

public List<Tage> get_texts() {
    return _texts;
}

public void set_texts(List<Tage> _texts) {
    this._texts = _texts;
}

public int get_size() {
    return _size;
}

public void set_size(int _size) {
    this._size = _size;
}

public Tage get_tar() {
    return _tar;
}

public void set_tar(Tage _tar) {
    this._tar = _tar;
}
}

我的tage bean:

public class Tage implements Serializable {

private int id=0;
private String text="";
private String color=null;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}
}

点击p:commandButton(add_item)不更新ui:repeat。

如果替换ui:重复c:forEach update right但组件不保存状态。

更新ui的方式:重复?

1 个答案:

答案 0 :(得分:0)

我在使用数据表之前遇到了同样的错误并解决了它并解决了我自己 查看下面的链接

JSF View- returning null on actions do not update the view