Struts Database Connectivity - Static Action Mapping
Struts Database Connectivity - Static Action Mapping
(OP)
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:
http://w ww.netbean s.org/kb/5 0/tutorial -webapps-s truts.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
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:
http://w
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
RE: Struts Database Connectivity - Static Action Mapping
CODE
request.setAttribute("SportsHall", court1List);
}
CODE
java.util.ArrayList court1List=(java.util.ArrayList)
session.getAttribute("sports_hall_1");
%>
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.