To overcome the above issues the Struts Framework has provided FormBean.
What is a FormBean?
A Java bean with setter and getter behaviors, for each form a property, thereby implementing validation and reset() behaviors.
Why FormBean?
-> To exchange data between the view and the Action.
-> Proper mechanism implementing DTO(Data Transfer Object) design pattern.
-> Provides implicit Data conversions.
How to use FormBean with respect to Struts Application Development.
public class LoginForm extends org.apache.struts.action.ActionForm{
public void setUname(String uname){
this.uname=uname;
}
public String getUname(){
return this.uname;
}
String uname;
//similarly for password also
}//class
How to configure Formbeans in the struts-config file?
<struts-config>
<form-beans>
<form-bean name="mybean" type="LoginForm"/>
</form-beans>
.....
....
....
</struts-config>
How to associate a Formbean to an Action?
.....
....
</action-mappings>
</struts-config>
From the above configuration we understand that the formbean is associated to the action using the name attributes value.
How to use the form data in our Action Class?
public class LoginAction extends Action{
public ActionForward execute(ActionMapping am,ActionForm af, HttpServletRequest req, HttpServletResponse)throws Exception{
//get the FormBean type
LoginForm lf=(LoginForm)af;
// now use the getXXX() of LoginForm to retrieve the form data.
System.out.println(lf.getUname());
}
}
The next requirement is, implementing Database Connectivity with respect to First Application and we go for using struts framework provided tag libraries to design the views.
What is a FormBean?
A Java bean with setter and getter behaviors, for each form a property, thereby implementing validation and reset() behaviors.
Why FormBean?
-> To exchange data between the view and the Action.
-> Proper mechanism implementing DTO(Data Transfer Object) design pattern.
-> Provides implicit Data conversions.
How to use FormBean with respect to Struts Application Development.
- Your class should be subtype of org.apache.struts.action.ActionForm.
- Write setter and getter behaviors, for each form property.
- Optionally, if required, override the validate(ActionMapping am, HttpServletRequest req) and reset() behaviors.
public class LoginForm extends org.apache.struts.action.ActionForm{
public void setUname(String uname){
this.uname=uname;
}
public String getUname(){
return this.uname;
}
String uname;
//similarly for password also
}//class
How to configure Formbeans in the struts-config file?
<struts-config>
<form-beans>
<form-bean name="mybean" type="LoginForm"/>
</form-beans>
.....
....
....
</struts-config>
How to associate a Formbean to an Action?
<struts-config>
<form-beans>
<form-bean name="mybean" type="LoginForm"/>
</form-beans>
<action-mappings>
<action type="LoginAction" path="/login" name "mybean">
.....
....
</action-mappings>
</struts-config>
From the above configuration we understand that the formbean is associated to the action using the name attributes value.
How to use the form data in our Action Class?
public class LoginAction extends Action{
public ActionForward execute(ActionMapping am,ActionForm af, HttpServletRequest req, HttpServletResponse)throws Exception{
//get the FormBean type
LoginForm lf=(LoginForm)af;
// now use the getXXX() of LoginForm to retrieve the form data.
System.out.println(lf.getUname());
}
}
The next requirement is, implementing Database Connectivity with respect to First Application and we go for using struts framework provided tag libraries to design the views.
No comments:
Post a Comment