You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Barry Volpe <st...@childrencare.com> on 2003/09/25 20:33:26 UTC

Here is an example TilesAction

----- Original Message ----- 
From: "Barry Volpe" <st...@childrencare.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Tuesday, September 23, 2003 3:41 PM
Subject: Re: Data Source issue in tiles controller execute()


> TYPO:
>
> This:
>
> <tiles:insert definition=".penguin.view" flush="true" />
>
> should be (correct name of definition):
>
> <tiles:insert definition=".sample.view" flush="true" />
>
>
> ----- Original Message ----- 
> From: "Barry Volpe" <st...@childrencare.com>
> To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> Sent: Tuesday, September 23, 2003 3:31 PM
> Subject: Re: Data Source issue in tiles controller execute()
>
>
> Found a solution!
>
> Here is a simple example of HOW TO EXTEND THE TILESACTION which not only
> allows tile definitions but just as important allows using the DataSource
> functionalilty of the Struts action(the implement Controller would not
allow
> the Data Source functionalilty):
>
> public final class LoadUserCategoriesAction extends TilesAction{
> public ActionForward execute( ComponentContext tilesContext,
>                                                     ActionMapping mapping,
>                                                     ActionForm form,
>                                                     HttpServletRequest
> request,
>                                                     HttpServletResponse
> response) throws Exception
> {
>
> //This just sets up a datasource
> Statement stmt = null;
> ResultSet rs = null;
> DataSource dataSource;
> Connection conn=null;
> try {
> dataSource = getDataSource(request);
> conn = dataSource.getConnection();
> stmt = conn.createStatement();
> //Example Query
> rs = stmt.executeQuery("select * from users where email=\'"
> + userName + "'");
> if ( rs.next() ) {
> //get parameters
> rs.getString("userid");
> }
> else {
> throw new Exception("User " + userName + " not found!");
> }
> } catch (SQLException sqle) {
> getServlet().log("Connection.process", sqle);
> } finally {
> //enclose this in a finally block to make
> //sure the connection is closed
> try {
> conn.close();
> } catch (SQLException sqle) {
> getServlet().log("Connection.close", sqle);
> }
> }
>
> //Here we have only a TEST but could put the database results into the
> tilesContext
>
> tilesContext.putAttribute("TEST" , "TEST THIS!");
>
> return null;
> }
>
>
> }
>
>
> Here is what goes in the struts-config:
>
> <action path="/Test"
>       type="com.example.LoadUserCategoriesAction">
>    </action>
>
> Here is what goes in the tiles-def:
>
> <definition name=".sample.view" path="/tiles/sampleview.jsp"
>  controllerUrl ="/Test.do">
> </definition>
>
> Here is what goes in the sampleview.jsp
>
> <tiles:importAttribute/>
>
> <tiles:getAsString name="TEST"/>
>
>
>
> Now make the call (insert) in any desired jsp page.  This insert can be
used
> in several jsp pages and the tile view will be displayed.
>
>
> <tiles:insert definition=".penguin.view" flush="true" />
>
>  NOTES:
>
> I found that using controllerClass which would have been a bit easier to
use
> would not allow me to use the getDataSource(request) function even when I
> extended the Action class (required for getDataSource).  I could not pass
> the database results from the Action class to the Controller class.
>
>
> Hope this helps anyone looking to use the tilesAction with database
> connectivity!
>
> Barry
>
>
>
>
>
>
>
> ----- Original Message ----- 
> From: "Barry Volpe" <st...@childrencare.com>
> To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> Sent: Tuesday, September 23, 2003 10:35 AM
> Subject: Data Source issue in tiles controller execute()
>
>
> > Hi,
> >
> > In the following (SEE CODE BELOW) when I attempt to inititialize
> >
> > getDataSource(request) in the Tiles Controller execute method it
> > causes a null in the fowarding JSP which looks for the string "TEST"
> > initialized by this:
> >
> >  tilesContext.putAttribute("TEST" , "TEST");
> >
> > If I remove the getDataSource(request) then the "TEST" string
> >  tilesContext.putAttribute("TEST" , "TEST");
> > displays correctly in the fowarding jsp.
> >
> > It appears that the getDataSource(request); does not work
> > in the tilesController execute but works in the action execute.
> >
> > Does anyone know why?
> >
> > In addition the problem I have is I want to query the database for data
> > and put it in the tiles controller execute so I can pass it to the
> fowarding
> > JSP.
> >
> > So I could query the database in the Action execute() and pass the data
to
> > the
> > tiles controller execute() and display in the fowarding jsp.
> >
> > I have tried declaring a:
> > static private String mystring;  outside the Action execute() and the
> tiles
> > controller execute()
> >
> > but when I try to update the string in the Action execute() the string
> > (mystring) does not show
> > the new string value in the tiles controller execute() method.
> >
> >
> > In summary either the database query is performed in the Action execute
> and
> > data fowarded in the Tiles Controller execute()
> >
> > or
> >
> > The database query is performed in the Tiles Controller execute() and
the
> > query results will already by available to be fowarded to the JSP.
> >
> >  BOTH DO NOT WORK!
> >
> >
> > HERE IS THE CODE:
> >
> > public class LoadUserCategoriesController extends Action implements
> > Controller{
> >
> >             static private String myString = "string";
> >
> > public ActionForward execute( ComponentContext tilesContext,
> >                         ActionMapping mapping,
> >                         ActionForm form,
> >                         HttpServletRequest request,
> >                         HttpServletResponse response) throws Exception
> > {
> >                 //Data source works here but require results to be in
> > Controller execute
> >                 myString = "newstring";
> >
> >
> >
> > }
> >         public void execute(ComponentContext tilesContext,
> >                 HttpServletRequest request,
> >                 HttpServletResponse response,
> >                 ServletContext servletContext)throws Exception{
> >
> >                 Statement stmt = null;
> >                 ResultSet rs = null;
> >                 DataSource dataSource;
> >                 Connection conn=null;
> >
> >                 try {
> >                         dataSource = getDataSource(request);
> >                 }.............................
> >
> >
> >                 //myString does not show "newString" here.
> >
> >                  tilesContext.putAttribute("TEST" , "TEST");   //only
> works
> > if I remove the dataSource = getDataSource(request) statement.
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
>



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