Results 1 to 10 of 11

Thread: dataSource is required?...But, I am providing the datasource...Aren't I???

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Posts
    12

    Default dataSource is required?...But, I am providing the datasource...Aren't I???

    Hi All

    After reading a couple of text books on this topic, I thought I had it right.
    ...But, I was wrong...

    Please tell me what I am missing. Thanks!

    applicationContext.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>oracle.jdbc.OracleDr iver</value></property>
    <property name="url"><value>jdbc:oracle:thin:@localhost:1521 :ORCL</value></property>
    <property name="username"><value>scott</value></property>
    <property name="password"><value>tiger</value></property>
    </bean>

    <bean id="jsp1Bean" class="testjsf01.Jsp1Bean">
    <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>

    </beans>

    Jsp1Bean class:
    package testjsf01;

    import org.springframework.jdbc.core.*;
    import org.springframework.jdbc.core.support.JdbcDaoSuppo rt;
    import org.springframework.jdbc.datasource.DriverManagerD ataSource;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.core.io.*;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.sql.DataSource;
    import java.util.ArrayList;
    import javax.faces.model.DataModel;
    import javax.faces.model.ListDataModel;

    public class Jsp1Bean extends JdbcDaoSupport
    {
    private JdbcTemplate template;
    private DriverManagerDataSource dataSource;

    public Jsp1Bean ()
    {
    }

    public void setDataSource (DriverManagerDataSource dataSource)
    {
    this.dataSource = dataSource;
    }

    public DataModel getDataModel1() throws Exception
    {
    final ArrayList outList = new ArrayList();
    template = new JdbcTemplate(dataSource);

    class RowCallbackHandlerImpl implements RowCallbackHandler
    {
    public void processRow(ResultSet rs) throws SQLException
    {
    ArrayList sublist = new ArrayList();
    sublist.add(rs.getString(1));
    sublist.add(rs.getString(2));
    sublist.add(rs.getString(3));
    outList.add(sublist);
    }
    }

    template.query("select a.empno, a.ename, a.job from scott.emp a", new RowCallbackHandlerImpl());

    return new ListDataModel(outList);
    }
    }


    web.xml:
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>

    <context-param>
    <param-name>com.sun.faces.verifyObjects</param-name>
    <param-value>true</param-value>
    </context-param>

    <context-param>
    <param-name>com.sun.faces.validateXml</param-name>
    <param-value>true</param-value>
    </context-param>

    <context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>

    <context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
    </servlet-mapping>

    <session-config>
    <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <taglib>
    <taglib-uri>/spring</taglib-uri>
    <taglib-location>/WEB-INF/tlds/spring.tld</taglib-location>
    </taglib>
    </web-app>


    faces-context.xml:
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
    <faces-config>

    <application>
    <variable-resolver>org.springframework.web.jsf.DelegatingVar iableResolver</variable-resolver>
    </application>

    <managed-bean>
    <managed-bean-name>jsp1Bean</managed-bean-name>
    <managed-bean-class>testjsf01.Jsp1Bean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

    </faces-config>




    Error messages:

    Mar 17, 2006 1:08:10 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet Faces Servlet threw exception
    javax.faces.el.EvaluationException: javax.faces.el.EvaluationException: Error getting property 'dataModel1' from bean of type testjsf01.Jsp1Bean: java.lang.IllegalArgumentException: dataSource is required
    at com.sun.faces.el.ValueBindingImpl.getValue(ValueBi ndingImpl.java:206)
    at com.sun.faces.el.ValueBindingImpl.getValue(ValueBi ndingImpl.java:154)
    at javax.faces.component.UIData.getValue(UIData.java: 527)
    at javax.faces.component.UIData.getDataModel(UIData.j ava:856)
    -
    -
    -
    at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:595)
    Caused by: javax.faces.el.EvaluationException: Error getting property 'dataModel1' from bean of type testjsf01.Jsp1Bean: java.lang.IllegalArgumentException: dataSource is required
    at com.sun.faces.el.PropertyResolverImpl.getValue(Pro pertyResolverImpl.java:89)
    Last edited by sairndain; Mar 17th, 2006 at 01:05 PM.

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Turn on logging and see what happens. It seems that the app context file is not being read - you might have some cached file which is used instead.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Mar 2005
    Location
    San Francisco, CA
    Posts
    114

    Default

    Get rid of the managed-bean definition in your faces-context.xml file. You have a JSF managed bean named "jsp1Bean" and Spring is managing a bean named "jsp1Bean". JSF is resolving to its own managed bean rather than delagating to Spring. That is causing it to create a new bean, which doesn't have the dataSource set.

  4. #4
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    Couple of things:

    You extend JdbcDaoSupport which already has a dataSource property which takes the java.sql.DataSource interface as parameter. You are overriding this setter with one that takes a DriverManageDataSource as the parameter. I think this causes the JdbcDaoSupport to think that the dataSource has not been set. Just remove your dataSource setters/getters and property declaration in Jsp1Bean and use getDataSource or getJdbcTemplate methods in your implementation.

    Use a connection pool datasource like DBCP or C3P0 instead of the DriverManagerDataSource.

    I would remove the second jsp1Bean declaration - the one in faces-config - since you already declared this bean in your applicationContext. It's just confusing to have it declared in two places.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  5. #5
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    Dave is right, the bean defined in faces-config is the one you are getting. So once you remove that one you will be OK. I would still make the other changes I mentioned.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  6. #6
    Join Date
    Mar 2006
    Posts
    12

    Default

    Hi All

    Thanks very much for your feedback! (Its been virtually impossible find resources (books/web) that speak to this issue)

    In any case, as per your collective advice [except for removing the "jsf" stuff]...
    1. I've removed the jsf-related code from the example, because I just want to get the "dataSource" thing working

    2. I've removed the "overriding" dataSource setters/getters and property declaration from "Jsp1Bean.java"

    3. I've defined the dataSource in "applicationContext.xml" using "org.apache.commons.dbcp.BasicDataSource" instead of "DriverManageDataSource"

    So far, unfortunately, I still have an issue with "No DataSource specified"... --Has anyone tried this configuration (below) on their own machine(s)... I'm curious what you find? (I've spent all weekend on this issue with no resolution)

    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"><value>oracle.jdbc.OracleDr iver</value></property>
    <property name="url"><value>jdbc:oracle:thin:@localhost:1521 :ORCL</value></property>
    <property name="username"><value>scott</value></property>
    <property name="password"><value>tiger</value></property>
    </bean>
    <bean id="jsp1Bean" class="sprjsp.Jsp1Bean">
    <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>
    </beans>

    (NOTE: above, if I try class="javax.sql.DataSource" I receive error messages complaining that "javax.sql.DataSource" is an interface, etc...)

    web.xml
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>
    <session-config>
    <session-timeout>
    30
    </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>
    jsp1.jsp
    </welcome-file>
    </welcome-file-list>
    <taglib>
    <taglib-uri>/spring</taglib-uri>
    <taglib-location>/WEB-INF/tlds/spring.tld</taglib-location>
    </taglib>
    </web-app>

    Jsp1Bean.java
    package sprjsp;

    import org.springframework.jdbc.core.RowCallbackHandler;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.support.JdbcDaoSuppo rt;

    public class Jsp1Bean extends JdbcDaoSupport
    {
    private JdbcTemplate jdbcTemplate;
    private String theResult;

    public Jsp1Bean()
    {
    }

    public String getTheResult() throws Exception
    {
    final ArrayList outList = new ArrayList();

    jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(this.getDataSource());

    class RowCallbackHandlerImpl implements RowCallbackHandler
    {
    public void processRow(ResultSet rs) throws SQLException
    {
    ArrayList sublist = new ArrayList();
    sublist.add(rs.getString(1));
    sublist.add(rs.getString(2));
    sublist.add(rs.getString(3));
    System.out.println("String.valueOf(sublist)=" + String.valueOf(sublist));
    outList.add(sublist);
    }
    }

    jdbcTemplate.query("select a.empno, a.ename, a.job from scott.emp a", new RowCallbackHandlerImpl());

    return String.valueOf(outList);
    }
    }

    jsp1.jsp
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <h1>JSP Page</h1>
    <jsp:useBean id="jsp1Bean" scope="request" class="sprjsp.Jsp1Bean" type="sprjsp.Jsp1Bean"/>
    <jsp:getProperty name="jsp1Bean" property="theResult"/>
    </body>
    </html>

    ERROR MESSAGES...
    Mar 20, 2006 2:23:24 PM org.apache.catalina.core.ApplicationContext log
    INFO: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRul e: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterR ule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingR ule: Redirect URL: http://jakarta.apache.org]]
    Mar 20, 2006 2:23:24 PM org.apache.catalina.core.ApplicationContext log
    INFO: ContextListener: contextInitialized()
    Mar 20, 2006 2:23:24 PM org.apache.catalina.core.ApplicationContext log
    INFO: SessionListener: contextInitialized()
    Mar 20, 2006 2:23:25 PM org.apache.catalina.core.ApplicationContext log
    INFO: ContextListener: contextInitialized()
    Mar 20, 2006 2:23:25 PM org.apache.catalina.core.ApplicationContext log
    INFO: SessionListener: contextInitialized()
    Mar 20, 2006 2:23:26 PM org.apache.catalina.core.ApplicationContext log
    INFO: Loading Spring root WebApplicationContext
    Mar 20, 2006 2:23:28 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet jsp threw exception
    java.lang.IllegalArgumentException: No DataSource specified
    at org.springframework.util.Assert.notNull(Assert.jav a:116)
    at org.springframework.jdbc.datasource.DataSourceUtil s.doGetConnection(DataSourceUtils.java:98)
    at org.springframework.jdbc.datasource.DataSourceUtil s.getConnection(DataSourceUtils.java:77)
    at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:268)
    at org.springframework.jdbc.core.JdbcTemplate.query(J dbcTemplate.java:348)
    at org.springframework.jdbc.core.JdbcTemplate.query(J dbcTemplate.java:352)
    at sprjsp.Jsp1Bean.getTheResult(Jsp1Bean.java:39)
    at org.apache.jsp.jsp1_jsp._jspService(org.apache.jsp .jsp1_jsp:63)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:332)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
    at org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:362)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:178)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:869)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:667)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:595)

  7. #7
    Join Date
    Mar 2005
    Location
    San Francisco, CA
    Posts
    114

    Default

    I think that your Spring configuration looks fine. The problem is that you aren't actually getting a reference to the bean that has been configured by Spring in either your old code or your new code.

    What is happening in your new code is that when you use the jsp:useBean tag with class and type attributes a *new* bean gets instantiated. Since this bean is new, it doesn't have the dataSource set. That is why you are seeing that exception.

    What you need to do is to get a reference to the instance of the bean that Spring has configured. Typically you would do that in a controller and then pass the bean to the JSP as a model object. But since you are hitting your JSP directly you can't do that. And JSP itself isn't aware of Spring.

    I'd suggest trying the SpringMVC approach and wiring up a controller who's job is to get the bean and pass it the JSP. You are going to find a lot more examples down that path. Or I do think you were very close with the JSF approach if you just removed that one managed bean definition.

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •