Announcement Announcement Module
Collapse
No announcement yet.
DWR 3 with Spring3 ,DataSource is null and never gets initialized Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DWR 3 with Spring3 ,DataSource is null and never gets initialized

    I am new with DWT 3/Spring 3 & struggling for few days to get it working. I am using DWRServlet & dwr.xml for configuration & have a Class-DwrService directly accessed in my JSP.
    Functionality is about 2 dependent dynamic dropdowns in JSP page based on ajax calls based on example (except i use dwr.xml)
    http://www.codercorp.com/blog/spring...wr-spring.html

    If i use mock data objects in my Service to populate data,DWR works fine but when i insert real DB calls,it gives me IllegalArgumentExceptionataSource is null.
    I tried using @Autowired & everything else but cannt figure out real problem.

    I am copying configuration files here so that someone can help me finding where i am wrong.

    ------my dwr.xml--
    <dwr>
    <allow>
    <create creator="new" javascript="dwrService">
    <param name="beanName" value="dataSource"/>
    <param name="location" value="applicationContext.xml"/>
    <param name="class"
    value="com.console.util.DwrService"/>
    <include method="getAllForms"/>
    <include method="getEntitiesForForm"/>
    </create>
    <convert converter="bean"
    match="com.console.pojo.Form">
    <param name="class"
    value="code,description"/>
    </convert>

    <convert converter="bean"
    match="com.console.pojo.Entity">
    <param name="class"
    value="id,name"/>
    </convert>
    </allow>
    </dwr>

    -------applicationContext.xml----

    <beans xmlns="............... >

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/mydb" />

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="txManager"/>

    </beans>
    -----console-servlet.xml---

    <beans xmlns .......3.0.xsd">

    <context:component-scan base-package="com.ct.**">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Reposit ory"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service "/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Control ler"/>
    </context:component-scan>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2 .TilesConfigurer">
    <property name="definitions">
    <value> /WEB-INF/tiles-defs.xml </value>
    </property>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBas edViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2 .TilesView" />
    </bean>

    <bean id="jasperViewResolver" class="org.springframework.web.servlet.view.Resour ceBundleViewResolver">
    <property name="basename" value="views"/>
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.Sessio nLocaleResolver" />

    <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter" />

    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basenames">
    <list>
    <value>errors</value>
    <value>labels</value>
    </list>
    </property>
    </bean>
    <bean id="log4jDirectConfigurer" class="com.console.util.Log4jDirectConfigurer">
    <property name="location" value="classpath:log4j.properties"/>
    </bean>

    <bean id="appPropertyName" class="java.lang.String">
    <constructor-arg type="java.lang.String">
    <value>ew8_console_app.properties</value>
    </constructor-arg>
    </bean>
    <bean id="appPropertyFile" class="org.springframework.core.io.ClassPathResour ce">
    <constructor-arg type="java.lang.String"><ref bean="appPropertyName"/></constructor-arg>
    </bean>
    <bean id="appProperty" class="org.springframework.beans.factory.config.Pr opertiesFactoryBean">
    <property name="location" ref="appPropertyFile"/>
    </bean>

    <bean id="maxFileUploadSize" class="java.lang.Long">
    <constructor-arg type="long">
    <value>1000000</value>
    </constructor-arg>
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver">
    </bean>
    </beans>

    -------web.xml------

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">

    <display-name>MyConsole</display-name>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml
    /WEB-INF/console-servlet.xml
    </param-value>
    </context-param>
    <context-param>
    <param-name>log4jExposeWebAppRoot</param-name>
    <param-value>false</param-value>
    </context-param>
    <servlet>
    <servlet-name>console</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <filter>
    <filter-name>consoleFilter</filter-name>
    <filter-class>com.console.common.ConsoleSecurityFilter</filter-class>
    </filter>
    <filter>
    <filter-name>ResponseOverrideFilter</filter-name>
    <filter-class>org.displaytag.filter.ResponseOverrideFilter </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>consoleFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>consoleFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>ResponseOverrideFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    </filter-mapping>
    <servlet-mapping>
    <servlet-name>console</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.springframework.js.resource.ResourceServ let</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>/jsp/index.jsp</welcome-file>
    </welcome-file-list>
    <resource-ref>
    <res-ref-name>jdbc/mydb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

    <servlet>
    <display-name>DWR Servlet</display-name>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
    <param-name>debug</param-name>
    <param-value>true</param-value>

    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

    </web-app>

    ------- DwrService ----
    @Transactional
    @Service
    public class DwrService {

    DataSource dataSource;

    public List<FormType> getAllForms() throws ConsoleException {

    SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);
    List<Form> formsList = new ArrayList<Form>();
    ParameterizedRowMapper<Form> mapper = new ParameterizedRowMapper<Form>() {

    @Override
    public FormType mapRow(ResultSet rs, int rowNum) throws SQLException {

    Form form = new Form();
    form.setCode(rs.getString(2));
    form.setDescription(rs.getString(3));

    return form;
    }

    };
    String sql = "select * from L_FORMS";
    // SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource); //error here
    return jdbcTemplate.query(sql, mapper, Collections.emptyMap());

    }


    public List<Entity> getEntitiesForForm(String formId) throws ConsoleException {

    List<Entity> enList = new ArrayList<Entity>();
    String sql = "select * from L_FORM_ENTITY where FORM_ID";
    if (formId == null) {

    } else
    sql = sql.concat("='").concat(formId).concat("'");
    SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);//error here
    ParameterizedRowMapper<Entity> mapper = new ParameterizedRowMapper<Entity>() {

    @Override
    public Entity mapRow(ResultSet rs, int rowNum) throws SQLException {

    Entity entity = new Entity();
    entity.setEntityId(rs.getString(1));
    entity.setEntityName(rs.getString(2));

    return entity;
    }

    };

    return jdbcTemplate.query(sql, mapper, Collections.emptyMap());

    }
    }
    -------Also my AbstractDataClass.---

    @Repository("consoleDAO")
    public class AbstractDataAccess implements IDataAccess {

    private DataSource dataSource;
    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    }

    public DataSource getDataSource() {
    return this.dataSource;
    }

    protected SimpleJdbcTemplate getSimpleJdbcTemplate() {
    return new SimpleJdbcTemplate(dataSource);
    }

    protected SimpleJdbcCall getSimpleJdbcCall() {
    return new SimpleJdbcCall(dataSource);
    }

    protected JdbcTemplate getJdbcTemplate() {
    return new JdbcTemplate(dataSource);
    }

    }

    I tried using this AbstractDataAccess but still my dataSource in DwrService is null allthe time.

    I also tried refactoring the code and extracting out db calls to some separate DAO class (which complies with my design too) but then thinking about DWr conversion to javascript problems i kept single file.

    Please suggest me what is going wrong here.
    I can also paste mt JSP code but i think its java problem.

  • #2
    Use CODE tags when posting code / xml / stacktraces.

    You should use Spring / DWR integration. Configure DWR through Spring so that you can use @RemoteProxy on your service beans, which you can easily wire up with your daos.

    Use the procedure described here.

    Comment

    Working...
    X