Home > FAQs > Cookbook > How do I populate a form bean and get the value using the taglib |

Apache Struts 2 Documentation
How do I populate a form bean and get the value using the taglib
Edit Page Browse Space Add Page Add News

First off, if you're coming from Struts 1, you may feel more comfortable using FormBeans instead of using the Action as your form bean. Be aware, though, that in Webwork you DO have the option of having the properties directly in the Action class. If you want to use a FormBean, here's an example:

    public class TestAction extends ActionSupport {
        private TestBean myBean;

        public TestBean getMyBean() {
            return myBean;
        }

        public void setMyBean(TestBean myBean) {
            this.myBean = myBean;
        }

        protected String doExecute() throws Exception {
            myBean = new TestBean();
            BeanUtil.setProperties(ActionContext.getContext().getParameters(), myBean);
            return SUCCESS;
        }
    }

Then, in your success.jsp, which is mapped as the success result of TestAction in the views.properties or actions.xml (see the docs for how to configure actions and view mappings), you can do this:

    <!-- This will call getMyBean() on your action and put it on the top of the value stack ->
    <webwork:property value="myBean">
    <!- This will call getName() on your TestBean and print it to the page -->
    The name is: <webwork:property value="name"/>
    </webwork:property>

This is a good way to do it if you have several parameters from the TestBean that you want to display, but, if you have just one, like in this case, it's probably better to do this:

    <webwork:property value="myBean/name"/>

NOTE:
As of WW2.2, the following should be used

    <webwork:property value="myBean.name" />

Which will call getMyBean.getName() and print that out to the page.