Struts Tutorial Home

Struts Home

JSP Resources

Community

Site

Configuring Form Bean in struts-config.xml

text zoom


Adding a form bean is a two step process. First of all we need to declare the form bean in the <form-beans> tag. Then we can associate this form bean to as many actions as we want. To declare the form bean,add the following line in the <form-beans> tag.


<form-bean name="loginForm" type="com.visualbuilder.struts.beans.User" />


Now we will associate this form bean with the LoginAction. Update the action tag in action-mappings so that it becomes like this.


 <action     


      name="loginForm"


      path="/login"


      scope="request"


      type="com.visualbuilder.struts.action.LoginAction"


      validate="false">


      <forward name="failure" path="/index.jsp" />


      <forward name="success" path="/manageusers.jsp" />


 </action>


Now update the LoginAction to retrieve form values from form bean instead of request object though they are still available in the request object s well. Execute method should look like this now.


public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
            User userForm = (User)form;
            String user = userForm.getUserId();
            String password = userForm.getPassword();
            if(user!=null && password!=null && user.equals("admin") && password.equals("admin"))
                return mapping.findForward("success");
            return mapping.findForward("failure");
}


Again deploy and run the project. You should get the same behavior as you got earlier.

                    

Copyright © 2010 VisualBuilder. All rights reserved