Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struts Database Connectivity - Static Action Mapping

Status
Not open for further replies.

Mike1948

Programmer
Dec 24, 2006
1
GB
Dear Sirs, 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 in 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.

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 acception of changing the datasource to mine personal MySQL datasource, tutorial can be found here:


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
 
First of all, in this line
Code:
if(court1List != null){
request.setAttribute("SportsHall", court1List);
}
you're putting the result in an attribute in the request, so if this line
Code:
<%
java.util.ArrayList court1List=(java.util.ArrayList)
session.getAttribute("sports_hall_1");
%>
is to look for that attribute, you won't find it on session but in request.

Another thing is... how do you access the page through the browser? Because in order to the Action to be executed you must create a link to it (normally something.do, or whatever you defined in struts-config.xml (in this file you do not put the .do in the action)) instead of directly accessing the JSP.
To help you more, you should post your struts-config.xml, but I think that your problem is that you're accessing the jsp file directly without using the action.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top