数据表页脚中的 JSF UIInput

发布于 2024-07-25 04:35:19 字数 480 浏览 7 评论 0原文

我将(UIInput)添加到动态创建的数据表(UIData)的页脚列(UIColumn)中。 UIData 绑定到 jsp 中的数据表标记。 在数据表中,我只有页眉和页脚,其中页眉具有标签,页脚在可编辑文本框中具有相应的值。 当我更改值并使用 commandButton 提交表单并尝试在操作方法中使用 .getValue() 访问 UIInput 值时,我只获取旧值,而不是页面中更新的值。 我尝试将其绑定到支持 bean 中的一个属性,并检查了 setter 中设置的值。 我注意到正在设置旧值,并且我在页面中更新的值没有反映在操作方法或设置器中。 我尝试使用 .getValue、.getLocalValue、.getSubscribedValue。 这些都没有给我新的价值观。 有什么建议我可能会做什么吗?


我设法通过从 requestParameterMap 中提取值来解决此问题。 如果该问题有解决办法,请告诉我。 麦克道尔 - 感谢您的投入。

I am adding (UIInput) to the footer columns (UIColumn) of a datatable (UIData) created dynamically. The UIData is bound to a datatable tag in the jsp.
In the datatable, I just have headers and footers with the header having the labels and footer having the corresponding value in editable textbox.
When I change the value and submit the form using a commandButton and I try to access the UIInput value using .getValue() in the action method, I just get the old values and not the values updated in the page.
I tried binding it to an attribute in the backing bean and checked the values being set in the setter. I notice that the old values are being set and the values I updated in the page do not reflect in the action method or setter.
I tried using .getValue, .getLocalValue, .getSubmittedValue. None of these give me the new values.
Any suggestions what I might be doing worng?


I managed to workaround by pulling the values from requestParameterMap.
If there is a fix for the issue please do let me know.
McDowell - thanks for your inputs.

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

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

发布评论

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

评论(3

感情旳空白 2024-08-01 04:35:19

我尝试在 Tomcat 上的 MyFaces 1.2.3 下运行您的演示代码代码和 Glassfish 上的 Mojarra 2.0.0 Beta,但无法重现该问题 - 打印了 save() 方法我在字段中输入的值。

(要使用 MyFaces,我必须将 new UIData() 更改为 new HtmlDataTable(),可能是由于它们实现表格渲染器的方式所致,但这是一个很小的更改。 )

我要注意关于 bean 的一些事情:

  • 表 getter 将在每次调用时不断添加列 - 就像在使用服务器端状态保存进行页面刷新时,
  • 在会话 bean 中保留对 UIComponent 的引用通常不是好主意; 您最好使用组件绑定的请求范围
    • 会话 bean 应该实现 Serialized(尽管我意识到并不是每个人都这样做)并且 UIComponent 无法序列化
    • 如果用户打开页面两次,您的组件可能会出现在多个视图中 - 并发问题
    • 根据 规范:当 JSF 创建view,它将使用通过 getter 绑定的组件; 但是,当它恢复视图(提交时)时,它将通过 setter 设置组件,因此保留引用(充其量)是多余的

您可能希望将 getter 更改为如下所示:

private UIData headerDataTable;

public UIData getHeaderDataTable() {
    if (headerDataTable == null) {
        headerDataTable = new UIData();
        getHeaderTable(headerDataTable);
    }
    return headerDataTable;
}

我不相信这些更改不过,如果您仍然遇到问题,请重试更多详细信息 - 您的 JSF 实现、版本以及 javax.faces.STATE_SAVING_METHOD web.xml 中的参数 (如果有的话)。

I tried running your demo code code under MyFaces 1.2.3 on Tomcat and Mojarra 2.0.0 Beta on Glassfish, but was unable to reproduce the problem - the save() method printed the values I entered into the fields.

(To use MyFaces, I had to change new UIData() to new HtmlDataTable(), probably due to how they implement the table renderer, but that is a minor change.)

I will note a couple of things about the bean:

  • the table getter will keep adding columns every time it is called - like on a page refresh with server-side state saving
  • keeping a reference to a UIComponent in a session bean usually is not a good idea; you would be better off using request scope for component bindings
    • session beans are supposed to implement Serializable (though I realize not everyone does this) and UIComponents cannot be serialized
    • your component might end up in multiple views if the user opens the page twice - concurrency issues
    • according to the spec: when JSF creates the view, it will use the component bound via the getter; but, when it restores the view (on submit), it will set the component via the setter, so keeping a reference is (at best) redundant

You might want to change the getter to something like this:

private UIData headerDataTable;

public UIData getHeaderDataTable() {
    if (headerDataTable == null) {
        headerDataTable = new UIData();
        getHeaderTable(headerDataTable);
    }
    return headerDataTable;
}

I am not confident that these changes will fix your issue, though - if you are still having trouble, try again with more details - your JSF implementation, the version, and the value of the javax.faces.STATE_SAVING_METHOD parameter in web.xml (if any).

剪不断理还乱 2024-08-01 04:35:19

实际的代码会执行其他一些处理,但下面的代码应该有助于复制该问题。 在下面的代码中,我希望 TestString 从页面输出修改后的值。 但它只是返回旧值。
下面是 jsp:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
</head>
<f:view>
    <body>
        <h:form styleClass="form" id="form1">
            <h:commandButton value="Save" action="#{TestPageBackingBean.save}"  styleClass="commandExButton"/>
            <h:outputText styleClass="label" value="Header Table"/>
            <h:dataTable binding="#{TestPageBackingBean.headerDataTable}"></h:dataTable>
        </h:form>
    </body>
</f:view>
</html>

下面是 faces 配置:

<managed-bean>
    <managed-bean-name>TestPageBackingBean</managed-bean-name>
    <managed-bean-class>test.jsf.TestPageBackingBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean> 

下面是支持 bean 代码:

package test.jsf;

import java.io.IOException;

import javax.faces.component.UIColumn;
import javax.faces.component.UIData;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;

public class TestPageBackingBean {

  private UIData headerDataTable = new UIData();

  public TestPageBackingBean() {

  }

  public UIData getHeaderDataTable()
    {    
            return getHeaderTable(headerDataTable);
    }       

  public UIData getHeaderTable(UIData dataTable)
    {    
        for (int i=0;i<10;++i)
        {
            dataTable.getChildren().add(getColumn(i));
        }
        return dataTable;
    }

    private UIColumn getColumn(int i)
  {
    UIOutput outputLabelText = new UIOutput();
        UIInput inputFieldText = new UIInput(); 
        UIColumn column = new UIColumn();

        outputLabelText.setValue("Label" + i);
        inputFieldText.setValue("test input" + i);

        column.setHeader(outputLabelText);
        column.setFooter(inputFieldText);
        return column;
    }

    public String save() throws IOException {
            String TestString = "";
            FacesContext ctx = FacesContext.getCurrentInstance();
            if (!ctx.getResponseComplete()) {
                for (int i=0; i<headerDataTable.getChildren().size();++i)
                {
                    TestString = TestString + (String)((UIInput)((UIColumn) headerDataTable.getChildren().get(i)).getFooter()).getValue();
                }
             System.out.println(TestString);
            }
           return "save";
        }

    public void setHeaderDataTable(UIData headerDataTable) {
        this.headerDataTable = headerDataTable;
    }   
}

The actual code does several other processing, but below code should help in replicating the issue. In the below code, I expect the TestString to output the modified values from the page. But it just returns old values.
Below is the jsp:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
</head>
<f:view>
    <body>
        <h:form styleClass="form" id="form1">
            <h:commandButton value="Save" action="#{TestPageBackingBean.save}"  styleClass="commandExButton"/>
            <h:outputText styleClass="label" value="Header Table"/>
            <h:dataTable binding="#{TestPageBackingBean.headerDataTable}"></h:dataTable>
        </h:form>
    </body>
</f:view>
</html>

Below is the faces config:

<managed-bean>
    <managed-bean-name>TestPageBackingBean</managed-bean-name>
    <managed-bean-class>test.jsf.TestPageBackingBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean> 

Below is the backing bean code:

package test.jsf;

import java.io.IOException;

import javax.faces.component.UIColumn;
import javax.faces.component.UIData;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;

public class TestPageBackingBean {

  private UIData headerDataTable = new UIData();

  public TestPageBackingBean() {

  }

  public UIData getHeaderDataTable()
    {    
            return getHeaderTable(headerDataTable);
    }       

  public UIData getHeaderTable(UIData dataTable)
    {    
        for (int i=0;i<10;++i)
        {
            dataTable.getChildren().add(getColumn(i));
        }
        return dataTable;
    }

    private UIColumn getColumn(int i)
  {
    UIOutput outputLabelText = new UIOutput();
        UIInput inputFieldText = new UIInput(); 
        UIColumn column = new UIColumn();

        outputLabelText.setValue("Label" + i);
        inputFieldText.setValue("test input" + i);

        column.setHeader(outputLabelText);
        column.setFooter(inputFieldText);
        return column;
    }

    public String save() throws IOException {
            String TestString = "";
            FacesContext ctx = FacesContext.getCurrentInstance();
            if (!ctx.getResponseComplete()) {
                for (int i=0; i<headerDataTable.getChildren().size();++i)
                {
                    TestString = TestString + (String)((UIInput)((UIColumn) headerDataTable.getChildren().get(i)).getFooter()).getValue();
                }
             System.out.println(TestString);
            }
           return "save";
        }

    public void setHeaderDataTable(UIData headerDataTable) {
        this.headerDataTable = headerDataTable;
    }   
}
温柔女人霸气范 2024-08-01 04:35:19

该问题尚未完全解决。

我在 WAS 6.0 上使用 RSA 7、IBM JSF - Base Faces Support 7.0 和增强型 Faces Components 7.0 javax.faces.STATE_SAVING_METHOD 默认情况下为“服务器”。

我尝试将 STATE_SAVING_METHOD 更改为“客户端”。 它确实在输出中打印了更改后的值,但在 label4 而不是我修改的 label0 中。 在下一次提交时,值从 label4 移动到 label8。 看起来不一致。

The issue is not fully resolved yet.

I use RSA 7, with IBM JSF - Base Faces Support 7.0 and Enhanced Faces Components 7.0 on WAS 6.0 The javax.faces.STATE_SAVING_METHOD was 'server' by default.

I tried changing STATE_SAVING_METHOD to 'client'. It did print the changed value in the output but in label4 instead of label0 which I modified. On the next submit the value moved from label4 to label8. Seemed inconsistent.

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