Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Struts Database Connectivity - Static Action Mapping

Struts Database Connectivity - Static Action Mapping

Struts Database Connectivity - Static Action Mapping

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)
<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: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.

RE: Struts Database Connectivity - Static Action Mapping

First of all, in this line


if(court1List != null){
request.setAttribute("SportsHall", court1List);
you're putting the result in an attribute in the request, so if this line


java.util.ArrayList court1List=(java.util.ArrayList)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close