Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1090989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:27:19+00:00 2026-05-16T23:27:19+00:00

How I should change following example that after changes values in inputText not disappears

  • 0

How I should change following example that after changes values in inputText not disappears after commandButton was submitted? I’m understanding why it happens, but I don’t know how to fix it.

<h:form>
    <h:selectOneMenu valueChangeListener="#{foo.selectChanges}" onchange="submit()" >
        <f:selectItem itemValue="5" itemLabel="Five"/>
        <f:selectItem itemValue="10" itemLabel="Ten"/>
    </h:selectOneMenu>

    <ui:repeat value="#{foo.repeatItems}" var="item">
        <div>
            <h:inputText value="#{item.value}"/>
        </div>
    </ui:repeat>

    <h:commandButton value="test" action="#{foo.submit}">
    </h:commandButton>
</h:form>

And bean:

public class FooBean {

    private RepeatItem[] repeatItems;

    private String value = "5";

    public String getValue() {
        return value;
    }

    public void setValue(final String value) {
        this.value = value;
    }

    public void submit() {

    }

    public void selectChanges(ValueChangeEvent e) {
        value = (String) e.getNewValue();
    }

    public RepeatItem[] getRepeatItems() {
        repeatItems = new RepeatItem[Integer.parseInt(value)];

        for (int i = 0; i < Integer.parseInt(value); i++) {
            repeatItems[i] = new RepeatItem();
        }

        return repeatItems;
    }

    public void setRepeatItems(final RepeatItem[] repeatItems) {
        this.repeatItems = repeatItems;
    }

    public static class RepeatItem {

        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(final String value) {
            this.value = value;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:27:20+00:00Added an answer on May 16, 2026 at 11:27 pm

    You’d like to put the bean in the view scope to keep it alive in subsequent requests to the same view. You’d also like to prefill in the valueChangeListener method instead of the getter method since a getter method is called multiple times in JSF’s lifecycle and is actually supposed to only return the data. You would also like to use f:ajax here to improve user experience by partial submits instead of submitting the whole page by Javascript’s submit(). Here’s a kickoff example:

    XHTML:

    <h:form>
        <h:selectOneMenu valueChangeListener="#{bean.prefill}" converter="javax.faces.Integer">
            <f:selectItem itemValue="0" itemLabel="Please select" />
            <f:selectItem itemValue="5" itemLabel="Five"/>
            <f:selectItem itemValue="10" itemLabel="Ten"/>
            <f:ajax event="change" render="items" />
        </h:selectOneMenu>
        <h:panelGroup id="items">
            <ui:repeat value="#{bean.items}" var="item">
                <div><h:inputText value="#{item.value}"/></div>
            </ui:repeat>
        </h:panelGroup>
        <h:commandButton value="submit" action="#{bean.submit}" />
        <h:messages />
    </h:form>
    

    Bean:

    @ManagedBean
    @ViewScoped
    public class Bean {
    
        private List<Item> items;
    
        public void prefill(ValueChangeEvent event) {
            Integer count = (Integer) event.getNewValue();
            items = new ArrayList<Item>();
            while (count-- > 0) items.add(new Item());
        }
    
        public void submit() {
            System.out.println(items);
        }
    
        public List<Item> getItems() {
            return items;
        }    
    
    }
    

    Item:

    public class Item {
    
        private String value;
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    
    }
    

    See also:

    • Why JSF calls getters multiple times?
    • The benefits and pitfalls of @ViewScoped
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To reset password, user should go to link that contains token: example.com/password-change/?token=8125b6da86694a19b5d76f30a04c9db8 In view
Why won't the following C code compile? It seems like it should just change
I just starting reading about SEO and realized that I should change my GET
It was recommended to me that, when using an IOC container, I should change
Following this nice example I found, I was trying to create a function that
How should change the date/time settings of the Android mobile phone through Android programming.
I'm wondering if I should change the software architecture of one of my projects.
I am trying to create a status window which content(textbox) should change in a
I'm currently building a simple application on Gjs , which should change the background-image
My problem is, I did coding for a sprite. It should change it should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.