You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Research labs <re...@yahoo.co.uk> on 2004/06/27 06:25:45 UTC

How to populate fields in a form with data retrieved from a database table...

//***Please note that I am using DispatchAction not
Action***

Please tell me how to populate fields in a form with
data retrieved from a database table.
Please note that I am particular about how to do this
using Struts tags etc. 
//***Please also note that I am using DispatchAction
not Action***

Process flow
============
User presses "get customer" button in customer.jsp,
customer.jsp calls, customerAction.getCustomer,
customerAction.getCustomer method executes
dbInterface.getCustomer.

JSP FILES:
==========

customer.jsp:
=============
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld"
prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld"
prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld"
prefix="bean" %>

<html>
<head>
<title><bean:message key="fieldL.title" /></title>
</head>

...........
</table>
<html:form action="/customer"
name="CustomerForm"
type="com.cargo.gen.CustomerForm" >
<table width="500" border="0">
<tr>
<td width="54"><bean:message
key="fieldL.customerNo"/>:</td>
<td width="37"><html:text property="customerNo"
maxlength="5" size="5"/></td>
<td width="54"><bean:message key="fieldL.firstName"
/>:</td>
<td width="37"><html:text property="firstName" /></td>
<td width="54"><bean:message key="fieldL.lastName"
/>:</td>
<td width="37"><html:text property="lastName" /></td>
</tr>
...
</table>

<html:hidden property="methodToExecute"
value="error"/> 
<SCRIPT>function set(target)
{document.forms[0].methodToExecute.value=target;}</SCRIPT>
<html:submit on click="set('regCustomer')">Register
Customer</html:submit>
<html:submit on click="set('getCustomer')">Get
Customer</html:submit>
<html:submit on click="set('updateCustomer');">Save
Changes</html:submit>

</html:form> 

</body>
</html>






struts-config.xml file:
=======================
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts
Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">







<struts-config>


<!-- ==================================== Data Source
Configuration -->


<data-sources>
<data-source
type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property property="url"
value="jdbc:mysql://localhost/cargo" />
<set-property property="username" value="ola" />
<set-property property="password" value="ola" />
</data-source>
</data-sources>



<!-- ======================================== Form
Bean Definitions -->



<form-beans>
<form-bean name="customerForm"
type="com.cargo.gen.CustomerForm"/>


<!-- sample form bean descriptor for a DynaActionForm
<form-bean

name="logonForm"

type="org.apache.struts.action.DynaActionForm">

<form-property

name="username"

type="java.lang.String"/>

<form-property

name="password"

type="java.lang.String"/>

end sample -->



</form-beans>



<global-forwards>
<forward name="cust" path="/customer"/>
</global-forwards>

<action-mappings>
<!-- Default "Welcome" action -->
<!-- Forwards to Welcome.jsp -->
<action path="/customer" 
type="com.cargo.gen.CustomerAction" 
name="customerForm" 
scope="request" 
parameter="methodToExecute">
<forward name="success" path="/jsp/customer.jsp"/>
<forward name="failure" path="/jsp/failure.jsp"/>
</action>
</action-mappings>


<message-resources
parameter="com.cargo.resources.application"/>

</struts-config>



Action Class:
=============
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import java.util.ArrayList;
import java.util.Iterator;


//***Please note that I am using DispatchAction not
Action***

public class CustomerAction extends DispatchAction {

public ActionForward getCustomer(ActionMapping
mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
));

CustomerForm customerForm = (CustomerForm) form;
copyBeanDataToForm(customerForm,
dbInterface.getCustomer( getDataSource(request),
customerForm ) );

return mapping.findForward("success");
}
...
...
}

//***Please note that I am using DispatchAction not
Action*** 


	
	
		
___________________________________________________________ALL-NEW Yahoo! Messenger - sooooo many all-new ways to express yourself http://uk.messenger.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: How to populate fields in a form with data retrieved from a database table...

Posted by Research labs <re...@yahoo.co.uk>.
Noted.  Thanks.

--- Joe Germuska <Jo...@Germuska.com> wrote: > At 5:25
AM +0100 6/27/04, Research labs wrote:
> >//***Please note that I am using DispatchAction not
> >Action***
> >
> >Please tell me how to populate fields in a form
> with
> >data retrieved from a database table.
> >Please note that I am particular about how to do
> this
> >using Struts tags etc.
> 
> This question is best suited for the Struts Users'
> list. 
> dev@struts.apache.org is designated for discussing
> the *development 
> of* Struts, not *developing with* Struts.
> 
> Joe
> 
> -- 
> Joe Germuska            
> Joe@Germuska.com  
> http://blog.germuska.com    
> "In fact, when I die, if I don't hear 'A Love
> Supreme,' I'll turn 
> back; I'll know I'm in the wrong place."
>     - Carlos Santana
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> dev-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> dev-help@struts.apache.org
> 
>  


	
	
		
___________________________________________________________ALL-NEW Yahoo! Messenger - sooooo many all-new ways to express yourself http://uk.messenger.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: How to populate fields in a form with data retrieved from a database table...

Posted by Joe Germuska <Jo...@Germuska.com>.
At 5:25 AM +0100 6/27/04, Research labs wrote:
>//***Please note that I am using DispatchAction not
>Action***
>
>Please tell me how to populate fields in a form with
>data retrieved from a database table.
>Please note that I am particular about how to do this
>using Struts tags etc.

This question is best suited for the Struts Users' list. 
dev@struts.apache.org is designated for discussing the *development 
of* Struts, not *developing with* Struts.

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"In fact, when I die, if I don't hear 'A Love Supreme,' I'll turn 
back; I'll know I'm in the wrong place."
    - Carlos Santana

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org