SelectBooleanCheckbox 呈现的状态与支持 bean 不匹配

发布于 2024-12-27 22:59:20 字数 4014 浏览 2 评论 0原文

我正在使用 JSF2.0 并正在构建一个向导。我遇到了 SelectBooleanCheckboxes 的问题。工作流程如下:

  • 加载带有复选框的页面(值绑定到支持 bean 中的 SortedMap)。
  • 勾选它们,然后单击下一步。这会增加一个光标,页面使用该光标来确定加载哪个PanelGroup。
  • (正确的)值被保存到 bean 中。
  • 单击返回(光标递减),页面呈现可编辑复选框。第一个复选框未选中(即使该框的绑定变量的值为 true)。

这种基于光标的方法(包含所有向导屏幕)似乎不起作用。但是,如果我稍微修改一下,使上一个/下一个按钮显示不同的 xhtml 页面,这个问题就会消失。

不幸的是我不能这样做。我们将把这个向导插入到模式对话框中,因此访问上一个/下一个新页面将不起作用,

我已经编写了一个这样的小示例(而不是要求您费力地完成整个向导)。

这是 Java 类:

@ConversationScoped
@Named("hashBool")
public class HashBoolTest2 implements Serializable {   

    private static final long serialVersionUID = 1962031429874426411L;

    @Inject private Conversation conversation;

    private List<RestrictionItem> list;
    private SortedMap<String, Boolean> values;

    private int cursor;

    public HashBoolTest2( ) {
        List<String> none = new ArrayList<String>();
        none.add("");

        this.setList( new ArrayList< RestrictionItem >( ) );
        this.getList().add( new RestrictionItem( "a", "a", none ) );
        ...
        this.getList().add( new RestrictionItem( "e", "e", none ) );

        this.setValues( new TreeMap< String, Boolean >() );

        this.setCursor( 0 );
    }

    @PostConstruct
    public void andThis() {
        this.conversation.begin( );
    }

    // getters and setters for instance variables

    @Override
    public String toString() {
        return "Values : " + this.values.toString( ) + " List: " + this.list.toString( );
    }

    public void kill() {
        this.conversation.end( );
    }

    public void doNext(ActionEvent e) {
        this.cursor++;
    }

    public void doPrev(ActionEvent e) {
        this.cursor--;
    }
}

这是 XHTML 片段:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:c="http://java.sun.com/jsp/jstl/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>IGNORED</title>
</head>
<body>
  <ui:composition>
    <h:panelGroup id="container">
      <h:form>
        <!-- edit state -->
        <h:panelGroup id="edit" rendered="#{hashBool.cursor eq 0}">
            <code>
              <h:outputText value="#{hashBool.toString()}" escape="false"/>
            </code>

            <ul>
              <ui:repeat value="#{hashBool.list}" var="elem">
                <li>
                  <h:selectBooleanCheckbox id="elem" value="#{hashBool.values[elem.id]}" title="#{elem.displayName}" />
                  <h:outputLabel for="elem" value="#{elem.displayName}"/>
                </li>
              </ui:repeat>
            </ul>
        </h:panelGroup>

        <!-- view state -->
        <h:panelGroup id="view" rendered="#{hashBool.cursor eq 1}">
          <code>
            <h:outputText value="#{hashBool.toString()}" escape="false"/>
          </code>
        </h:panelGroup>

        <br/>

        <!-- buttons -->
        <h:panelGroup id="buttons">
          <f:ajax render=":container">
            <h:commandButton value="Prev" actionListener="#{hashBool.doPrev}"/>
            <h:commandButton value="Next" actionListener="#{hashBool.doNext}"/>
          </f:ajax>
          <h:commandButton value="Kill" actionListener="#{hashBool.kill()}"/>
        </h:panelGroup>

      </h:form>
    </h:panelGroup>
  </ui:composition>
</body>
</html>

欢迎任何建议! (抱歉,如果这是重复的帖子,我在此处搜索时无法发现类似的内容)

I'm using JSF2.0 and am building up a wizard. I have encountered a problem with SelectBooleanCheckboxes. Here is the workflow:

  • Load page with checkboxes (values are bound to a SortedMap in the backing bean).
  • Tick them, and click next. This increments a cursor, which the page uses to determine which PanelGroup the load.
  • The (correct) values are persisted to the bean.
  • Click back (cursor is decremented) and page renders the editable checkboxes. The first checkbox is not ticked (even though the bound variable holds a value of true for that box).

This cursor-based approach (which contains all of the wizard screens) doesn't seem to work. However, if i slightly modify this so that the prev/next buttons bring up different xhtml pages, this issue disappears.

Unfortunately I cant do this. We are going to plug this wizard into a modal dialog, so visiting a new page on prev/next will not work

I've written up a smallish example of this (rather than asking you to wade through the entire wizard).

Here is the Java class:

@ConversationScoped
@Named("hashBool")
public class HashBoolTest2 implements Serializable {   

    private static final long serialVersionUID = 1962031429874426411L;

    @Inject private Conversation conversation;

    private List<RestrictionItem> list;
    private SortedMap<String, Boolean> values;

    private int cursor;

    public HashBoolTest2( ) {
        List<String> none = new ArrayList<String>();
        none.add("");

        this.setList( new ArrayList< RestrictionItem >( ) );
        this.getList().add( new RestrictionItem( "a", "a", none ) );
        ...
        this.getList().add( new RestrictionItem( "e", "e", none ) );

        this.setValues( new TreeMap< String, Boolean >() );

        this.setCursor( 0 );
    }

    @PostConstruct
    public void andThis() {
        this.conversation.begin( );
    }

    // getters and setters for instance variables

    @Override
    public String toString() {
        return "Values : " + this.values.toString( ) + " List: " + this.list.toString( );
    }

    public void kill() {
        this.conversation.end( );
    }

    public void doNext(ActionEvent e) {
        this.cursor++;
    }

    public void doPrev(ActionEvent e) {
        this.cursor--;
    }
}

Here is the XHTML fragment:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:c="http://java.sun.com/jsp/jstl/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>IGNORED</title>
</head>
<body>
  <ui:composition>
    <h:panelGroup id="container">
      <h:form>
        <!-- edit state -->
        <h:panelGroup id="edit" rendered="#{hashBool.cursor eq 0}">
            <code>
              <h:outputText value="#{hashBool.toString()}" escape="false"/>
            </code>

            <ul>
              <ui:repeat value="#{hashBool.list}" var="elem">
                <li>
                  <h:selectBooleanCheckbox id="elem" value="#{hashBool.values[elem.id]}" title="#{elem.displayName}" />
                  <h:outputLabel for="elem" value="#{elem.displayName}"/>
                </li>
              </ui:repeat>
            </ul>
        </h:panelGroup>

        <!-- view state -->
        <h:panelGroup id="view" rendered="#{hashBool.cursor eq 1}">
          <code>
            <h:outputText value="#{hashBool.toString()}" escape="false"/>
          </code>
        </h:panelGroup>

        <br/>

        <!-- buttons -->
        <h:panelGroup id="buttons">
          <f:ajax render=":container">
            <h:commandButton value="Prev" actionListener="#{hashBool.doPrev}"/>
            <h:commandButton value="Next" actionListener="#{hashBool.doNext}"/>
          </f:ajax>
          <h:commandButton value="Kill" actionListener="#{hashBool.kill()}"/>
        </h:panelGroup>

      </h:form>
    </h:panelGroup>
  </ui:composition>
</body>
</html>

Any suggestions are welcome! (And sorry if this is a double post, i havnt been able to uncover anything similar while searching here)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2025-01-03 22:59:20

主要是为了确保古人的智慧得到正确记录 (http://xkcd.com/979/):事实证明这是 JSF 2.0.2(与 Liferay 6.0 GA 4 捆绑在一起)中的一个错误。请参阅此处了解更多信息:http://java.net/jira/browse/JAVASERVERFACES-1561

Mainly to ensure that the Wisdom of the Ancients remains properly documented (http://xkcd.com/979/): Turns out this is a bug within JSF 2.0.2 (which comes bundled with Liferay 6.0 GA 4). See here for more info: http://java.net/jira/browse/JAVASERVERFACES-1561

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文