Mike1948 wrote:
Dear Sirs, Apologies for the length of this post but I have read forums, book
and websites and cannot find how to do this. I'm sure it is very simpe but I
am a complete novice so any help would be much appreciated.
I am new to struts and have hit a road block concerning action mappings.
From everything that I have read the action class gets called ivn the event
of a form being submitted for example. However, I do not want any form to be
processed, just when the jsp page is accessed the action class is called.
An action will be invoked in response to any request whose URL matches
whatever the action servlet is mapped to. You don't have to submit a
form to invoke an action.
For what you want to do, you need to make your initial request go
through an action which then forwards to your JSP. For example, if you
have the action servlet mapped to *.do:
http://myhost/myapp/myaction.do
and in struts-config.xml
<action path="/myaction" type="MyActionClass">
<forward name="success" path="myjsp.jsp"/>
</action>
Hi current am using a struts datasource and a data access object. When a jsp
page is accessed I want to be able to store the rows of the database in an
array and iterate them back out to the jsp page. The Action class can be
found below
package com.myapp.struts;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.sql.*;
import java.util.ArrayList;
import javax.sql.*;
public class DataSourceConnectionAction extends Action {
private DataSource dataSource;
public ArrayList court1List = new ArrayList();
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
/** Here the method that connects to the datasource is called: */
dataSource =
(DataSource)servlet.getServletContext().getAttribute("SportsHall");
Connection conn = dataSource.getConnection();
UserDAO dao = DAOFactory.createUserDAO(conn);
court1List = dao.getCourt1();
/** Here we put the court1List in scope, so that we can use it in the JSP
page: */
if(court1List != null){
request.setAttribute("SportsHall", court1List);
} return (mapping.findForward(SUCCESS));
}
}
The jsp page then contains this:
<%
java.util.ArrayList court1List=(java.util.ArrayList)
session.getAttribute("sports_hall_1");
%>
<logic:notPresent scope="request"
name="SportsHall"><h2>Data source not in scope!</h2></logic:notPresent>
<logic:present name = "SportsHall">
<logic:empty name = "SportsHall">
<h2>Data source in scope but no data found!</h2>
</logic:empty>
</logic:present>
<logic:present name = "SportsHall">
The logic not present tag always shows that there is nothing there. Is this
because there is not an appropriate action mapping?
I used the tutorial under the section Working with a Struts Data Source: A
Simple Scenario with the only exception of changing the datasource to my
personal MySQL datasource, tutorial can be found here:
http://www.netbeans.org/kb/50/tutorial-webapps-struts.html
hence, I have a class called row with the getter and setter methods in.
Following this tutorial seemed appropriate as it was exactly what i needed
to do with the exception of not having a form to sumbit and I am using
Netbeans. I have the same setup as the tutorial with the classes:
UserDAO row DAOFactory MySQLUserDAO DataSourceConnection Action (As shown in
the original post)
Apologies but could you instruct for a complete begginer?
Many thanks for your help in advance, this is a big learning curve for me.
Regards
Mike
The tutorial must be a bit out of date; support for data sources in
Struts was long since deprecated, and is no longer supported in the
latest versions of Struts. Depending on what version of Struts you're
using, you may be able to stick with a Struts-based data source until
you get things working, but you should look at moving the data source
configuration to your container when you get a chance.
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]