You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ricky <Ri...@hotmail.com> on 2003/12/06 05:51:06 UTC
HELP: about to get datasource of struts and pass to logic beans...
hi, there,
with my project i had several queries about get datasource in struts. i know how to get datasource in struts, just as in myAction, use getDataSource method and return DataSource object, and go on to get connection ....
i also know the logic beans between the Controller and Module, so i create a logic bean ,i don't know how to get datasourc of pass the datasource, in some tutorial , i got a way to solve this, create a method getDAO(HttpServletRequest request) and pass the request to the method to get DataSource, i was lost here, i dont' know what should i return in getDAO, should i return a DataSource object or others and in my logic bean i can use the datasource and also get connection , statement and resultset object , there i have to close it after i finish my process ....what do you think of this i did ? in general, when i got a DataSource, i will got a connection from datasource and a PrepareStatement and also a ResultSet , when i finished my process, i can close all object i opened before in final method of try ...catch...experession.. now what should i do to return a proper object in getDAO method and in logic beans i can use the object to process logic business...
i appologized for this ... i hope you can give me some suggestion or flow...... thanx!
now here is my flow of struts framework to pass the datasource to the logic beans... i hope you can reword if you have some good idea...of correct my mistake... :)
myAction :
myAction extends Action() {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
LogicBean bean = new LogicBean();
Object objcet = bean.doMethod(getDAO(request), other param)
}
public DataSource getDAO(HttpServletRequest request) {
DataSource ds = null;
ds = getDataSource(request);
return ds;
}
}
and in my logic bean :
class LogicBean {
public Objct doMethod(DataSource ds, other param) {
try {
Connection conn = ds.getConnection();
PrepareStatment stmt = conn.prepareStatment("SELECT .......");
stmt.setInt(1,....);
ResultSet rs = stmt.executeQuery();
....
return Object.....;
} catch (SQLException ex) {
ex.printStactTrace();
} final {
try {
rs.close();
stmt.close();
conn.close()
} catch (Exception ex) {
ex.printStactTrace();
}
}
return null;
}
}
should i pass the DataSource to my logic beans? otherwise what object should i pass....
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: HELP: about to get datasource of struts and pass to logic beans...
Posted by Vic Cekvenich <ce...@basebeans.com>.
Take a look at a very good db example app that uses Struts on iBatis.com
caled PetStore 3.
.V
Ricky wrote:
> hi, there,
>
> with my project i had several queries about get datasource in struts. i know how to get datasource in struts, just as in myAction, use getDataSource method and return DataSource object, and go on to get connection ....
>
> i also know the logic beans between the Controller and Module, so i create a logic bean ,i don't know how to get datasourc of pass the datasource, in some tutorial , i got a way to solve this, create a method getDAO(HttpServletRequest request) and pass the request to the method to get DataSource, i was lost here, i dont' know what should i return in getDAO, should i return a DataSource object or others and in my logic bean i can use the datasource and also get connection , statement and resultset object , there i have to close it after i finish my process ....what do you think of this i did ? in general, when i got a DataSource, i will got a connection from datasource and a PrepareStatement and also a ResultSet , when i finished my process, i can close all object i opened before in final method of try ...catch...experession.. now what should i do to return a proper object in getDAO method and in logic beans i can use the object to process logic business...
>
> i appologized for this ... i hope you can give me some suggestion or flow...... thanx!
>
> now here is my flow of struts framework to pass the datasource to the logic beans... i hope you can reword if you have some good idea...of correct my mistake... :)
>
> myAction :
>
> myAction extends Action() {
> public ActionForward execute(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest request,
> HttpServletResponse response) {
> LogicBean bean = new LogicBean();
> Object objcet = bean.doMethod(getDAO(request), other param)
> }
>
> public DataSource getDAO(HttpServletRequest request) {
> DataSource ds = null;
> ds = getDataSource(request);
> return ds;
> }
> }
>
> and in my logic bean :
>
> class LogicBean {
> public Objct doMethod(DataSource ds, other param) {
> try {
> Connection conn = ds.getConnection();
> PrepareStatment stmt = conn.prepareStatment("SELECT .......");
> stmt.setInt(1,....);
> ResultSet rs = stmt.executeQuery();
> ....
> return Object.....;
> } catch (SQLException ex) {
> ex.printStactTrace();
> } final {
> try {
> rs.close();
> stmt.close();
> conn.close()
> } catch (Exception ex) {
> ex.printStactTrace();
> }
> }
> return null;
> }
> }
>
> should i pass the DataSource to my logic beans? otherwise what object should i pass....
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Using Input Button inside a Iterate Tag
Posted by Shantanu Deo <sd...@nc.rr.com>.
What is the mechanics of using the Input button inside of the Iterate tag.
I have my JSP as
<html:form action="/action/dispatchEmployeeAction.do">
<table>
<TR>
<th>Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Telephone Count</th>
<th>Address</th>
</TR>
<logic:iterate name="employeeList" id="employee" indexId="empNumber"
type="com.innovate.test.Employee">
<TR>
<TD><bean:write name="empNumber"/></TD>
<TD><bean:write name="employee" property="fname"/></TD>
<TD><bean:write name="employee" property="lname"/></TD>
<TD><bean:write name="employee" property="telephoneNumberCount"/></TD>
<TD><bean:write name="employee" property="fullAddress"/></TD>
<TD>
<html:image page="/images/Delete_Button.gif"
property="employee_delete" indexed="true" />
</TD>
<TD>
<html:image page="/images/Update_Button.gif"
property="employee_update" indexed="true" />
</TD>
</logic:iterate>
</table>
<html:submit value="Create Employee" property="theAction"/>
</html:form>
What should my corresponding code in Action be to recognize which of the
delete/update buttons are clicked.
Thanks in advance
Shantanu
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: HELP: about to get datasource of struts and pass to logic beans...
Posted by Caroline Jen <ji...@yahoo.com>.
There are lots of classes involved. I will give you
an example:
1. my LogonAction calls EditorService.java (business
delegate)
2. EditorService.java calls MySQLEditorDAO.java (data
access object implements EditorDAO.java, which is a
data access interface)
3. the MySQLEditorDAO.java returns EditorBean.java (a
Java bean with three properties)
Here is my LogonAction.java:
package org.apache.artimus.logon;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.artimus.lang.Tokens;
public final class LogonAction extends Action {
public ActionForward execute(ActionMapping
mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws java.lang.Exception {
// Obtain username and password from web tier
String username = ((LogonForm)
form).getUsername();
String password = ((LogonForm)
form).getPassword();
EditorService service = new EditorService();
EditorBean editor = service.findEditorData(
username );
HttpSession session = request.getSession();
session.setAttribute( "editor", editor );
// Log this event, if appropriate
if (servlet.getDebug() >= Tokens.DEBUG) {
StringBuffer message =
new StringBuffer("LogonAction: User
'");
message.append(username);
message.append("' logged on in session ");
message.append(session.getId());
servlet.log(message.toString());
}
// Return success
return (mapping.findForward(Tokens.VALID));
}
} // End LogonAction
Here is the EditorService.java:
package org.apache.artimus.logon;
import org.apache.artimus.logon.dao.*;
public class EditorService
{
EditorDAO ed = new MySQLEditorDAO();
public EditorBean findEditorData( String username )
{
return ed.findEditor( username );
}
}
Here is the EditorDAO.java:
package org.apache.artimus.logon.dao;
import org.apache.artimus.logon.EditorBean;
import
org.apache.artimus.logon.exceptions.EditorDAOSysException;
public interface EditorDAO.java
{
public EditorBean findEditor( String username )
throws EditorDAOSysException;
}
Here is the MySQLEditorDAO.java:
package org.apache.artimus.logon.dao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import org.apache.artimus.logon.EditorBean;
import
org.apache.artimus.logon.exceptions.EditorDAOSysException;
import org.apache.artimus.ConnectionPool.DBConnection;
public class MySQLEditorDAO implements EditorDAO
{
// Here the return type is EditorBean
public EditorBean findEditor( String username )
throws EditorDAOSysException
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
conn = DBConnection.getDBConnection();
stmt = conn.createStatement();
String query = "SELECT user_role,
journal_category FROM members WHERE user_name = '" +
username + "'";
rs = stmt.executeQuery( query );
if (rs.next())
{
return new EditorBean( username,
rs.getString( "user_role" ), rs.getString(
"journal_category" ) );
}
else
{
System.out.println( "invalid user name" );
return null;
}
}
catch (SQLException se)
{
throw new
EditorDAOSysException("SQLException: " +
se.getMessage());
}
finally
{
if ( conn != null )
{
try
{
rs.close();
rs = null;
stmt.close();
stmt = null;
conn.close();
}
catch( SQLException sqlEx )
{
System.out.println( "Problem occurs
while closing " + sqlEx );
}
conn = null;
}
}
}
}
Here is the EditorBean.java:
package org.apache.artimus.logon;
import org.apache.artimus.logon.dao.*;
public class EditorBean {
private String username;
private String userrole;
private String keyValue;
static EditorDAO ed = new MySQLEditorDAO();
public EditorBean() {}
public EditorBean( String username, String
userrole, String keyValue )
{
setUsername( username );
setUserrole( userrole );
setKeyValue( keyValue );
}
public String getUsername() {
return username;
}
public void setUsername( String username ) {
this.username = username;
}
public String getUserrole() {
return userrole;
}
public void setUserrole( String userrole ) {
this.userrole = userrole;
}
public String getKeyValue() {
return keyValue;
}
public void setKeyValue( String keyValue ) {
this.keyValue = keyValue;
}
}
Here is the DBConnection.java, which gets a connection
object from a connection pool (you have to configure
your server's connection pool):
package org.apache.artimus.ConnectionPool;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConnection
{
public static Connection getDBConnection() throws
SQLException
{
Connection conn = null;
try
{
InitialContext ctx = new InitialContext();
DataSource ds = ( DataSource ) ctx.lookup(
"java:comp/env/jdbc/MySQLDB" );
try
{
conn = ds.getConnection();
}
catch( SQLException e )
{
System.out.println( "Open connection
failure: " + e.getMessage() );
}
}
catch( NamingException nEx )
{
nEx.printStackTrace();
}
return conn;
}
}
Here is the exception handling class:
package org.apache.artimus.logon.exceptions;
/**
* EditorDAOSysException is an exception that extends
the standard
* RunTimeException Exception. This is thrown by the
DAOs of the article
* component when there is some irrecoverable error
(like SQLException)
*/
public class EditorDAOSysException extends
RuntimeException {
/**
* Constructor
* @param str a string that explains what the
exception condition is
*/
public EditorDAOSysException (String str) {
super(str);
}
/**
* Default constructor. Takes no arguments
*/
public EditorDAOSysException () {
super();
}
}
-Caroline
--- Ricky <Ri...@hotmail.com> wrote:
> hi, there,
>
> with my project i had several queries about get
> datasource in struts. i know how to get datasource
> in struts, just as in myAction, use getDataSource
> method and return DataSource object, and go on to
> get connection ....
>
> i also know the logic beans between the
> Controller and Module, so i create a logic bean ,i
> don't know how to get datasourc of pass the
> datasource, in some tutorial , i got a way to solve
> this, create a method getDAO(HttpServletRequest
> request) and pass the request to the method to get
> DataSource, i was lost here, i dont' know what
> should i return in getDAO, should i return a
> DataSource object or others and in my logic bean i
> can use the datasource and also get connection ,
> statement and resultset object , there i have to
> close it after i finish my process ....what do you
> think of this i did ? in general, when i got a
> DataSource, i will got a connection from datasource
> and a PrepareStatement and also a ResultSet , when i
> finished my process, i can close all object i opened
> before in final method of try
> ...catch...experession.. now what should i do to
> return a proper object in getDAO method and in logic
> beans i can use the object to process logic
> business...
>
> i appologized for this ... i hope you can give me
> some suggestion or flow...... thanx!
>
> now here is my flow of struts framework to pass the
> datasource to the logic beans... i hope you can
> reword if you have some good idea...of correct my
> mistake... :)
>
> myAction :
>
> myAction extends Action() {
> public ActionForward execute(ActionMapping
> mapping,
> ActionForm form,
> HttpServletRequest
> request,
> HttpServletResponse
> response) {
> LogicBean bean = new LogicBean();
> Object objcet =
> bean.doMethod(getDAO(request), other param)
> }
>
> public DataSource getDAO(HttpServletRequest
> request) {
> DataSource ds = null;
> ds = getDataSource(request);
> return ds;
> }
> }
>
> and in my logic bean :
>
> class LogicBean {
> public Objct doMethod(DataSource ds, other
> param) {
> try {
> Connection conn = ds.getConnection();
> PrepareStatment stmt =
> conn.prepareStatment("SELECT .......");
> stmt.setInt(1,....);
> ResultSet rs = stmt.executeQuery();
> ....
> return Object.....;
> } catch (SQLException ex) {
> ex.printStactTrace();
> } final {
> try {
> rs.close();
> stmt.close();
> conn.close()
> } catch (Exception ex) {
> ex.printStactTrace();
> }
> }
> return null;
> }
> }
>
> should i pass the DataSource to my logic beans?
> otherwise what object should i pass....
>
>
>
>
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
>
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org