获取动态生成的组件ID和值

时间:2022-10-17 20:07:47

I have a questionnaire form in which components (questions) are generated all programatically in my backing bean.

我有一个问卷表单,其中组件(问题)在我的支持bean中以编程方式生成。

In form submit event I need to collect all user inputs and store them in db.

在表单提交事件中,我需要收集所有用户输入并将其存储在db中。

But JSF does not recognize the dymanically generated components and only finds the ones that are in my Facelets page which are my panelgrid and submit button. This is my submit() method.

但JSF无法识别dymanically生成的组件,只能找到我的Facelets页面中的那些是我的panelgrid和submit按钮。这是我的submit()方法。

   public boolean submit() {
        UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
        UIComponent formComponent = viewRoot.findComponent("mainForm");  //form id
        HtmlForm form = (HtmlForm)formComponent;
        List<UIComponent> componentList = form.getChildren();
        for(int p=0; p<componentList.size(); p++) {
            UIComponent component = componentList.get(p);
                System.out.println("The Component ID is:"+component.getId());
        }
        return true;
}

So does anyone know where I can hunt my components other than the method above?

那么除了上面的方法之外,有谁知道我可以在哪里寻找我的组件?

1 个解决方案

#1


0  

This is not the right way to collect submitted values.

这不是收集提交值的正确方法。

You should instead bind the component's value attribute to a bean property. E.g.

您应该将组件的value属性绑定到bean属性。例如。

UIInput input = new HtmlInputText();
input.setId("input1");
input.setValueExpression("value", createValueExpression("#{bean.input1}", String.class));
form.getChildren().add(input);

This way JSF will just update the bean property the usual way.

这样JSF就会以通常的方式更新bean属性。

private String input1; // +getter+setter

public void submit() {
    System.out.println(input1); // Look, JSF has already set it.
}

You could make use of a Map<String, Object> property to bring some more dynamics.

您可以使用Map 属性来带来更多动态。 ,object>

See also:

#1


0  

This is not the right way to collect submitted values.

这不是收集提交值的正确方法。

You should instead bind the component's value attribute to a bean property. E.g.

您应该将组件的value属性绑定到bean属性。例如。

UIInput input = new HtmlInputText();
input.setId("input1");
input.setValueExpression("value", createValueExpression("#{bean.input1}", String.class));
form.getChildren().add(input);

This way JSF will just update the bean property the usual way.

这样JSF就会以通常的方式更新bean属性。

private String input1; // +getter+setter

public void submit() {
    System.out.println(input1); // Look, JSF has already set it.
}

You could make use of a Map<String, Object> property to bring some more dynamics.

您可以使用Map 属性来带来更多动态。 ,object>

See also: