为啥找不到"dataSource"呢?好郁闷!

06-03-18 big1980

为啥找不到"dataSource"呢?好郁闷! 
想运用struts+spring+jboss+jdbc做个项目,还没学过hibernate,所以想先从简单的做起,所以用了jboss下配置的mysql数据源,数据源没有问题(经过测试了,直接拿,不经过spring),现在想应用spring的事务管理,所以配置了jta。
报的错先给大家看一下:

java.lang.IllegalArgumentException: dataSource is required
org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:130)
org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:122)
org.thf.dao.UserDAO.selectUser(UserDAO.java:87)
org.thf.lg.LoginAction.execute(LoginAction.java:38)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

----------------------------------------------------------
然后列出相关文件的配置

web.xml

----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<display-name>ssh_web</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</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>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

</web-app>
----------------------------------------------------------
struts-config.xml
----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

<data-sources />
<!-- ===================== ActionForm ==================== -->
<form-beans >
<form-bean name="loginForm" type="org.thf.lg.LoginForm" />
</form-beans>


<global-exceptions />
<global-forwards>
<forward name="init" path="/jsp/login.jsp" />
</global-forwards>

<!-- ======================= Actions ==================== -->

<action-mappings >

<action 
path="/login"
name="loginForm"
type="org.springframework.web.struts.DelegatingActionProxy" >
<forward name="success" path="/jsp/success.jsp"/>
<forward name="failure" path="/jsp/login.jsp"/>
</action> 

</action-mappings>

<message-resources parameter="org.thf.ApplicationResources"/>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml"/>
</plug-in>



</struts-config>
----------------------------------------------------------
applicationContext.xml
----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for "sshapp" DispatcherServlet. -->
<beans>

<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" singleton="true">
<property name="jndiName">
<value>java:/MySqlDS</value>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>

<bean id="userDAO" class="org.thf.dao.UserDAO">
<property name="dataSource"> 
<ref bean="myDataSource"/> 
</property>
</bean>

<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref bean="userDAO" />
</property>

<property name="transactionAttributes">
<props>
<prop key="INSERT*">PROPAGATION_REQUIRED</prop>
<prop key="UPDATE*">PROPAGATION_REQUIRED</prop>
<prop key="DELETE*">PROPAGATION_REQUIRED</prop>
<prop key="SELECT*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

</beans>
----------------------------------------------------------
action-servlet.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 name="/login" class="org.thf.lg.LoginAction" singleton="false">
<property name="userDAO">
<ref bean="userDAOProxy" />
</property>
</bean>

</beans>

----------------------------------------------------------
UserDAO.java
----------------------------------------------------------
package org.thf.dao;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
//import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;

import org.thf.tabs.User;
import org.thf.lg.*;

public class UserDAO {

/** USER SELECT文 */
private static final String USER_SELECT = 
"SELECT USERID," +
" NAME, " +
" PASSWORD " +
"FROM USER " +
"WHERE USERID = :USERID";

/** USER INSERT文 */
private static final String USER_INSERT = 
"INSERT INTO `PRODUCTS` " +
"(ID, DESCRIPTION, PRICE, CLASS, SUBCLASS)" +
" VALUES" +
" (4,'Book',400.00,444,4444)," +
" (5,'Pen',500.00,555,5555), " +
" (6,'Ruler',600.00,666,6666);";

private DataSource dataSource;

private PlatformTransactionManager transactionManager;

public PlatformTransactionManager getTransactionManager() {
return transactionManager;
}

public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}

public DataSource executeTestSource() {
return dataSource;
}

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

//after set UserDAOProxy
public void insertUser(LoginForm loginForm) {
JdbcTemplate jt = new JdbcTemplate(executeTestSource());
jt.update(USER_INSERT);
}

List userList = new ArrayList();

public List getUserList(){
return userList;
}

public void setUserList(List userlist){
this.userList = userlist;
}

public void selectUser() {

JdbcTemplate jt = new JdbcTemplate(dataSource);
jt.query(USER_SELECT, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
User user = new User();
user.setUsername(rs.getString("NAME"));
user.setUserid(rs.getString("USERID"));
user.setPassword(rs.getString("PASSWORD"));
userList.add(user);
setUserList(userList);
}
});
}

}
----------------------------------------------------------
就这些,已经走到UserDAO里了,为什么拿不到dataSource呢?哪里可能设置有问题呢,大虾们来帮帮小弟看看!
<p>

banq
2006-03-20 11:45
确保myDataSource这个类书写正确,输入Spring的JdbcTemp参数正确.

big1980
2006-03-20 15:33
banq大哥能不能说详细点呢?我现在dataSource拿不到,是null,所以走不下去了,myDataSource是内部引用的,会有什么问题呢?能帮我详细看看吗?

猜你喜欢