Re: how populate form from database ?
On 9/21/07, A. Lotfi [EMAIL PROTECTED] wrote: Hi, I need to populate a form with data from database, is there any example. thank you your help is appreciated. Probably are a ton of them. (Here's one for Struts1.x using iBATIS http://www.learntechnology.net/content/ibatis/spring_ibatis.jsp ). In Struts1, I basically do this... 1) Struts action method calls a businessDelegate/serviceClass to get your object. (Just something to abstract a way the dao call to the database - but for rapid development you could even skip this class if you want.) 2) Delegate calls your DAO (data access object) to get back your object from the database. 3) You then populate your ActionForm from this object. There are several ways to do this. First off it's a good idea to make your ActionForm property names match up with the value object names you returned in your object in step 2. (ie an EmployeeActionForm has property firstName, and your Employee value object has property name firstName.) Typicall then in your Action class you can use BeanUtils (commons package) to do: Employee employee = employeeDelegate.getEmployee(employeeID); BeanUtils.copyProperties( employeeActionForm, employee ); You're done. (There are a few caveats to using BeanUtils which can be quite annoying but you can worry about them later:) A side note, I actually have changed where I populate my ActionForm and return the value object. Instead of doing the above in the Action class, I add two methods to my ActionForm... populateForm(Employee emp ) { //takes care of some code like above } populateEmployee(Employee emp) { //populates an Employee object from the form data } I just found it cleaner to do there, especially when some odd cases come up that require some special handling, such as when maybe you can't easily use BeanUtils for everything.) -- Rick
Re: how populate form from database ?
Hi, If you are using struts 1.x then u need to have a database code.Call that from your action get the values. Set them to your action form thats itexample if u want... let your action form be someActionform extends ValidatorForm{ private String name; public String getName(){ return name; } public String setName(String name) { this.name = name; } } public someAction extends Action{ function extends... { someActionForm form = (SomeActionForm)form; uploadName(form); } } thats it... If I am not able to represent idea in right way sorry.. and as Rick said there are many ways to do this..what I showed is the basic way... Regards Viplav public uploadName(SomeActionForm f){ //use connection string to connect to database and return a resultset for example resultset = dbcon.getResult(select name from //name); f.setName(resultSet.getString(name)); } On 9/21/07, A. Lotfi [EMAIL PROTECTED] wrote: Hi, I need to populate a form with data from database, is there any example. thank you your help is appreciated. - Tonight's top picks. What will you watch tonight? Preview the hottest shows on Yahoo! TV. -- Regards Viplav Kallepu
Re: how populate form from database ?
thank you Rick, I will try to do what you said, in my database I have a table that has : Id Title Priority I want to populate a form with lot of rows and present them to the user as : Id Title - five radio buttons how I am gonna do it in jsp file ? thanks your help is appreciated. Rick Reumann [EMAIL PROTECTED] wrote: On 9/21/07, A. Lotfi wrote: Hi, I need to populate a form with data from database, is there any example. thank you your help is appreciated. Probably are a ton of them. (Here's one for Struts1.x using iBATIS http://www.learntechnology.net/content/ibatis/spring_ibatis.jsp ). In Struts1, I basically do this... 1) Struts action method calls a businessDelegate/serviceClass to get your object. (Just something to abstract a way the dao call to the database - but for rapid development you could even skip this class if you want.) 2) Delegate calls your DAO (data access object) to get back your object from the database. 3) You then populate your ActionForm from this object. There are several ways to do this. First off it's a good idea to make your ActionForm property names match up with the value object names you returned in your object in step 2. (ie an EmployeeActionForm has property firstName, and your Employee value object has property name firstName.) Typicall then in your Action class you can use BeanUtils (commons package) to do: Employee employee = employeeDelegate.getEmployee(employeeID); BeanUtils.copyProperties( employeeActionForm, employee ); You're done. (There are a few caveats to using BeanUtils which can be quite annoying but you can worry about them later:) A side note, I actually have changed where I populate my ActionForm and return the value object. Instead of doing the above in the Action class, I add two methods to my ActionForm... populateForm(Employee emp ) { //takes care of some code like above } populateEmployee(Employee emp) { //populates an Employee object from the form data } I just found it cleaner to do there, especially when some odd cases come up that require some special handling, such as when maybe you can't easily use BeanUtils for everything.) -- Rick - Be a better Heartthrob. Get better relationship answers from someone who knows. Yahoo! Answers - Check it out.